Skip to content

Commit ff32bbc

Browse files
committed
Verify rich Pages serialization across integrations, closes #606.
1 parent d2b4fd6 commit ff32bbc

9 files changed

Lines changed: 38 additions & 25 deletions

File tree

‎packages/react/test/integration/md-layout-frontmatter.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { expect, it } from 'vite-plus/test';
88
import { waitForIslandsHydrated } from '../../../../test/browser-helpers.ts';
99
import { openIslandPage, type IslandPageVisit } from '../../../../test/island-page-browser.ts';
1010
import { voidMarkdown } from '../../../md/src/plugin.ts';
11+
import { islandFrontmatterScript } from '../../../void/src/pages/serialize.ts';
1112
import { islandsPlugin } from '../../src/plugin-islands.ts';
1213

13-
const ISLAND_FRONTMATTER_SCRIPT =
14-
'<script type="application/json" id="__void_frontmatter">{"title":"Hello from frontmatter"}</script>';
14+
const ISLAND_FRONTMATTER_SCRIPT = islandFrontmatterScript({ title: 'Hello from frontmatter' });
1515

1616
type IslandRenderer = {
1717
renderIslandPage(

‎packages/solid/test/integration/md-layout-frontmatter.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { expect, it } from 'vite-plus/test';
99
import { waitForIslandsHydrated } from '../../../../test/browser-helpers.ts';
1010
import { openIslandPage, type IslandPageVisit } from '../../../../test/island-page-browser.ts';
1111
import { voidMarkdown } from '../../../md/src/plugin.ts';
12+
import { islandFrontmatterScript } from '../../../void/src/pages/serialize.ts';
1213
import { islandsPlugin } from '../../src/plugin-islands.ts';
1314

14-
const ISLAND_FRONTMATTER_SCRIPT =
15-
'<script type="application/json" id="__void_frontmatter">{"title":"Hello from frontmatter"}</script>';
15+
const ISLAND_FRONTMATTER_SCRIPT = islandFrontmatterScript({ title: 'Hello from frontmatter' });
1616

1717
type IslandRenderer = {
1818
renderIslandPage(

‎packages/svelte/test/integration/md-layout-frontmatter.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { expect, it } from 'vite-plus/test';
99
import { waitForIslandsHydrated } from '../../../../test/browser-helpers.ts';
1010
import { openIslandPage, type IslandPageVisit } from '../../../../test/island-page-browser.ts';
1111
import { voidMarkdown } from '../../../md/src/plugin.ts';
12+
import { islandFrontmatterScript } from '../../../void/src/pages/serialize.ts';
1213
import { islandsPlugin } from '../../src/plugin-islands.ts';
1314

14-
const ISLAND_FRONTMATTER_SCRIPT =
15-
'<script type="application/json" id="__void_frontmatter">{"title":"Hello from frontmatter"}</script>';
15+
const ISLAND_FRONTMATTER_SCRIPT = islandFrontmatterScript({ title: 'Hello from frontmatter' });
1616

1717
type IslandRenderer = {
1818
renderIslandPage(

‎packages/void/test/integration/node-static.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../../../../test/integration-helpers.ts';
1212
import { renderVoidConfig } from '../../src/config-write.ts';
1313
import { readConfig } from '../../src/config.ts';
14+
import { deserializePageData } from '../../src/pages/serialize.ts';
1415
import { collectPrerenderPathsNode } from '../../src/prerender.ts';
1516

1617
declare global {
@@ -196,13 +197,13 @@ describe('playground/node-static build (target: node, output: static)', () => {
196197
});
197198

198199
it('emits static page data for prerendered routes', () => {
199-
const indexData = JSON.parse(
200+
const indexData = deserializePageData(
200201
readFileSync(join(DIST, 'client/_void/pages/index.json'), 'utf-8'),
201202
) as { component: string; props: Record<string, unknown>; url: string };
202-
const aboutData = JSON.parse(
203+
const aboutData = deserializePageData(
203204
readFileSync(join(DIST, 'client/_void/pages/about.json'), 'utf-8'),
204205
) as { component: string; props: Record<string, unknown>; url: string };
205-
const legacyData = JSON.parse(
206+
const legacyData = deserializePageData(
206207
readFileSync(join(DIST, 'client/_void/pages/legacy.json'), 'utf-8'),
207208
) as { component: string; props: Record<string, unknown>; url: string };
208209

@@ -344,7 +345,7 @@ describe('playground/node-static build (target: node, output: static)', () => {
344345
const routerUrl = () => page.textContent('[data-testid="router-url"]');
345346

346347
// The page HTML and its static page data were rendered for the path, without a query.
347-
const pageData = JSON.parse(
348+
const pageData = deserializePageData(
348349
readFileSync(join(DIST, 'client/_void/pages/router-url/plain.json'), 'utf-8'),
349350
) as { url: string };
350351
expect(pageData.url).toBe('/router-url/plain');

‎packages/void/test/integration/pg-test.test.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import spawn from 'cross-spawn';
66
import { createServer } from 'pglite-server';
77
import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
88
import { killChild, startServer } from '../../../../test/integration-helpers.ts';
9+
import { decodePageData } from '../../src/pages/serialize.ts';
910

1011
const ROOT = join(import.meta.dirname, '..', '..', '..', '..');
1112
const ENV_PATH = join(ROOT, 'playground/pg-test/.env');
@@ -118,7 +119,7 @@ describe('playground/pg-test', () => {
118119
const propsRes = await fetch(BASE + '/', {
119120
headers: { 'X-VoidPages': 'true' },
120121
});
121-
const data = (await propsRes.json()) as PageData;
122+
const data = decodePageData<PageData>(await propsRes.json());
122123
expect(data.props.users).toEqual(
123124
expect.arrayContaining([expect.objectContaining({ name: 'Alice', email: 'alice@test.com' })]),
124125
);
@@ -127,7 +128,7 @@ describe('playground/pg-test', () => {
127128
it('creates a post via action', async () => {
128129
// Get the user id first
129130
const propsRes = await fetch(BASE + '/', { headers: { 'X-VoidPages': 'true' } });
130-
const data = (await propsRes.json()) as PageData;
131+
const data = decodePageData<PageData>(await propsRes.json());
131132
const alice = data.props.users.find((u) => u.name === 'Alice');
132133
expect(alice).toBeTruthy();
133134
if (!alice) {
@@ -152,15 +153,15 @@ describe('playground/pg-test', () => {
152153

153154
// Verify post appears
154155
const postsRes = await fetch(BASE + '/', { headers: { 'X-VoidPages': 'true' } });
155-
const postsData = (await postsRes.json()) as PageData;
156+
const postsData = decodePageData<PageData>(await postsRes.json());
156157
expect(postsData.props.posts).toEqual(
157158
expect.arrayContaining([expect.objectContaining({ title: 'Hello World', author: 'Alice' })]),
158159
);
159160
});
160161

161162
it('deletes a post via action', async () => {
162163
const propsRes = await fetch(BASE + '/', { headers: { 'X-VoidPages': 'true' } });
163-
const data = (await propsRes.json()) as PageData;
164+
const data = decodePageData<PageData>(await propsRes.json());
164165
const post = data.props.posts.find((p) => p.title === 'Hello World');
165166
expect(post).toBeTruthy();
166167
if (!post) {
@@ -179,14 +180,14 @@ describe('playground/pg-test', () => {
179180
expect(deleteRes.status).toBeLessThan(400);
180181

181182
const afterRes = await fetch(BASE + '/', { headers: { 'X-VoidPages': 'true' } });
182-
const afterData = (await afterRes.json()) as PageData;
183+
const afterData = decodePageData<PageData>(await afterRes.json());
183184
expect(afterData.props.posts.find((p) => p.title === 'Hello World')).toBeUndefined();
184185
});
185186

186187
it('deletes a user and cascade-deletes their posts', async () => {
187188
// Create a post for Alice first
188189
const propsRes = await fetch(BASE + '/', { headers: { 'X-VoidPages': 'true' } });
189-
const data = (await propsRes.json()) as PageData;
190+
const data = decodePageData<PageData>(await propsRes.json());
190191
const alice = data.props.users.find((u) => u.name === 'Alice');
191192

192193
expect(alice).toBeTruthy();
@@ -215,7 +216,7 @@ describe('playground/pg-test', () => {
215216
expect(deleteRes.status).toBeLessThan(400);
216217

217218
const afterRes = await fetch(BASE + '/', { headers: { 'X-VoidPages': 'true' } });
218-
const afterData = (await afterRes.json()) as PageData;
219+
const afterData = decodePageData<PageData>(await afterRes.json());
219220
expect(afterData.props.users.find((u) => u.name === 'Alice')).toBeUndefined();
220221
expect(afterData.props.posts.find((p) => p.author === 'Alice')).toBeUndefined();
221222
});

‎packages/void/test/integration/static-pages.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { join } from 'node:path';
44
import { Miniflare, convertV4MiniflareOptions } from 'miniflare';
55
import { beforeAll, describe, expect, it } from 'vite-plus/test';
66
import { GENERATED_ASSET_HEADERS_MARKER } from '../../src/asset-worker-bypass.ts';
7+
import { deserializePageData } from '../../src/pages/serialize.ts';
78
import { collectPrerenderPaths } from '../../src/prerender.ts';
89

910
const ROOT = join(import.meta.dirname, '..', '..', '..', '..');
@@ -94,10 +95,10 @@ describe('playground/static-pages build (output: static)', () => {
9495
});
9596

9697
it('emits static page data for prerendered routes', () => {
97-
const indexData = JSON.parse(
98+
const indexData = deserializePageData(
9899
readFileSync(join(DIST, 'client/_void/pages/index.json'), 'utf-8'),
99100
) as { component: string; props: Record<string, unknown>; url: string };
100-
const aboutData = JSON.parse(
101+
const aboutData = deserializePageData(
101102
readFileSync(join(DIST, 'client/_void/pages/about.json'), 'utf-8'),
102103
) as { component: string; props: Record<string, unknown>; url: string };
103104

@@ -121,7 +122,7 @@ describe('playground/static-pages build (output: static)', () => {
121122
expect(html).not.toContain('data-testid="client-only-title"');
122123
expect(html).not.toContain('client-only static page component rendered on the server');
123124

124-
const pageData = JSON.parse(
125+
const pageData = deserializePageData(
125126
readFileSync(join(DIST, 'client/_void/pages/client-only.json'), 'utf-8'),
126127
) as { component: string; props: Record<string, unknown>; url: string };
127128
expect(pageData.component).toBe('client-only');

‎packages/vue/test/integration/md-island-frontmatter.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { expect, it } from 'vite-plus/test';
88
import { waitForIslandsHydrated } from '../../../../test/browser-helpers.ts';
99
import { openIslandPage, type IslandPageVisit } from '../../../../test/island-page-browser.ts';
1010
import { voidMarkdown } from '../../../md/src/plugin.ts';
11+
import { islandFrontmatterScript } from '../../../void/src/pages/serialize.ts';
1112
import { islandsPlugin } from '../../src/plugin-islands.ts';
1213

13-
const ISLAND_FRONTMATTER_SCRIPT =
14-
'<script type="application/json" id="__void_frontmatter">{"title":"Hello from frontmatter"}</script>';
14+
const ISLAND_FRONTMATTER_SCRIPT = islandFrontmatterScript({ title: 'Hello from frontmatter' });
1515

1616
it('an island reads the same frontmatter in the browser as on the server', async () => {
1717
const root = mkdtempSync(join(import.meta.dirname, '__tmp_md_island_frontmatter_'));

‎platform/packages/api/test/integration/admin-builds.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'vite-plus/test';
2+
import { deserializePageData } from 'void/pages-server';
23
import { updateBuildStatus } from '../../src/db.ts';
34
import { app } from '../../src/index.ts';
45
import { builds } from '../../src/schema.ts';
@@ -345,7 +346,11 @@ describe('GET /admin/builds/:id (build page)', () => {
345346
expect(html).toContain('cafebab');
346347
// The React log panel receives the build ID and starts with its loading state.
347348
expect(html).toContain('id="build-log"');
348-
expect(html).toContain('"component":"buildDetail"');
349+
expect(
350+
deserializePageData(
351+
html.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/)![1],
352+
),
353+
).toMatchObject({ component: 'buildDetail' });
349354
expect(html).toContain('Loading logs...');
350355
// No external GitHub link for a container build.
351356
expect(html).not.toContain('View logs on GitHub');

‎platform/packages/api/test/integration/admin-dashboard.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
afterEach,
1212
vi,
1313
} from 'vite-plus/test';
14+
import { deserializePageData } from 'void/pages-server';
1415
import { respondToProjectInvitation } from '../../src/db.ts';
1516
import type { ApiEnv } from '../../src/env.ts';
1617
import { app } from '../../src/index.ts';
@@ -684,7 +685,7 @@ function pageSnapshot(html: string): string {
684685
const match =
685686
/<script id="__VOID_PAGE_DATA__" type="application\/json">([\s\S]*?)<\/script>/.exec(html);
686687
expect(match).not.toBeNull();
687-
const page = JSON.parse(match![1]!) as { shared: { adminFateSnapshot?: unknown } };
688+
const page = deserializePageData(match![1]!) as { shared: { adminFateSnapshot?: unknown } };
688689
expect(page.shared.adminFateSnapshot).toBeDefined();
689690
return JSON.stringify(page.shared.adminFateSnapshot);
690691
}
@@ -3568,7 +3569,11 @@ describe('GET /admin/deployments/:id (logs section)', () => {
35683569
const html = await res.text();
35693570
expect(html).toContain('role="combobox"');
35703571
expect(html).toContain('placeholder="Filter logs"');
3571-
expect(html).toContain('"component":"deploymentDetail"');
3572+
expect(
3573+
deserializePageData(
3574+
html.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/)![1],
3575+
),
3576+
).toMatchObject({ component: 'deploymentDetail' });
35723577
expect(html).toContain('src="/admin/client.js"');
35733578
});
35743579

0 commit comments

Comments
 (0)