Skip to content

Commit 03fbdf9

Browse files
committed
test: add hydration matrix
1 parent d2a6af0 commit 03fbdf9

File tree

1 file changed

+103
-18
lines changed

1 file changed

+103
-18
lines changed

test/e2e/hydration.spec.ts

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,124 @@
1+
import type { Page } from '@playwright/test'
12
import { expect, test } from './test-utils'
23

4+
const PAGES = [
5+
'/',
6+
'/about',
7+
'/settings',
8+
'/privacy',
9+
'/compare',
10+
'/search',
11+
'/package/nuxt',
12+
'/search?q=vue',
13+
] as const
14+
15+
// ---------------------------------------------------------------------------
16+
// Test matrix
17+
//
18+
// For each user setting, we test two states across all pages:
19+
// 1. undefined — empty localStorage, the default/fresh-install experience
20+
// 2. a non-default value — verifies hydration still works when the user has
21+
// changed that setting from its default
22+
// ---------------------------------------------------------------------------
23+
324
test.describe('Hydration', () => {
4-
test('/ (homepage) has no hydration mismatches', async ({ goto, hydrationErrors }) => {
5-
await goto('/', { waitUntil: 'hydration' })
25+
test.describe('no user settings (empty localStorage)', () => {
26+
for (const page of PAGES) {
27+
test(`${page}`, async ({ goto, hydrationErrors }) => {
28+
await goto(page, { waitUntil: 'hydration' })
29+
30+
expect(hydrationErrors).toEqual([])
31+
})
32+
}
33+
})
34+
35+
// Default: "system" → test explicit "dark"
36+
test.describe('color mode: dark', () => {
37+
for (const page of PAGES) {
38+
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
39+
await injectLocalStorage(pw, { 'npmx-color-mode': 'dark' })
40+
await goto(page, { waitUntil: 'hydration' })
641

7-
expect(hydrationErrors).toEqual([])
42+
expect(hydrationErrors).toEqual([])
43+
})
44+
}
845
})
946

10-
test('/about has no hydration mismatches', async ({ goto, hydrationErrors }) => {
11-
await goto('/about', { waitUntil: 'hydration' })
47+
// Default: null → test "violet"
48+
test.describe('accent color: violet', () => {
49+
for (const page of PAGES) {
50+
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
51+
await injectLocalStorage(pw, {
52+
'npmx-settings': JSON.stringify({ accentColorId: 'violet' }),
53+
})
54+
await goto(page, { waitUntil: 'hydration' })
1255

13-
expect(hydrationErrors).toEqual([])
56+
expect(hydrationErrors).toEqual([])
57+
})
58+
}
1459
})
1560

16-
test('/settings has no hydration mismatches', async ({ goto, hydrationErrors }) => {
17-
await goto('/settings', { waitUntil: 'hydration' })
61+
// Default: null → test "slate"
62+
test.describe('background theme: slate', () => {
63+
for (const page of PAGES) {
64+
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
65+
await injectLocalStorage(pw, {
66+
'npmx-settings': JSON.stringify({ preferredBackgroundTheme: 'slate' }),
67+
})
68+
await goto(page, { waitUntil: 'hydration' })
1869

19-
expect(hydrationErrors).toEqual([])
70+
expect(hydrationErrors).toEqual([])
71+
})
72+
}
2073
})
2174

22-
test('/privacy has no hydration mismatches', async ({ goto, hydrationErrors }) => {
23-
await goto('/privacy', { waitUntil: 'hydration' })
75+
// Default: "npm" → test "pnpm"
76+
test.describe('package manager: pnpm', () => {
77+
for (const page of PAGES) {
78+
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
79+
await injectLocalStorage(pw, {
80+
'npmx-pm': JSON.stringify('pnpm'),
81+
})
82+
await goto(page, { waitUntil: 'hydration' })
2483

25-
expect(hydrationErrors).toEqual([])
84+
expect(hydrationErrors).toEqual([])
85+
})
86+
}
2687
})
2788

28-
test('/compare has no hydration mismatches', async ({ goto, hydrationErrors }) => {
29-
await goto('/compare', { waitUntil: 'hydration' })
89+
// Default: "en-US" (LTR) → test "ar-EG" (RTL)
90+
test.describe('locale: ar-EG (RTL)', () => {
91+
for (const page of PAGES) {
92+
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
93+
await injectLocalStorage(pw, {
94+
'npmx-settings': JSON.stringify({ selectedLocale: 'ar-EG' }),
95+
})
96+
await goto(page, { waitUntil: 'hydration' })
3097

31-
expect(hydrationErrors).toEqual([])
98+
expect(hydrationErrors).toEqual([])
99+
})
100+
}
32101
})
33102

34-
test('/packages/nuxt has no hydration mismatches', async ({ goto, hydrationErrors }) => {
35-
await goto('/packages/nuxt', { waitUntil: 'hydration' })
103+
// Default: false → test true
104+
test.describe('relative dates: enabled', () => {
105+
for (const page of PAGES) {
106+
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
107+
await injectLocalStorage(pw, {
108+
'npmx-settings': JSON.stringify({ relativeDates: true }),
109+
})
110+
await goto(page, { waitUntil: 'hydration' })
36111

37-
expect(hydrationErrors).toEqual([])
112+
expect(hydrationErrors).toEqual([])
113+
})
114+
}
38115
})
39116
})
117+
118+
async function injectLocalStorage(page: Page, entries: Record<string, string>) {
119+
await page.addInitScript((e: Record<string, string>) => {
120+
for (const [key, value] of Object.entries(e)) {
121+
localStorage.setItem(key, value)
122+
}
123+
}, entries)
124+
}

0 commit comments

Comments
 (0)