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

non-nested-paths.spec.ts

199 lines7.0 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})