Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

not-found.spec.ts

101 lines3.2 KB

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(`global not found`, async ({ page }) => {
16 const response = await page.goto(`/this-page-does-not-exist/foo/bar`)
17​
18 expect(response?.status()).toBe(isSpaMode ? 200 : 404)
19​
20 await expect(
21 page.getByTestId('default-not-found-component'),
22 ).toBeInViewport()
23 })
24​
25 test.describe('throw notFound()', () => {
26 const navigationTestMatrix = combinate({
27 thrower: ['beforeLoad', 'loader'] as const,
28 preload: [false, true] as const,
29 })
30​
31 navigationTestMatrix.forEach(({ thrower, preload }) => {
32 test(`navigation: thrower: ${thrower}, preload: ${preload}`, async ({
33 page,
34 }) => {
35 await page.goto(
36 `/not-found/${preload === false ? '?preload=false' : ''}`,
37 )
38 const link = page.getByTestId(`via-${thrower}`)
39​
40 if (preload) {
41 await link.focus()
42 await new Promise((r) => setTimeout(r, 250))
43 }
44​
45 await link.click()
46​
47 await expect(
48 page.getByTestId(`via-${thrower}-notFound-component`),
49 ).toBeInViewport()
50 await expect(
51 page.getByTestId(`via-${thrower}-route-component`),
52 ).not.toBeInViewport()
53 })
54 })
55 const directVisitTestMatrix = combinate({
56 thrower: ['beforeLoad', 'loader'] as const,
57 })
58​
59 directVisitTestMatrix.forEach(({ thrower }) => {
60 test(`direct visit: thrower: ${thrower}`, async ({ page }) => {
61 await page.goto(`/not-found/via-${thrower}`)
62 await page.waitForLoadState('networkidle')
63 await expect(
64 page.getByTestId(`via-${thrower}-notFound-component`),
65 ).toBeInViewport()
66 await expect(
67 page.getByTestId(`via-${thrower}-route-component`),
68 ).not.toBeInViewport()
69 })
70 })
71​
72 test('direct visit: child beforeLoad notFound renders parent boundary with parent loader data', async ({
73 page,
74 }) => {
75 await page.goto('/not-found/parent-boundary/via-beforeLoad')
76 await page.waitForLoadState('networkidle')
77​
78 await expect(
79 page.getByTestId('parent-boundary-notFound-component'),
80 ).toBeInViewport()
81 await expect(page.getByTestId('parent-loader-data')).toHaveText('ready')
82 await expect(
83 page.getByTestId('parent-boundary-child-route-component'),
84 ).not.toBeInViewport()
85 })
86​
87 test('direct visit: beforeLoad notFound with routeId targets root boundary', async ({
88 page,
89 }) => {
90 await page.goto('/not-found/via-beforeLoad-target-root')
91 await page.waitForLoadState('networkidle')
92​
93 await expect(
94 page.getByTestId('default-not-found-component'),
95 ).toBeInViewport()
96 await expect(
97 page.getByTestId('via-beforeLoad-target-root-route-component'),
98 ).not.toBeInViewport()
99 })
100 })
101})