From 0cbd20120023f6b53065dd05f56d8f6d46388ffc Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 26 Jun 2017 08:09:41 -0400 Subject: [PATCH] set up tests for omitted scoping attributes --- test/css/index.js | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/test/css/index.js b/test/css/index.js index 51cf1b4aba..9566667c65 100644 --- a/test/css/index.js +++ b/test/css/index.js @@ -1,6 +1,6 @@ import assert from "assert"; import * as fs from "fs"; -import { svelte } from "../helpers.js"; +import { env, svelte } from "../helpers.js"; function tryRequire(file) { try { @@ -23,19 +23,45 @@ describe("css", () => { } (solo ? it.only : it)(dir, () => { - const config = tryRequire(`./samples/${dir}/_config.js`) || {}; + const config = Object.assign(tryRequire(`./samples/${dir}/_config.js`) || {}, { + format: 'iife', + name: 'SvelteComponent' + }); const input = fs .readFileSync(`test/css/samples/${dir}/input.html`, "utf-8") .replace(/\s+$/, ""); - const actual = svelte.compile(input, config).css; - fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, actual); - const expected = fs.readFileSync( - `test/css/samples/${dir}/expected.css`, - "utf-8" - ); + const actual = svelte.compile(input, config); + fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, actual.css); + const expected = { + html: read(`test/css/samples/${dir}/expected.html`), + css: read(`test/css/samples/${dir}/expected.css`) + }; - assert.equal(actual.trim(), expected.trim()); + assert.equal(actual.css.trim(), expected.css.trim()); + + // verify that the right elements have scoping selectors + if (expected.html !== null) { + return env().then(window => { + const Component = eval(`(function () { ${actual.code}; return SvelteComponent; }())`); + const target = window.document.querySelector("main"); + + new Component({ target }); + const html = target.innerHTML; + + fs.writeFileSync(`test/css/samples/${dir}/_actual.html`, html); + + assert.equal(html.trim(), expected.html.trim()); + }); + } }); }); }); + +function read(file) { + try { + return fs.readFileSync(file, 'utf-8'); + } catch(err) { + return null; + } +} \ No newline at end of file