Open History to see the last commit
1import { expect } from '@playwright/test'
2import combinateImport from 'combinate'
3import { test } from '@tanstack/router-e2e-utils'
4import { isSpaMode } from './utils/isSpaMode'
5β
6// somehow playwright does not correctly import default exports
7const combinate = (combinateImport as any).default as typeof combinateImport
8β
9test.use({
10 whitelistErrors: [
11 'Failed to load resource: the server responded with a status of 404',
12 ],
13})
14test.describe('not-found', () => {
15 test('direct nested not-found hydrates its already-rendered parent route', async ({
16 page,
17 }) => {
18 test.skip(isSpaMode, 'issue #5106 requires a server-rendered direct visit')
19β
20 const response = await page.goto('/posts/i-do-not-exist')
21 await page.waitForLoadState('networkidle')
22β
23 const serverHtml = await response?.text()
24 expect(serverHtml).toContain('posts-parent-hydration-counter')
25 expect(serverHtml).toContain('Post not found')
26 await expect(page.getByText('Post not found')).toBeInViewport()
27 const counter = page.getByTestId('posts-parent-hydration-counter')
28 await expect(counter).toHaveText('Parent hydration count: 0')
29 await counter.click()
30 await expect(counter).toHaveText('Parent hydration count: 1')
31 })
32β
33 test(`global not found`, async ({ page }) => {
34 const response = await page.goto(`/this-page-does-not-exist/foo/bar`)
35β
36 expect(response?.status()).toBe(isSpaMode ? 200 : 404)
37β
38 await expect(
39 page.getByTestId('default-not-found-component'),
40 ).toBeInViewport()
41 })
42β
43 test.describe('throw notFound()', () => {
44 const navigationTestMatrix = combinate({
45 thrower: ['beforeLoad', 'loader'] as const,
46 preload: [false, true] as const,
47 })
48β
49 navigationTestMatrix.forEach(({ thrower, preload }) => {
50 test(`navigation: thrower: ${thrower}, preload: ${preload}`, async ({
51 page,
52 }) => {
53 await page.goto(
54 `/not-found/${preload === false ? '?preload=false' : ''}`,
55 )
56 const link = page.getByTestId(`via-${thrower}`)
57β
58 if (preload) {
59 await link.focus()
60 await new Promise((r) => setTimeout(r, 250))
61 }
62β
63 await link.click()
64β
65 await expect(
66 page.getByTestId(`via-${thrower}-notFound-component`),
67 ).toBeInViewport()
68β
69 await expect(
70 page.getByTestId(`via-${thrower}-route-component`),
71 ).not.toBeInViewport()
72 })
73 })
74 const directVisitTestMatrix = combinate({
75 thrower: ['beforeLoad', 'loader'] as const,
76 })
77β
78 directVisitTestMatrix.forEach(({ thrower }) => {
79 test(`direct visit: thrower: ${thrower}`, async ({ page }) => {
80 await page.goto(`/not-found/via-${thrower}`)
81 await page.waitForLoadState('networkidle')
82 await expect(
83 page.getByTestId(`via-${thrower}-notFound-component`),
84 ).toBeInViewport()
85 await expect(
86 page.getByTestId(`via-${thrower}-route-component`),
87 ).not.toBeInViewport()
88 })
89 })
90β
91 test('direct visit: child beforeLoad notFound with routeId renders parent boundary with parent loader data', async ({
92 page,
93 }) => {
94 await page.goto('/not-found/parent-boundary/via-beforeLoad')
95 await page.waitForLoadState('networkidle')
96β
97 await expect(
98 page.getByTestId('parent-boundary-notFound-component'),
99 ).toBeInViewport()
100 await expect(page.getByTestId('parent-loader-data')).toHaveText('ready')
101 await expect(
102 page.getByTestId('parent-boundary-notFound-source'),
103 ).toHaveText('with-routeId')
104 await expect(
105 page.getByTestId('parent-boundary-child-route-component'),
106 ).not.toBeInViewport()
107 })
108β
109 test('direct visit: child beforeLoad notFound without routeId still hydrates and renders parent boundary', async ({
110 page,
111 }) => {
112 await page.goto('/not-found/parent-boundary/via-beforeLoad?target=none')
113 await page.waitForLoadState('networkidle')
114β
115 await expect(
116 page.getByTestId('parent-boundary-notFound-component'),
117 ).toBeInViewport()
118 await expect(page.getByTestId('parent-loader-data')).toHaveText('ready')
119 await expect(
120 page.getByTestId('parent-boundary-notFound-source'),
121 ).toHaveText('without-routeId')
122 await expect(
123 page.getByTestId('parent-boundary-child-route-component'),
124 ).not.toBeInViewport()
125 })
126β
127 test('direct visit: beforeLoad notFound with routeId targets root boundary', async ({
128 page,
129 }) => {
130 await page.goto('/not-found/via-beforeLoad-target-root')
131 await page.waitForLoadState('networkidle')
132β
133 await expect(
134 page.getByTestId('default-not-found-component'),
135 ).toBeInViewport()
136 await expect(
137 page.getByTestId('via-beforeLoad-target-root-route-component'),
138 ).not.toBeInViewport()
139 })
140β
141 test('direct visit: deep hierarchy d throws in beforeLoad and hydrates route d boundary', async ({
142 page,
143 }) => {
144 await page.goto('/not-found/deep/b/c/d?throwAt=d')
145 await page.waitForLoadState('networkidle')
146β
147 await expect(
148 page.getByTestId('deep-d-notFound-component'),
149 ).toBeInViewport()
150 await expect(
151 page.getByTestId('deep-d-route-component'),
152 ).not.toBeInViewport()
153 })
154β
155 test('direct visit: deep hierarchy c throws in beforeLoad, d loader does not run, and c boundary hydrates', async ({
156 page,
157 }) => {
158 await page.goto('/not-found/deep/b/c/d?throwAt=c')
159 await page.waitForLoadState('networkidle')
160β
161 await expect(
162 page.getByTestId('deep-c-notFound-component'),
163 ).toBeInViewport()
164 await expect(
165 page.getByTestId('deep-d-route-component'),
166 ).not.toBeInViewport()
167 })
168β
169 test('direct visit: deep hierarchy b throws in beforeLoad and b boundary hydrates', async ({
170 page,
171 }) => {
172 await page.goto('/not-found/deep/b/c/d?throwAt=b')
173 await page.waitForLoadState('networkidle')
174β
175 await expect(
176 page.getByTestId('deep-b-notFound-component'),
177 ).toBeInViewport()
178 await expect(
179 page.getByTestId('deep-d-route-component'),
180 ).not.toBeInViewport()
181 })
182 })
183})