From f199c69eabb67fec9207ed74cfdf66c4a9b4e6b9 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 13 Oct 2021 16:57:27 -0400 Subject: [PATCH] Brings back astro-dynamic tests (#1548) --- packages/astro/package.json | 2 +- packages/astro/test/astro-dynamic.test.js | 37 ++++++------------- .../src/pages/default-rename.astro | 12 ------ .../astro-dynamic/src/pages/index.astro | 2 +- .../client-only.astro | 0 packages/renderers/renderer-preact/server.js | 18 +++++---- yarn.lock | 8 ++-- 7 files changed, 29 insertions(+), 50 deletions(-) delete mode 100644 packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro rename packages/astro/test/fixtures/astro-dynamic/src/{pages => skipped-pages}/client-only.astro (100%) diff --git a/packages/astro/package.json b/packages/astro/package.json index 7ed4267a9..9665f89d2 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -40,7 +40,7 @@ "test": "mocha --parallel --timeout 15000" }, "dependencies": { - "@astrojs/compiler": "^0.1.12", + "@astrojs/compiler": "^0.1.13", "@astrojs/language-server": "^0.7.16", "@astrojs/markdown-remark": "^0.3.1", "@astrojs/markdown-support": "0.3.1", diff --git a/packages/astro/test/astro-dynamic.test.js b/packages/astro/test/astro-dynamic.test.js index 47738d12f..2cc89f97f 100644 --- a/packages/astro/test/astro-dynamic.test.js +++ b/packages/astro/test/astro-dynamic.test.js @@ -1,5 +1,3 @@ -/** - * UNCOMMENT: fix transform error and "window is not defined" Vite error import { expect } from 'chai'; import cheerio from 'cheerio'; import { loadFixture } from './test-utils.js'; @@ -12,37 +10,29 @@ before(async () => { }); describe('Dynamic components', () => { - it('Loads client-only packages', async () => { - const html = await fixture.fetch('/index.html'); + it('Loads packages that only run code in client', async () => { + const html = await fixture.readFile('/index.html'); - // Grab the react-dom import - const exp = /import\("(.+?)"\)/g; - let match, reactRenderer; - while ((match = exp.exec(html))) { - if (match[1].includes('renderers/renderer-react/client.js')) { - reactRenderer = match[1]; - } - } - - // test 1: React renderer is on the page - expect(reactRenderer).to.be.ok; - - // test 2: Can load React renderer - // const result = await fixture.fetch(reactRenderer); - // expect(result.status).to.equal(200); + const $ = cheerio.load(html) + expect($('script').length).to.eq(2) }); it('Loads pages using client:media hydrator', async () => { + const root = new URL('http://example.com/media/index.html'); const html = await fixture.readFile('/media/index.html'); + const $ = cheerio.load(html); // test 1: static value rendered - expect(html).to.include(`value: "(max-width: 700px)"`); + + let js = await fixture.readFile(new URL($('script').attr('src'), root).pathname); + expect(js).to.include(`value:"(max-width: 700px)"`); // test 2: dynamic value rendered - expect(html).to.include(`value: "(max-width: 600px)"`); + js = await fixture.readFile(new URL($('script').eq(1).attr('src'), root).pathname); + expect(js).to.include(`value:"(max-width: 600px)"`); }); - it('Loads pages using client:only hydrator', async () => { + it.skip('Loads pages using client:only hydrator', async () => { const html = await fixture.readFile('/client-only/index.html'); const $ = cheerio.load(html); @@ -66,6 +56,3 @@ describe('Dynamic components', () => { // expect(result.status).to.equal(200); }); }); -*/ - -it.skip('is skipped', () => {}); diff --git a/packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro b/packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro deleted file mode 100644 index 95b4bb538..000000000 --- a/packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro +++ /dev/null @@ -1,12 +0,0 @@ ---- -import CounterRenamed from '../components/Counter.jsx'; -import SvelteCounterRenamed from '../components/SvelteCounter.svelte'; ---- - -Dynamic pages - - - - - - diff --git a/packages/astro/test/fixtures/astro-dynamic/src/pages/index.astro b/packages/astro/test/fixtures/astro-dynamic/src/pages/index.astro index 4dbb7f2c6..80367e195 100644 --- a/packages/astro/test/fixtures/astro-dynamic/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-dynamic/src/pages/index.astro @@ -8,6 +8,6 @@ import SvelteCounter from '../components/SvelteCounter.svelte'; - + \ No newline at end of file diff --git a/packages/astro/test/fixtures/astro-dynamic/src/pages/client-only.astro b/packages/astro/test/fixtures/astro-dynamic/src/skipped-pages/client-only.astro similarity index 100% rename from packages/astro/test/fixtures/astro-dynamic/src/pages/client-only.astro rename to packages/astro/test/fixtures/astro-dynamic/src/skipped-pages/client-only.astro diff --git a/packages/renderers/renderer-preact/server.js b/packages/renderers/renderer-preact/server.js index b0e682e25..a48eb364d 100644 --- a/packages/renderers/renderer-preact/server.js +++ b/packages/renderers/renderer-preact/server.js @@ -9,16 +9,20 @@ function check(Component, props, children) { return BaseComponent.isPrototypeOf(Component); } - const { html } = renderToStaticMarkup(Component, props, children); + try { + const { html } = renderToStaticMarkup(Component, props, children); - if (typeof html !== 'string') { + if (typeof html !== 'string') { + return false; + } + + // There are edge cases (SolidJS) where Preact *might* render a string, + // but components would be + + return !/\/.test(html); + } catch { return false; } - - // There are edge cases (SolidJS) where Preact *might* render a string, - // but components would be - - return !/\/.test(html); } function renderToStaticMarkup(Component, props, children) { diff --git a/yarn.lock b/yarn.lock index 23e75b30b..359776c52 100644 --- a/yarn.lock +++ b/yarn.lock @@ -106,10 +106,10 @@ "@algolia/logger-common" "4.10.5" "@algolia/requester-common" "4.10.5" -"@astrojs/compiler@^0.1.12": - version "0.1.12" - resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-0.1.12.tgz#e20fd240044505bea509e62c10f6e2a725e56e7a" - integrity sha512-oBPK6Rw9K+En1rSB8/15YXF7fC4QhJKwzGaQ8a7AFMga8hJqQwY8rReiwmatAe/6lYNUKJjeLOLO3nE2lGXhzQ== +"@astrojs/compiler@^0.1.13": + version "0.1.13" + resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-0.1.13.tgz#06c810d71c7bcce1603ea77f9381940523544e69" + integrity sha512-t/soXPJ34AQ67goh8EInCNVWd2HYOpgL5hE2xcZGwG74cIuczT4c+Guiqt50KYF5a7eNTdolPdy/dd7yipW8nQ== dependencies: typescript "^4.3.5"