-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathreact-router.test.ts
More file actions
187 lines (158 loc) · 5.67 KB
/
react-router.test.ts
File metadata and controls
187 lines (158 loc) · 5.67 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { createHash } from 'node:crypto'
import { expect, test } from '@playwright/test'
import { type Fixture, useFixture } from './fixture'
import { expectNoReload, testNoJs, waitForHydration } from './helper'
import { readFileSync } from 'node:fs'
import React from 'react'
test.describe('dev-default', () => {
test.skip(/canary|experimental/.test(React.version))
const f = useFixture({ root: 'examples/react-router', mode: 'dev' })
defineTest(f)
})
test.describe('build-default', () => {
const f = useFixture({ root: 'examples/react-router', mode: 'build' })
defineTest(f)
})
test.describe('dev-cloudflare', () => {
test.skip(/canary|experimental/.test(React.version))
const f = useFixture({
root: 'examples/react-router',
mode: 'dev',
command: 'pnpm cf-dev',
})
defineTest(f, 'cloudflare')
})
test.describe('build-cloudflare', () => {
const f = useFixture({
root: 'examples/react-router',
mode: 'build',
buildCommand: 'pnpm cf-build',
command: 'pnpm cf-preview',
})
defineTest(f, 'cloudflare')
})
function defineTest(f: Fixture, variant?: 'cloudflare') {
test('loader', async ({ page }) => {
await page.goto(f.url())
await expect(page.getByText(`loaderData: {"name":"Unknown"}`)).toBeVisible()
})
test('client', async ({ page }) => {
await page.goto(f.url('./about'))
await waitForHydration(page)
await page.getByRole('button', { name: 'Client counter: 0' }).click()
await expect(
page.getByRole('button', { name: 'Client counter: 1' }),
).toBeVisible()
})
test('navigation', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await using _ = await expectNoReload(page)
await page.getByText('This is the home page.').click()
await page.getByRole('link', { name: 'About' }).click()
await page.waitForURL(f.url('./about'))
await page.getByText('This is the about page.').click()
await page.getByRole('link', { name: 'Home' }).click()
await page.waitForURL(f.url())
await page.getByText('This is the home page.').click()
})
test.describe(() => {
test.skip(f.mode !== 'build')
testNoJs('ssr modulepreload', async ({ page }) => {
await page.goto(f.url())
const srcs = await page
.locator(`head >> link[rel="modulepreload"]`)
.evaluateAll((elements) =>
elements.map((el) => el.getAttribute('href')),
)
const manifest = JSON.parse(
readFileSync(
`${f.root}/${variant === 'cloudflare' ? 'dist/rsc/ssr' : 'dist/ssr'}/__vite_rsc_assets_manifest.js`,
'utf-8',
).slice('export default '.length),
)
const hashString = (v: string) =>
createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
const deps =
manifest.clientReferenceDeps[hashString('app/routes/home.client.tsx')]
expect(srcs).toEqual(expect.arrayContaining(deps.js))
})
})
test.describe(() => {
test.skip(f.mode !== 'dev')
test('client hmr', async ({ page }) => {
await page.goto(f.url('./about'))
await waitForHydration(page)
await using _ = await expectNoReload(page)
await page.getByRole('button', { name: 'Client counter: 0' }).click()
await expect(
page.getByRole('button', { name: 'Client counter: 1' }),
).toBeVisible()
const editor = f.createEditor('app/routes/about.tsx')
editor.edit((s) => s.replace('Client counter:', 'Client [edit] counter:'))
await expect(
page.getByRole('button', { name: 'Client [edit] counter: 1' }),
).toBeVisible()
})
test('server hmr', async ({ page }) => {
await page.goto(f.url('/'))
await waitForHydration(page)
await using _ = await expectNoReload(page)
await page.getByText('This is the home page.').click()
const editor = f.createEditor('app/routes/home.tsx')
editor.edit((s) =>
s.replace('This is the home page.', 'This is the home [edit] page.'),
)
await page.getByText('This is the home [edit] page.').click()
})
})
test('server css code split', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await expect(page.locator('.test-style-home')).toHaveCSS(
'color',
'rgb(250, 150, 0)',
)
// client side navigation to "/about" keeps "/" styles
await page.getByRole('link', { name: 'About' }).click()
await page.waitForURL(f.url('./about'))
await expect(page.locator('.test-style-home')).toHaveCSS(
'color',
'rgb(250, 150, 0)',
)
// SSR of "/about" doesn't include "/" styles
await page.goto(f.url('./about'))
await waitForHydration(page)
await expect(page.locator('.test-style-home')).not.toHaveCSS(
'color',
'rgb(250, 150, 0)',
)
// client side navigation to "/" loads "/" styles
await page.getByRole('link', { name: 'Home' }).click()
await page.waitForURL(f.url())
await expect(page.locator('.test-style-home')).toHaveCSS(
'color',
'rgb(250, 150, 0)',
)
})
test('vite-rsc-css-export', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await expect(page.getByTestId('root-style')).toHaveCSS(
'color',
'rgb(0, 0, 255)',
)
})
test('useActionState', async ({ page }) => {
await page.goto(f.url())
await waitForHydration(page)
await page.getByTestId('use-action-state-jsx').getByRole('button').click()
await expect(page.getByTestId('use-action-state-jsx')).toContainText(
/\(ok\)/,
)
await page.getByTestId('use-action-state-jsx').getByRole('button').click()
await expect(page.getByTestId('use-action-state-jsx')).toContainText(
/\(ok\).*\(ok\)/,
)
})
}