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

relative.spec.ts

272 lines8.4 KB

1import { expect } from '@playwright/test'
2import { apiTest as test } from '@tanstack/router-e2e-utils'
3import combinateImport from 'combinate'
4​
5// somehow playwright does not correctly import default exports
6const combinate = (combinateImport as any).default as typeof combinateImport
7​
8const PORT = Number(process.env.E2E_APP_PORT ?? 0)
9​
10test.describe('relative_routing', () => {
11 const internalNavigationTestMatrix = combinate({
12 navigation: ['link', 'useNavigate'] as const,
13 })
14​
15 internalNavigationTestMatrix.forEach(({ navigation }) => {
16 test(`simple relative navigation. navigation: ${navigation}`, async ({
17 page,
18 }) => {
19 await page.waitForLoadState('networkidle')
20 await page.goto(`/relative/${navigation}`)
21 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
22​
23 await expect(
24 page.getByTestId(`relative-${navigation}-header`),
25 ).toBeInViewport()
26​
27 const indexLink = page.getByTestId(`relative-${navigation}-index`)
28 const backLink = page.getByTestId(`relative-${navigation}-back`)
29 const relativeRouteA = page.getByTestId(`relative-${navigation}-a`)
30 const relativeRouteB = page.getByTestId(`relative-${navigation}-b`)
31​
32 await relativeRouteA.click()
33​
34 await page.waitForURL(
35 `http://localhost:${PORT}/relative/${navigation}/relative-${navigation}-a`,
36 )
37​
38 await expect(
39 page.getByTestId(`relative-${navigation}-header`),
40 ).toBeInViewport()
41​
42 await expect(
43 page.getByTestId(`relative-${navigation}-a-header`),
44 ).toBeInViewport()
45​
46 await relativeRouteB.click()
47​
48 await page.waitForURL(
49 `http://localhost:${PORT}/relative/${navigation}/relative-${navigation}-b`,
50 )
51​
52 await expect(
53 page.getByTestId(`relative-${navigation}-header`),
54 ).toBeInViewport()
55​
56 await expect(
57 page.getByTestId(`relative-${navigation}-b-header`),
58 ).toBeInViewport()
59​
60 await indexLink.click()
61​
62 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
63​
64 await expect(
65 page.getByTestId(`relative-${navigation}-header`),
66 ).toBeInViewport()
67​
68 await expect(
69 page.getByTestId(`relative-${navigation}-b-header`),
70 ).not.toBeInViewport()
71​
72 await backLink.click()
73​
74 await page.waitForURL(`http://localhost:${PORT}/relative`)
75​
76 await expect(page.getByTestId(`relative-routing-home`)).toBeInViewport()
77 })
78​
79 test(`nested chilren. navigation: ${navigation}`, async ({ page }) => {
80 await page.waitForLoadState('networkidle')
81 await page.goto(`/relative/${navigation}`)
82 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
83​
84 await expect(
85 page.getByTestId(`relative-${navigation}-header`),
86 ).toBeInViewport()
87​
88 const backLink = page.getByTestId(`relative-${navigation}-back`)
89​
90 const deeplyNestedChildRoute = page.getByTestId(
91 `relative-${navigation}-deeply-nested`,
92 )
93​
94 await deeplyNestedChildRoute.click()
95​
96 await page.waitForURL(
97 `http://localhost:${PORT}/relative/${navigation}/nested/deep`,
98 )
99​
100 await expect(
101 page.getByTestId(`relative-${navigation}-nested-deep-header`),
102 ).toBeInViewport()
103​
104 await backLink.click()
105​
106 await page.waitForURL(
107 `http://localhost:${PORT}/relative/${navigation}/nested`,
108 )
109​
110 await expect(
111 page.getByTestId(`relative-${navigation}-nested-header`),
112 ).toBeInViewport()
113 })
114​
115 test(`with path params. navigation: ${navigation}`, async ({ page }) => {
116 await page.waitForLoadState('networkidle')
117 await page.goto(`/relative/${navigation}`)
118 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
119​
120 await expect(
121 page.getByTestId(`relative-${navigation}-header`),
122 ).toBeInViewport()
123​
124 const pathParamRoute = page.getByTestId(`relative-${navigation}-path`)
125​
126 await pathParamRoute.click()
127​
128 await page.waitForURL(
129 `http://localhost:${PORT}/relative/${navigation}/path/a`,
130 )
131​
132 expect(page.url().endsWith('/path/a')).toBe(true)
133​
134 await expect(
135 page.getByTestId(`relative-${navigation}-path-param-header`),
136 ).toBeInViewport()
137​
138 const switchParamLink = page.getByTestId(
139 `relative-${navigation}-path-param-switchAB`,
140 )
141​
142 await switchParamLink.click()
143​
144 await page.waitForURL(
145 `http://localhost:${PORT}/relative/${navigation}/path/b`,
146 )
147​
148 expect(page.url().endsWith('/path/b')).toBe(true)
149​
150 await expect(
151 page.getByTestId(`relative-${navigation}-path-param-header`),
152 ).toBeInViewport()
153 })
154​
155 test(`with search params. navigation: ${navigation}`, async ({ page }) => {
156 await page.waitForLoadState('networkidle')
157 await page.goto(`/relative/${navigation}`)
158 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
159​
160 await expect(
161 page.getByTestId(`relative-${navigation}-header`),
162 ).toBeInViewport()
163​
164 const searchParamRoute = page.getByTestId(
165 `relative-${navigation}-withSearch`,
166 )
167​
168 await searchParamRoute.click()
169​
170 let expectedUrl = new URL(
171 `http://localhost:${PORT}/relative/${navigation}/with-search?searchParam="1"`,
172 )
173​
174 await page.waitForLoadState('domcontentloaded')
175​
176 expect(page.url().toString()).toBe(expectedUrl.toString())
177​
178 await expect(
179 page.getByTestId(`relative-${navigation}-withSearch-header`),
180 ).toBeInViewport()
181​
182 await expect(
183 page.getByTestId(`relative-${navigation}-withSearch-header`),
184 ).toContainText('searchParam: 1')
185​
186 const updateSearchLink = page.getByTestId(
187 `relative-${navigation}-withSearch-update-param`,
188 )
189​
190 await updateSearchLink.click()
191​
192 await page.waitForLoadState('domcontentloaded')
193​
194 expectedUrl = new URL(
195 `http://localhost:${PORT}/relative/${navigation}/with-search?searchParam="2"`,
196 )
197​
198 expect(page.url().toString()).toBe(expectedUrl.toString())
199​
200 await expect(
201 page.getByTestId(`relative-${navigation}-withSearch-header`),
202 ).toContainText('searchParam: 2')
203​
204 await expect(
205 page.getByTestId(`relative-${navigation}-withSearch-header`),
206 ).toBeInViewport()
207 })
208​
209 test(`shouldn't cause excessive rendering. navigation: ${navigation}`, async ({
210 page,
211 }) => {
212 let navigateMsgs = 0
213​
214 page.on('console', (msg) => {
215 if (msg.text() === 'navigate') {
216 navigateMsgs++
217 }
218 })
219​
220 await page.waitForLoadState('networkidle')
221 await page.goto(`/relative/${navigation}`)
222 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
223​
224 await expect(
225 page.getByTestId(`relative-${navigation}-header`),
226 ).toBeInViewport()
227​
228 expect(navigateMsgs).toBe(1)
229 const indexLink = page.getByTestId(`relative-${navigation}-index`)
230 const backLink = page.getByTestId(`relative-${navigation}-back`)
231 const relativeRouteA = page.getByTestId(`relative-${navigation}-a`)
232 const relativeRouteB = page.getByTestId(`relative-${navigation}-b`)
233 const deeplyNestedChildRoute = page.getByTestId(
234 `relative-${navigation}-deeply-nested`,
235 )
236 const pathParamRoute = page.getByTestId(`relative-${navigation}-path`)
237 const searchParamRoute = page.getByTestId(
238 `relative-${navigation}-withSearch`,
239 )
240​
241 await relativeRouteA.click()
242 await page.waitForLoadState('domcontentloaded')
243 await relativeRouteB.click()
244 await page.waitForLoadState('domcontentloaded')
245 await deeplyNestedChildRoute.click()
246 await page.waitForLoadState('domcontentloaded')
247 await backLink.click()
248 await page.waitForLoadState('domcontentloaded')
249 await pathParamRoute.click()
250 await page.waitForLoadState('domcontentloaded')
251 const switchParamLink = page.getByTestId(
252 `relative-${navigation}-path-param-switchAB`,
253 )
254 await switchParamLink.click()
255 await page.waitForLoadState('domcontentloaded')
256 await searchParamRoute.click()
257 await page.waitForLoadState('domcontentloaded')
258​
259 const updateSearchLink = page.getByTestId(
260 `relative-${navigation}-withSearch-update-param`,
261 )
262​
263 await updateSearchLink.click()
264 await page.waitForLoadState('domcontentloaded')
265​
266 await indexLink.click()
267 await page.waitForLoadState('domcontentloaded')
268​
269 expect(navigateMsgs).toBe(1)
270 })
271 })
272})