|
| 1 | +import type { Page } from '@playwright/test' |
1 | 2 | import { expect, test } from './test-utils' |
2 | 3 |
|
| 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 | + |
3 | 24 | 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' }) |
6 | 41 |
|
7 | | - expect(hydrationErrors).toEqual([]) |
| 42 | + expect(hydrationErrors).toEqual([]) |
| 43 | + }) |
| 44 | + } |
8 | 45 | }) |
9 | 46 |
|
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' }) |
12 | 55 |
|
13 | | - expect(hydrationErrors).toEqual([]) |
| 56 | + expect(hydrationErrors).toEqual([]) |
| 57 | + }) |
| 58 | + } |
14 | 59 | }) |
15 | 60 |
|
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' }) |
18 | 69 |
|
19 | | - expect(hydrationErrors).toEqual([]) |
| 70 | + expect(hydrationErrors).toEqual([]) |
| 71 | + }) |
| 72 | + } |
20 | 73 | }) |
21 | 74 |
|
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' }) |
24 | 83 |
|
25 | | - expect(hydrationErrors).toEqual([]) |
| 84 | + expect(hydrationErrors).toEqual([]) |
| 85 | + }) |
| 86 | + } |
26 | 87 | }) |
27 | 88 |
|
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' }) |
30 | 97 |
|
31 | | - expect(hydrationErrors).toEqual([]) |
| 98 | + expect(hydrationErrors).toEqual([]) |
| 99 | + }) |
| 100 | + } |
32 | 101 | }) |
33 | 102 |
|
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' }) |
36 | 111 |
|
37 | | - expect(hydrationErrors).toEqual([]) |
| 112 | + expect(hydrationErrors).toEqual([]) |
| 113 | + }) |
| 114 | + } |
38 | 115 | }) |
39 | 116 | }) |
| 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