pull/1839/head
Rich Harris 7 years ago
parent a20bbc442e
commit b38d8e9bff

@ -22,7 +22,21 @@ function normalizeWarning(warning) {
return warning; return warning;
} }
describe('css', () => { function create(code) {
const fn = new Function('module', 'exports', 'require', code);
const module = { exports: {} };
fn(module, module.exports, id => {
if (id === 'svelte') return require('../../index.js');
if (id.startsWith('svelte/')) return require(id.replace('svelte', '../../'));
return require(id);
});
return module.exports.default;
}
describe.only('css', () => {
fs.readdirSync('test/css/samples').forEach(dir => { fs.readdirSync('test/css/samples').forEach(dir => {
if (dir[0] === '.') return; if (dir[0] === '.') return;
@ -41,54 +55,62 @@ describe('css', () => {
.replace(/\s+$/, ''); .replace(/\s+$/, '');
const expectedWarnings = (config.warnings || []).map(normalizeWarning); const expectedWarnings = (config.warnings || []).map(normalizeWarning);
const warnings = []; const domWarnings = [];
const ssrWarnings = [];
const { js, css } = svelte.compile( const dom = svelte.compile(
input, input,
Object.assign(config, { Object.assign(config, {
format: 'cjs', format: 'cjs',
name: 'SvelteComponent', name: 'SvelteComponent',
onwarn: warning => { onwarn: warning => {
warnings.push(warning); domWarnings.push(warning);
} }
}) })
); );
// we do this here, rather than in the expected.html !== null const ssr = svelte.compile(
// block, to verify that valid code was generated input,
const fn = new Function('module', 'exports', 'require', js.code); Object.assign(config, {
format: 'cjs',
generate: 'ssr',
name: 'SvelteComponent',
onwarn: warning => {
ssrWarnings.push(warning);
}
})
);
console.log(warnings); assert.equal(dom.css.code, ssr.css.code);
console.log(expectedWarnings);
assert.deepEqual(warnings.map(normalizeWarning), expectedWarnings);
fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, css.code); assert.deepEqual(
domWarnings.map(normalizeWarning),
ssrWarnings.map(normalizeWarning)
);
assert.deepEqual(domWarnings.map(normalizeWarning), expectedWarnings);
fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, dom.css.code);
const expected = { const expected = {
html: read(`test/css/samples/${dir}/expected.html`), html: read(`test/css/samples/${dir}/expected.html`),
css: read(`test/css/samples/${dir}/expected.css`) css: read(`test/css/samples/${dir}/expected.css`)
}; };
assert.equal(css.code.replace(/svelte(-ref)?-[a-z0-9]+/g, (m, $1) => $1 ? m : 'svelte-xyz'), expected.css); assert.equal(dom.css.code.replace(/svelte(-ref)?-[a-z0-9]+/g, (m, $1) => $1 ? m : 'svelte-xyz'), expected.css);
// we do this here, rather than in the expected.html !== null
// block, to verify that valid code was generated
const ClientComponent = create(dom.js.code);
const ServerComponent = create(ssr.js.code);
// verify that the right elements have scoping selectors // verify that the right elements have scoping selectors
if (expected.html !== null) { if (expected.html !== null) {
const window = env(); const window = env();
const module = { exports: {} };
fn(module, module.exports, id => {
if (id === 'svelte') return require('../../index.js');
if (id.startsWith('svelte/')) return require(id.replace('svelte', '../../'));
return require(id);
});
const { default: Component, $render } = module.exports;
try {
// dom // dom
try {
const target = window.document.querySelector('main'); const target = window.document.querySelector('main');
new Component({ target, props: config.props }); new ClientComponent({ target, data: config.data });
const html = target.innerHTML; const html = target.innerHTML;
fs.writeFileSync(`test/css/samples/${dir}/_actual.html`, html); fs.writeFileSync(`test/css/samples/${dir}/_actual.html`, html);
@ -99,17 +121,22 @@ describe('css', () => {
); );
window.document.head.innerHTML = ''; // remove added styles window.document.head.innerHTML = ''; // remove added styles
} catch (err) {
console.log(dom.js.code);
throw err;
}
// ssr // ssr
try {
assert.equal( assert.equal(
normalizeHtml( normalizeHtml(
window, window,
$render(config.props).html.replace(/svelte(-ref)?-[a-z0-9]+/g, (m, $1) => $1 ? m : 'svelte-xyz') ServerComponent.render(config.data).html.replace(/svelte(-ref)?-[a-z0-9]+/g, (m, $1) => $1 ? m : 'svelte-xyz')
), ),
normalizeHtml(window, expected.html) normalizeHtml(window, expected.html)
); );
} catch (err) { } catch (err) {
console.log(js.code); console.log(ssr.js.code);
throw err; throw err;
} }
} }

Loading…
Cancel
Save