From 556ad97f47c288feac45831e2495f261ab6f46a0 Mon Sep 17 00:00:00 2001 From: Yuichiro Yamashita Date: Sat, 3 Sep 2022 22:37:01 +0900 Subject: [PATCH] add tests --- test/runtime-puppeteer/index.ts | 17 ++++++++++++++-- .../samples/style_manager-cleanup/_config.js | 17 ++++++++++++++++ .../samples/style_manager-cleanup/main.svelte | 16 +++++++++++++++ .../samples/transition-css-out-in/_config.js | 14 +++++++++++++ .../samples/transition-css-out-in/main.svelte | 20 +++++++++++++++++++ .../samples/style_manager-cleanup/_config.js | 14 ------------- .../samples/style_manager-cleanup/main.svelte | 14 ------------- 7 files changed, 82 insertions(+), 30 deletions(-) create mode 100644 test/runtime-puppeteer/samples/style_manager-cleanup/_config.js create mode 100644 test/runtime-puppeteer/samples/style_manager-cleanup/main.svelte create mode 100644 test/runtime-puppeteer/samples/transition-css-out-in/_config.js create mode 100644 test/runtime-puppeteer/samples/transition-css-out-in/main.svelte delete mode 100644 test/runtime/samples/style_manager-cleanup/_config.js delete mode 100644 test/runtime/samples/style_manager-cleanup/main.svelte diff --git a/test/runtime-puppeteer/index.ts b/test/runtime-puppeteer/index.ts index 8e6f813de4..e4bcb108b3 100644 --- a/test/runtime-puppeteer/index.ts +++ b/test/runtime-puppeteer/index.ts @@ -117,8 +117,12 @@ describe('runtime (puppeteer)', function() { load(id) { if (id === 'main') { return ` - import SvelteComponent from ${JSON.stringify(path.join(__dirname, 'samples', dir, 'main.svelte'))}; - import config from ${JSON.stringify(path.join(__dirname, 'samples', dir, '_config.js'))}; + import SvelteComponent from ${JSON.stringify( + path.join(__dirname, "samples", dir, "main.svelte") + )}; + import config from ${JSON.stringify( + path.join(__dirname, "samples", dir, "_config.js") + )}; import * as assert from 'assert'; export default async function (target) { @@ -140,6 +144,14 @@ describe('runtime (puppeteer)', function() { const component = new SvelteComponent(options); + const waitUntil = async (fn, ms = 500) => { + const start = new Date().getTime(); + do { + if (fn()) return; + await new Promise(resolve => window.setTimeout(resolve, 1)); + } while (new Date().getTime() <= start + ms); + }; + if (config.html) { assert.htmlEqual(target.innerHTML, config.html); } @@ -150,6 +162,7 @@ describe('runtime (puppeteer)', function() { component, target, window, + waitUntil, }); component.$destroy(); diff --git a/test/runtime-puppeteer/samples/style_manager-cleanup/_config.js b/test/runtime-puppeteer/samples/style_manager-cleanup/_config.js new file mode 100644 index 0000000000..b3a5a97e45 --- /dev/null +++ b/test/runtime-puppeteer/samples/style_manager-cleanup/_config.js @@ -0,0 +1,17 @@ +export default { + skip_if_ssr: true, + skip_if_hydrate: true, + skip_if_hydrate_from_ssr: true, + test: async ({ component, assert, window, waitUntil }) => { + assert.htmlEqual(window.document.head.innerHTML, ''); + component.visible = true; + assert.htmlEqual(window.document.head.innerHTML, ''); + await waitUntil(() => window.document.head.innerHTML === ''); + assert.htmlEqual(window.document.head.innerHTML, ''); + + component.visible = false; + assert.htmlEqual(window.document.head.innerHTML, ''); + await waitUntil(() => window.document.head.innerHTML === ''); + assert.htmlEqual(window.document.head.innerHTML, ''); + } +}; diff --git a/test/runtime-puppeteer/samples/style_manager-cleanup/main.svelte b/test/runtime-puppeteer/samples/style_manager-cleanup/main.svelte new file mode 100644 index 0000000000..ab13073b1e --- /dev/null +++ b/test/runtime-puppeteer/samples/style_manager-cleanup/main.svelte @@ -0,0 +1,16 @@ + + +{#if visible} +
+{/if} diff --git a/test/runtime-puppeteer/samples/transition-css-out-in/_config.js b/test/runtime-puppeteer/samples/transition-css-out-in/_config.js new file mode 100644 index 0000000000..a50d28b257 --- /dev/null +++ b/test/runtime-puppeteer/samples/transition-css-out-in/_config.js @@ -0,0 +1,14 @@ +export default { + test: async ({ assert, component, window, waitUntil }) => { + component.visible = true; + await waitUntil(() => window.document.head.querySelector('style').sheet.rules.length === 2); + assert.equal(window.document.head.querySelector('style').sheet.rules.length, 2); + await waitUntil(() => window.document.head.querySelector('style') === null); + assert.equal(window.document.head.querySelector('style'), null); + component.visible = false; + await waitUntil(() => window.document.head.querySelector('style').sheet.rules.length === 2); + assert.equal(window.document.head.querySelector('style').sheet.rules.length, 2); + await waitUntil(() => window.document.head.querySelector('style') === null); + assert.equal(window.document.head.querySelector('style'), null); + } +}; diff --git a/test/runtime-puppeteer/samples/transition-css-out-in/main.svelte b/test/runtime-puppeteer/samples/transition-css-out-in/main.svelte new file mode 100644 index 0000000000..3ee93f0b55 --- /dev/null +++ b/test/runtime-puppeteer/samples/transition-css-out-in/main.svelte @@ -0,0 +1,20 @@ + + +{#if visible} +
+{/if} + +{#if !visible} +
+{/if} diff --git a/test/runtime/samples/style_manager-cleanup/_config.js b/test/runtime/samples/style_manager-cleanup/_config.js deleted file mode 100644 index 7609d76cf2..0000000000 --- a/test/runtime/samples/style_manager-cleanup/_config.js +++ /dev/null @@ -1,14 +0,0 @@ -export default { - skip_if_ssr: true, - skip_if_hydrate: true, - skip_if_hydrate_from_ssr: true, - test({ raf, assert, component, window }) { - component.visible = true; - raf.tick(100); - component.visible = false; - raf.tick(200); - raf.tick(0); - - assert.htmlEqual(window.document.head.innerHTML, ''); - } -}; diff --git a/test/runtime/samples/style_manager-cleanup/main.svelte b/test/runtime/samples/style_manager-cleanup/main.svelte deleted file mode 100644 index c2ccdc7c09..0000000000 --- a/test/runtime/samples/style_manager-cleanup/main.svelte +++ /dev/null @@ -1,14 +0,0 @@ - - - {#if visible} -
- {/if} \ No newline at end of file