2016-12-08 11:48:54 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2017-01-17 19:46:37 +01:00
|
|
|
import { exists, loadConfig, setupHtmlEqual, svelte, tryToLoadJson } from './helpers.js';
|
2016-12-08 11:48:54 +01:00
|
|
|
|
|
|
|
function tryToReadFile ( file ) {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync( file, 'utf-8' );
|
|
|
|
} catch ( err ) {
|
|
|
|
if ( err.code !== 'ENOENT' ) throw err;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe( 'ssr', () => {
|
|
|
|
before( () => {
|
|
|
|
require( process.env.COVERAGE ?
|
|
|
|
'../src/server-side-rendering/register.js' :
|
|
|
|
'../ssr/register' );
|
2016-12-16 22:17:31 +01:00
|
|
|
|
|
|
|
return setupHtmlEqual();
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.readdirSync( 'test/server-side-rendering' ).forEach( dir => {
|
|
|
|
if ( dir[0] === '.' ) return;
|
|
|
|
|
|
|
|
const solo = exists( `test/server-side-rendering/${dir}/solo` );
|
|
|
|
|
2017-01-04 20:07:05 +01:00
|
|
|
if ( solo && process.env.CI ) {
|
|
|
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
|
|
|
}
|
|
|
|
|
2016-12-08 11:48:54 +01:00
|
|
|
( solo ? it.only : it )( dir, () => {
|
|
|
|
const component = require( `./server-side-rendering/${dir}/main.html` );
|
|
|
|
|
|
|
|
const expectedHtml = tryToReadFile( `test/server-side-rendering/${dir}/_expected.html` );
|
|
|
|
const expectedCss = tryToReadFile( `test/server-side-rendering/${dir}/_expected.css` ) || '';
|
|
|
|
|
|
|
|
const data = tryToLoadJson( `test/server-side-rendering/${dir}/data.json` );
|
|
|
|
const html = component.render( data );
|
|
|
|
const { css } = component.renderCss();
|
|
|
|
|
|
|
|
fs.writeFileSync( `test/server-side-rendering/${dir}/_actual.html`, html );
|
|
|
|
if ( css ) fs.writeFileSync( `test/server-side-rendering/${dir}/_actual.css`, css );
|
|
|
|
|
|
|
|
assert.htmlEqual( html, expectedHtml );
|
|
|
|
assert.equal( css.replace( /^\s+/gm, '' ), expectedCss.replace( /^\s+/gm, '' ) );
|
|
|
|
});
|
|
|
|
});
|
2017-01-17 19:46:37 +01:00
|
|
|
|
|
|
|
// duplicate client-side tests, as far as possible
|
|
|
|
fs.readdirSync( 'test/generator' ).forEach( dir => {
|
|
|
|
if ( dir[0] === '.' ) return;
|
|
|
|
|
|
|
|
const config = loadConfig( `./generator/${dir}/_config.js` );
|
|
|
|
|
|
|
|
if ( config.solo && process.env.CI ) {
|
|
|
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
|
|
|
}
|
2017-01-24 18:04:20 +01:00
|
|
|
|
|
|
|
if ( config['skip-ssr'] ) return;
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-01-24 18:04:20 +01:00
|
|
|
( config.skip ? it.skip : config.solo ? it.only : it )( dir, () => {
|
2017-01-17 19:46:37 +01:00
|
|
|
try {
|
|
|
|
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' );
|
|
|
|
svelte.compile( source, { generate: 'ssr' });
|
|
|
|
} catch ( err ) {
|
|
|
|
if ( config.compileError ) {
|
|
|
|
config.compileError( err );
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const component = require( `./generator/${dir}/main.html` );
|
|
|
|
|
|
|
|
const html = component.render( config.data );
|
|
|
|
|
|
|
|
if ( config.html ) {
|
|
|
|
assert.htmlEqual( html, config.html );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|