-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathhydration.spec.ts
More file actions
141 lines (121 loc) · 4.22 KB
/
Copy pathhydration.spec.ts
File metadata and controls
141 lines (121 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { Page } from '@playwright/test'
import { expect, test } from './test-utils'
const PAGES = [
'/',
'/about',
'/settings',
'/privacy',
'/compare',
'/search',
'/package/nuxt',
'/search?q=vue',
'/~qwerzl',
] as const
const SEARCH_PROVIDER_PAGES = ['/search?q=vue', '/~qwerzl'] as const
// ---------------------------------------------------------------------------
// Test matrix
//
// For each user setting, we test two states across all pages:
// 1. undefined — empty localStorage, the default/fresh-install experience
// 2. a non-default value — verifies hydration still works when the user has
// changed that setting from its default
// ---------------------------------------------------------------------------
test.describe('Hydration', () => {
test.describe('no user settings (empty localStorage)', () => {
for (const page of PAGES) {
test(`${page}`, async ({ goto, hydrationErrors }) => {
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: "system" → test explicit "dark"
test.describe('color mode: dark', () => {
for (const page of PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, { 'npmx-color-mode': 'dark' })
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: null → test "violet"
test.describe('accent color: violet', () => {
for (const page of PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, {
'npmx-settings': JSON.stringify({ accentColorId: 'violet' }),
})
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: null → test "slate"
test.describe('background theme: slate', () => {
for (const page of PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, {
'npmx-settings': JSON.stringify({ preferredBackgroundTheme: 'slate' }),
})
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: "npm" → test "pnpm"
test.describe('package manager: pnpm', () => {
for (const page of PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, {
'npmx-pm': JSON.stringify('pnpm'),
})
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: "en-US" (LTR) → test "ar-EG" (RTL)
test.describe('locale: ar-EG (RTL)', () => {
for (const page of PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, {
'npmx-settings': JSON.stringify({ selectedLocale: 'ar-EG' }),
})
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: false → test true
test.describe('relative dates: enabled', () => {
for (const page of PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, {
'npmx-settings': JSON.stringify({ relativeDates: true }),
})
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
// Default: "algolia" in production -> test persisted "npm"
test.describe('search provider: npm', () => {
for (const page of SEARCH_PROVIDER_PAGES) {
test(`${page}`, async ({ page: pw, goto, hydrationErrors }) => {
await injectLocalStorage(pw, {
'npmx-settings': JSON.stringify({ searchProvider: 'npm' }),
})
await goto(page, { waitUntil: 'hydration' })
expect(hydrationErrors).toEqual([])
})
}
})
})
async function injectLocalStorage(page: Page, entries: Record<string, string>) {
await page.addInitScript((e: Record<string, string>) => {
for (const [key, value] of Object.entries(e)) {
localStorage.setItem(key, value)
}
}, entries)
}