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

non-nested-paths.spec.ts

351 lines11.7 KB

1import { expect } from '@playwright/test'
2import { apiTest as test } from '@tanstack/router-e2e-utils'
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 expect(JSON.parse(bar2ParamValue)).toEqual(paramValue2)
189 })
190 })
191 },
192 )
193})
194​
195test.describe('Deeply nested non-nested paths', () => {
196 test.beforeEach(async ({ page }) => {
197 await page.goto('/non-nested/deep')
198 await page.waitForURL('/non-nested/deep')
199 })
200​
201 test('It should nest nested paths 1 level deep', async ({ page }) => {
202 const rootRouteHeading = page.getByTestId(
203 `non-nested-deep-root-route-heading`,
204 )
205​
206 await expect(rootRouteHeading).toBeVisible()
207​
208 const bazLink = page.getByTestId('to-deep-baz')
209 await bazLink.click()
210 await page.waitForURL('/non-nested/deep/baz')
211 const bazRouteHeading = page.getByTestId(
212 'non-nested-deep-baz-route-heading',
213 )
214 const bazIndexHeading = page.getByTestId(
215 'non-nested-deep-baz-index-heading',
216 )
217 const bazIndexParams = page.getByTestId('non-nested-deep-baz-index-param')
218​
219 await expect(bazRouteHeading).toBeVisible()
220 await expect(bazIndexHeading).toBeVisible()
221 expect(await bazIndexParams.innerText()).toBe(
222 JSON.stringify({ baz: 'baz' }),
223 )
224 })
225​
226 test('It should not nest non-nested paths 1 level deep', async ({ page }) => {
227 const rootRouteHeading = page.getByTestId(
228 `non-nested-deep-root-route-heading`,
229 )
230​
231 await expect(rootRouteHeading).toBeVisible()
232 const bazBarLink = page.getByTestId('to-deep-baz-bar')
233​
234 await bazBarLink.click()
235 await page.waitForURL('/non-nested/deep/baz-bar/bar')
236 const bazRouteHeading = page.getByTestId(
237 'non-nested-deep-baz-route-heading',
238 )
239 const bazBarRouteHeading = page.getByTestId(
240 'non-nested-deep-baz-bar-route-heading',
241 )
242 const bazBarIndexHeading = page.getByTestId(
243 'non-nested-deep-baz-bar-index-heading',
244 )
245 const bazBarIndexParams = page.getByTestId(
246 'non-nested-deep-baz-bar-index-param',
247 )
248​
249 await expect(bazRouteHeading).not.toBeVisible()
250 await expect(bazBarRouteHeading).toBeVisible()
251 await expect(bazBarIndexHeading).toBeVisible()
252 expect(await bazBarIndexParams.innerText()).toBe(
253 JSON.stringify({ baz: 'baz-bar' }),
254 )
255 })
256​
257 test('It should not nest non-nested paths 2 levels deep', async ({
258 page,
259 }) => {
260 const rootRouteHeading = page.getByTestId(
261 `non-nested-deep-root-route-heading`,
262 )
263​
264 await expect(rootRouteHeading).toBeVisible()
265​
266 const bazBarQuxLink = page.getByTestId('to-deep-baz-bar-qux')
267 await bazBarQuxLink.click()
268​
269 await page.waitForURL('/non-nested/deep/baz-bar-qux/bar/qux')
270 const bazRouteHeading = page.getByTestId(
271 'non-nested-deep-baz-route-heading',
272 )
273 const bazBarRouteHeading = page.getByTestId(
274 'non-nested-deep-baz-bar-route-heading',
275 )
276 const bazBarQuxHeading = page.getByTestId(
277 'non-nested-deep-baz-bar-qux-heading',
278 )
279 const bazBarQuxParams = page.getByTestId(
280 'non-nested-deep-baz-bar-qux-param',
281 )
282​
283 await expect(bazRouteHeading).not.toBeVisible()
284 await expect(bazBarRouteHeading).not.toBeVisible()
285 await expect(bazBarQuxHeading).toBeVisible()
286 await expect(bazBarQuxParams).toBeVisible()
287 expect(await bazBarQuxParams.innerText()).toBe(
288 JSON.stringify({ baz: 'baz-bar-qux' }),
289 )
290 })
291​
292 test('It should nest and un-nest non-nested across paths multiple levels deep', async ({
293 page,
294 }) => {
295 const rootRouteHeading = page.getByTestId(
296 `non-nested-deep-root-route-heading`,
297 )
298​
299 await expect(rootRouteHeading).toBeVisible()
300​
301 const bazBarFooLink = page.getByTestId('to-deep-baz-bar-foo')
302 await bazBarFooLink.click()
303​
304 await page.waitForURL('/non-nested/deep/baz-bar/bar/foo')
305​
306 const bazRouteHeading = page.getByTestId(
307 'non-nested-deep-baz-route-heading',
308 )
309 const bazBarRouteHeading = page.getByTestId(
310 'non-nested-deep-baz-bar-route-heading',
311 )
312 const bazBarFooRouteHeading = page.getByTestId(
313 'non-nested-deep-baz-bar-foo-route-heading',
314 )
315 const bazBarFooIndexHeading = page.getByTestId(
316 'non-nested-deep-baz-bar-foo-index-heading',
317 )
318 const bazBarFooIndexParams = page.getByTestId(
319 'non-nested-deep-baz-bar-foo-index-param',
320 )
321​
322 await expect(bazRouteHeading).not.toBeVisible()
323 await expect(bazBarRouteHeading).toBeVisible()
324 await expect(bazBarFooRouteHeading).toBeVisible()
325 await expect(bazBarFooIndexHeading).toBeVisible()
326 await expect(bazBarFooIndexParams).toBeVisible()
327 expect(await bazBarFooIndexParams.innerText()).toBe(
328 JSON.stringify({ baz: 'baz-bar', foo: 'foo' }),
329 )
330​
331 const bazBarFooQuxLink = page.getByTestId('to-deep-baz-bar-foo-qux')
332 await bazBarFooQuxLink.click()
333 await page.waitForURL('/non-nested/deep/baz-bar-qux/bar/foo/qux')
334​
335 const bazBarFooQuxHeading = page.getByTestId(
336 'non-nested-deep-baz-bar-foo-qux-heading',
337 )
338 const bazBarFooQuxParams = page.getByTestId(
339 'non-nested-deep-baz-bar-foo-qux-param',
340 )
341​
342 await expect(bazRouteHeading).not.toBeVisible()
343 await expect(bazBarRouteHeading).toBeVisible()
344 await expect(bazBarFooRouteHeading).not.toBeVisible()
345 await expect(bazBarFooQuxHeading).toBeVisible()
346 await expect(bazBarFooQuxParams).toBeVisible()
347 expect(await bazBarFooQuxParams.innerText()).toBe(
348 JSON.stringify({ baz: 'baz-bar-qux', foo: 'foo' }),
349 )
350 })
351})