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

non-nested-paths.spec.ts

366 lines12.4 KB

1import { expect, test } from '@playwright/test'
2import { useExperimentalNonNestedRoutes } from './utils/useExperimentalNonNestedRoutes'
3​
4const testCases: Array<{
5 name: string
6 testPathDesc: string
7 testPathPrefix: string
8 testPathSuffix: string
9 paramValue: Record<string, string>
10 paramValue2: Record<string, string>
11}> = [
12 {
13 name: 'Named path params',
14 testPathDesc: 'named',
15 testPathPrefix: '',
16 testPathSuffix: '',
17 paramValue: { baz: 'baz' },
18 paramValue2: { baz: 'baz_' },
19 },
20 {
21 name: 'prefix params',
22 testPathPrefix: 'prefix',
23 testPathSuffix: '',
24 testPathDesc: 'prefix',
25 paramValue: { baz: 'baz' },
26 paramValue2: { baz: 'baz_' },
27 },
28 {
29 name: 'suffix params',
30 testPathPrefix: '',
31 testPathSuffix: 'suffix',
32 testPathDesc: 'suffix',
33 paramValue: { baz: 'baz' },
34 paramValue2: { baz: 'baz2' },
35 },
36 {
37 name: 'path',
38 testPathPrefix: '',
39 testPathSuffix: '',
40 testPathDesc: 'path',
41 paramValue: {},
42 paramValue2: {},
43 },
45​
46test.describe('Non-nested paths', () => {
47 testCases.forEach(
48 ({
49 name,
50 testPathDesc,
51 testPathPrefix,
52 testPathSuffix,
53 paramValue,
54 paramValue2,
55 }) => {
56 test.describe(name, () => {
57 const path = `/non-nested/${testPathDesc}`
58 const paramNameDesc = Object.keys(paramValue)[0] ?? 'baz'
59​
60 test.beforeEach(async ({ page }) => {
61 await page.goto('/non-nested')
62 await page.waitForURL('/non-nested')
63 })
64​
65 test('Should not un-nest nested paths', async ({ page }) => {
66 const nonNestedPathHeading = page.getByTestId(
67 'non-nested-path-heading',
68 )
69 await expect(nonNestedPathHeading).toBeVisible()
70​
71 await page.getByTestId(`l-to-${testPathDesc}`).click()
72 await page.waitForURL(path)
73​
74 const rootRouteHeading = page.getByTestId(
75 `non-nested-${testPathDesc}-root-route-heading`,
76 )
77​
78 await expect(rootRouteHeading).toBeVisible()
79​
80 const pathRouteHeading = page.getByTestId(
81 `non-nested-${testPathDesc}-${paramNameDesc}-route-heading`,
82 )
83​
84 const indexHeading = page.getByTestId(
85 `non-nested-${testPathDesc}-${paramNameDesc}-index-heading`,
86 )
87​
88 const fooHeading = page.getByTestId(
89 `non-nested-${testPathDesc}-${paramNameDesc}-foo-heading`,
90 )
91​
92 const indexParams = page.getByTestId(
93 `non-nested-${testPathDesc}-${paramNameDesc}-index-param`,
94 )
95​
96 const fooParams = page.getByTestId(
97 `non-nested-${testPathDesc}-${paramNameDesc}-foo-param`,
98 )
99​
100 const indexLink = page.getByTestId(`to-${testPathDesc}-index`)
101 const fooLink = page.getByTestId(`to-${testPathDesc}-foo`)
102 const foo2Link = page.getByTestId(`to-${testPathDesc}-foo-2`)
103​
104 const indexPath = `${path}/${testPathPrefix}${paramValue[paramNameDesc] ?? 'baz'}${testPathSuffix}`
105 const fooPath = `${path}/${testPathPrefix}${paramValue[paramNameDesc] ?? 'baz'}${testPathSuffix}/foo`
106 const foo2Path = `${path}/${testPathPrefix}${paramValue2[paramNameDesc] ?? 'baz'}${testPathSuffix}/foo`
107​
108 console.log(await indexLink.getAttribute('href'))
109 await expect(indexLink).toHaveAttribute('href', indexPath)
110 await expect(fooLink).toHaveAttribute('href', fooPath)
111 await expect(foo2Link).toHaveAttribute('href', foo2Path)
112​
113 await indexLink.click()
114 await page.waitForURL(indexPath)
115 await expect(rootRouteHeading).toBeVisible()
116 await expect(pathRouteHeading).toBeVisible()
117 await expect(indexHeading).toBeVisible()
118 const indexParamValue = await indexParams.innerText()
119 expect(JSON.parse(indexParamValue)).toEqual(paramValue)
120​
121 await fooLink.click()
122 await page.waitForURL(fooPath)
123 await expect(rootRouteHeading).toBeVisible()
124 await expect(pathRouteHeading).toBeVisible()
125 await expect(fooHeading).toBeVisible()
126 const fooParamValue = await fooParams.innerText()
127 expect(JSON.parse(fooParamValue)).toEqual(paramValue)
128​
129 await foo2Link.click()
130 await page.waitForURL(foo2Path)
131 await expect(rootRouteHeading).toBeVisible()
132 await expect(pathRouteHeading).toBeVisible()
133 await expect(fooHeading).toBeVisible()
134 const foo2ParamValue = await fooParams.innerText()
135 expect(JSON.parse(foo2ParamValue)).toEqual(paramValue2)
136 })
137​
138 test('Should not nest non-nested paths', async ({ page }) => {
139 const nonNestedPathHeading = page.getByTestId(
140 'non-nested-path-heading',
141 )
142 await expect(nonNestedPathHeading).toBeVisible()
143​
144 await page.getByTestId(`l-to-${testPathDesc}`).click()
145 await page.waitForURL(path)
146​
147 const rootRouteHeading = page.getByTestId(
148 `non-nested-${testPathDesc}-root-route-heading`,
149 )
150​
151 await expect(rootRouteHeading).toBeVisible()
152​
153 const pathRouteHeading = page.getByTestId(
154 `non-nested-${testPathDesc}-${paramNameDesc}-route-heading`,
155 )
156​
157 const barHeading = page.getByTestId(
158 `non-nested-${testPathDesc}-${paramNameDesc}-bar-heading`,
159 )
160​
161 const barParams = page.getByTestId(
162 `non-nested-${testPathDesc}-${paramNameDesc}-bar-param`,
163 )
164​
165 const barLink = page.getByTestId(`to-${testPathDesc}-bar`)
166 const bar2Link = page.getByTestId(`to-${testPathDesc}-bar-2`)
167​
168 const barPath = `${path}/${testPathPrefix}${paramValue[paramNameDesc] ?? 'baz'}${testPathSuffix}/bar`
169 const bar2Path = `${path}/${testPathPrefix}${paramValue2[paramNameDesc] ?? 'baz'}${testPathSuffix}/bar`
170 await expect(barLink).toHaveAttribute('href', barPath)
171​
172 await expect(bar2Link).toHaveAttribute('href', bar2Path)
173​
174 await barLink.click()
175 await page.waitForURL(barPath)
176 await expect(rootRouteHeading).toBeVisible()
177 await expect(pathRouteHeading).not.toBeVisible()
178 await expect(barHeading).toBeVisible()
179 const barParamValue = await barParams.innerText()
180 expect(JSON.parse(barParamValue)).toEqual(paramValue)
181​
182 await bar2Link.click()
183 await page.waitForURL(bar2Path)
184 await expect(rootRouteHeading).toBeVisible()
185 await expect(pathRouteHeading).not.toBeVisible()
186 await expect(barHeading).toBeVisible()
187 const bar2ParamValue = await barParams.innerText()
188 if (useExperimentalNonNestedRoutes || testPathDesc !== 'named') {
189 expect(JSON.parse(bar2ParamValue)).toEqual(paramValue2)
190 } else {
191 // this is a bug with named path params and non-nested paths
192 // that is resolved in the new experimental flag
193 expect(JSON.parse(bar2ParamValue)).toEqual(paramValue)
194 }
195 })
196 })
197 },
198 )
199})
200​
201test.describe('Deeply nested non-nested paths', () => {
202 test.beforeEach(async ({ page }) => {
203 await page.goto('/non-nested/deep')
204 await page.waitForURL('/non-nested/deep')
205 })
206​
207 test('It should nest nested paths 1 level deep', async ({ page }) => {
208 const rootRouteHeading = page.getByTestId(
209 `non-nested-deep-root-route-heading`,
210 )
211​
212 await expect(rootRouteHeading).toBeVisible()
213​
214 const bazLink = page.getByTestId('to-deep-baz')
215 await bazLink.click()
216 await page.waitForURL('/non-nested/deep/baz')
217 const bazRouteHeading = page.getByTestId(
218 'non-nested-deep-baz-route-heading',
219 )
220 const bazIndexHeading = page.getByTestId(
221 'non-nested-deep-baz-index-heading',
222 )
223 const bazIndexParams = page.getByTestId('non-nested-deep-baz-index-param')
224​
225 await expect(bazRouteHeading).toBeVisible()
226 await expect(bazIndexHeading).toBeVisible()
227 expect(await bazIndexParams.innerText()).toBe(
228 JSON.stringify({ baz: 'baz' }),
229 )
230 })
231​
232 test('It should not nest non-nested paths 1 level deep', async ({ page }) => {
233 const rootRouteHeading = page.getByTestId(
234 `non-nested-deep-root-route-heading`,
235 )
236​
237 await expect(rootRouteHeading).toBeVisible()
238 const bazBarLink = page.getByTestId('to-deep-baz-bar')
239​
240 await bazBarLink.click()
241 await page.waitForURL('/non-nested/deep/baz-bar/bar')
242 const bazRouteHeading = page.getByTestId(
243 'non-nested-deep-baz-route-heading',
244 )
245 const bazBarRouteHeading = page.getByTestId(
246 'non-nested-deep-baz-bar-route-heading',
247 )
248 const bazBarIndexHeading = page.getByTestId(
249 'non-nested-deep-baz-bar-index-heading',
250 )
251 const bazBarIndexParams = page.getByTestId(
252 'non-nested-deep-baz-bar-index-param',
253 )
254​
255 await expect(bazRouteHeading).not.toBeVisible()
256 await expect(bazBarRouteHeading).toBeVisible()
257 await expect(bazBarIndexHeading).toBeVisible()
258 expect(await bazBarIndexParams.innerText()).toBe(
259 JSON.stringify({ baz: 'baz-bar' }),
260 )
261 })
262​
263 test('It should not nest non-nested paths 2 levels deep', async ({
264 page,
265 }) => {
266 const rootRouteHeading = page.getByTestId(
267 `non-nested-deep-root-route-heading`,
268 )
269​
270 await expect(rootRouteHeading).toBeVisible()
271​
272 const bazBarQuxLink = page.getByTestId('to-deep-baz-bar-qux')
273 await bazBarQuxLink.click()
274​
275 await page.waitForURL('/non-nested/deep/baz-bar-qux/bar/qux')
276 const bazRouteHeading = page.getByTestId(
277 'non-nested-deep-baz-route-heading',
278 )
279 const bazBarRouteHeading = page.getByTestId(
280 'non-nested-deep-baz-bar-route-heading',
281 )
282 const bazBarQuxHeading = page.getByTestId(
283 'non-nested-deep-baz-bar-qux-heading',
284 )
285 const bazBarQuxParams = page.getByTestId(
286 'non-nested-deep-baz-bar-qux-param',
287 )
288​
289 await expect(bazRouteHeading).not.toBeVisible()
290 await expect(bazBarRouteHeading).not.toBeVisible()
291 await expect(bazBarQuxHeading).toBeVisible()
292 await expect(bazBarQuxParams).toBeVisible()
293 expect(await bazBarQuxParams.innerText()).toBe(
294 JSON.stringify({ baz: 'baz-bar-qux' }),
295 )
296 })
297​
298 test('It should nest and un-nest non-nested across paths multiple levels deep', async ({
299 page,
300 }) => {
301 const rootRouteHeading = page.getByTestId(
302 `non-nested-deep-root-route-heading`,
303 )
304​
305 await expect(rootRouteHeading).toBeVisible()
306​
307 const bazBarFooLink = page.getByTestId('to-deep-baz-bar-foo')
308 await bazBarFooLink.click()
309​
310 await page.waitForURL('/non-nested/deep/baz-bar/bar/foo')
311​
312 const bazRouteHeading = page.getByTestId(
313 'non-nested-deep-baz-route-heading',
314 )
315 const bazBarRouteHeading = page.getByTestId(
316 'non-nested-deep-baz-bar-route-heading',
317 )
318 const bazBarFooRouteHeading = page.getByTestId(
319 'non-nested-deep-baz-bar-foo-route-heading',
320 )
321 const bazBarFooIndexHeading = page.getByTestId(
322 'non-nested-deep-baz-bar-foo-index-heading',
323 )
324 const bazBarFooIndexParams = page.getByTestId(
325 'non-nested-deep-baz-bar-foo-index-param',
326 )
327​
328 await expect(bazRouteHeading).not.toBeVisible()
329 await expect(bazBarRouteHeading).toBeVisible()
330 await expect(bazBarFooRouteHeading).toBeVisible()
331 await expect(bazBarFooIndexHeading).toBeVisible()
332 await expect(bazBarFooIndexParams).toBeVisible()
333 expect(await bazBarFooIndexParams.innerText()).toBe(
334 JSON.stringify({ baz: 'baz-bar', foo: 'foo' }),
335 )
336​
337 const bazBarFooQuxLink = page.getByTestId('to-deep-baz-bar-foo-qux')
338 await bazBarFooQuxLink.click()
339 await page.waitForURL('/non-nested/deep/baz-bar-qux/bar/foo/qux')
340​
341 const bazBarFooQuxHeading = page.getByTestId(
342 'non-nested-deep-baz-bar-foo-qux-heading',
343 )
344 const bazBarFooQuxParams = page.getByTestId(
345 'non-nested-deep-baz-bar-foo-qux-param',
346 )
347​
348 await expect(bazRouteHeading).not.toBeVisible()
349 await expect(bazBarRouteHeading).toBeVisible()
350 await expect(bazBarFooRouteHeading).not.toBeVisible()
351 await expect(bazBarFooQuxHeading).toBeVisible()
352 await expect(bazBarFooQuxParams).toBeVisible()
353​
354 if (useExperimentalNonNestedRoutes) {
355 expect(await bazBarFooQuxParams.innerText()).toBe(
356 JSON.stringify({ baz: 'baz-bar-qux', foo: 'foo' }),
357 )
358 } else {
359 // this is a bug with named path params and non-nested paths
360 // that is resolved in the new experimental flag
361 expect(await bazBarFooQuxParams.innerText()).toBe(
362 JSON.stringify({ baz: 'baz-bar', foo: 'foo' }),
363 )
364 }
365 })
366})