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

params.spec.ts

352 lines12.5 KB

1import { expect, test } from '@playwright/test'
2import { useExperimentalNonNestedRoutes } from './utils/useExperimentalNonNestedRoutes'
3import type { Page } from '@playwright/test'
4​
5test.beforeEach(async ({ page }) => {
6 await page.goto('/')
7})
8​
9test.describe('ensure single params have been parsed correctly whilst being stable in the browser', () => {
10 const cases = [
11 { value: 'hello', expected: 'hello' },
12 {
13 value: '100%25',
14 expected: '100%',
15 },
16 {
17 value: '100%2525',
18 expected: '100%25',
19 },
20 {
21 value: '100%26',
22 expected: '100&',
23 },
24 ]
25​
26 function getParsedValue(page: Page) {
27 return page.getByTestId('parsed-param-value').textContent()
28 }
29​
30 for (const { value, expected } of cases) {
31 test(`navigating to /params/single/${value}`, async ({ page, baseURL }) => {
32 await page.goto(`/params/single/${value}`)
33​
34 // on the first run, the value should be the same as the expected value
35 const valueOnFirstRun = await getParsedValue(page)
36 expect(valueOnFirstRun).toBe(expected)
37​
38 // the url/pathname should be the same as the expected value
39 const urlOnFirstRun = page.url().replace(baseURL!, '')
40 expect(urlOnFirstRun).toBe(`/params/single/${value}`)
41​
42 // click on the self link to the same value
43 await page.getByTestId('self-link-same').click()
44 const valueOnSecondRun = await getParsedValue(page)
45 expect(valueOnSecondRun).toBe(expected)
46​
47 // click on the self link to the amended value
48 await page.getByTestId('self-link-amended').click()
49 const valueOnThirdRun = await getParsedValue(page)
50 expect(valueOnThirdRun).toBe(`e2e${expected}`)
51​
52 // the url/pathname should be the same as the expected value
53 const urlOnThirdRun = page.url().replace(baseURL!, '')
54 expect(urlOnThirdRun).toBe(`/params/single/e2e${value}`)
55 })
56 }
57})
58​
59test.describe('params operations + non-nested routes', () => {
60 test.beforeEach(async ({ page }) => {
61 await page.goto('/params-ps/non-nested')
62 })
63​
64 test('useParams must resolve non-nested path params', async ({ page }) => {
65 await page.waitForURL('/params-ps/non-nested')
66​
67 const fooBarLink = page.getByTestId('l-to-non-nested-foo-bar')
68​
69 const foo2Bar2Link = page.getByTestId('l-to-non-nested-foo2-bar2')
70​
71 await expect(fooBarLink).toHaveAttribute(
72 'href',
73 '/params-ps/non-nested/foo/bar',
74 )
75​
76 await fooBarLink.click()
77 await page.waitForURL('/params-ps/non-nested/foo/bar')
78 const pagePathname = new URL(page.url()).pathname
79 expect(pagePathname).toBe('/params-ps/non-nested/foo/bar')
80​
81 const fooParamsValue = page.getByTestId('foo-params-value')
82 const fooParamsText = await fooParamsValue.innerText()
83 const fooParamsObj = JSON.parse(fooParamsText)
84 expect(fooParamsObj).toEqual({ foo: 'foo' })
85​
86 const paramsValue = page.getByTestId('foo-bar-params-value')
87 const paramsText = await paramsValue.innerText()
88 const paramsObj = JSON.parse(paramsText)
89 expect(paramsObj).toEqual({ foo: 'foo', bar: 'bar' })
90​
91 await expect(foo2Bar2Link).toHaveAttribute(
92 'href',
93 '/params-ps/non-nested/foo2/bar2',
94 )
95 await foo2Bar2Link.click()
96 await page.waitForURL('/params-ps/non-nested/foo2/bar2')
97 const pagePathname2 = new URL(page.url()).pathname
98 expect(pagePathname2).toBe('/params-ps/non-nested/foo2/bar2')
99​
100 const foo2ParamsValue = page.getByTestId('foo-params-value')
101 const foo2ParamsText = await foo2ParamsValue.innerText()
102 const foo2ParamsObj = JSON.parse(foo2ParamsText)
103 if (useExperimentalNonNestedRoutes) {
104 expect(foo2ParamsObj).toEqual({ foo: 'foo2' })
105 } else {
106 // this is a bug that is resolved in the new experimental flag
107 expect(foo2ParamsObj).toEqual({ foo: 'foo' })
108 }
109​
110 const params2Value = page.getByTestId('foo-bar-params-value')
111 const params2Text = await params2Value.innerText()
112 const params2Obj = JSON.parse(params2Text)
113 if (useExperimentalNonNestedRoutes) {
114 expect(params2Obj).toEqual({ foo: 'foo2', bar: 'bar2' })
115 } else {
116 // this is a bug that is resolved in the new experimental flag
117 expect(params2Obj).toEqual({ foo: 'foo', bar: 'bar2' })
118 }
119 })
120})
121​
122test.describe('params operations + prefix/suffix', () => {
123 test.beforeEach(async ({ page }) => {
124 await page.goto('/params-ps')
125 })
126​
127 test.describe('named params', () => {
128 const NAMED_PARAMS_PAIRS = [
129 // Test ID | Expected href
130 {
131 id: 'l-to-named-foo',
132 pathname: '/params-ps/named/foo',
133 params: { foo: 'foo' },
134 destHeadingId: 'ParamsNamedFoo',
135 },
136 {
137 id: 'l-to-named-prefixfoo',
138 pathname: '/params-ps/named/prefixfoo',
139 params: { foo: 'foo' },
140 destHeadingId: 'ParamsNamedFooPrefix',
141 },
142 {
143 id: 'l-to-named-foosuffix',
144 pathname: '/params-ps/named/foosuffix',
145 params: { foo: 'foo' },
146 destHeadingId: 'ParamsNamedFooSuffix',
147 },
148 ] satisfies Array<{
149 id: string
150 pathname: string
151 params: Record<string, string>
152 destHeadingId: string
153 }>
154​
155 test.describe('Link', () => {
156 NAMED_PARAMS_PAIRS.forEach(({ id, pathname }) => {
157 test(`interpolation for testid="${id}" has href="${pathname}"`, async ({
158 page,
159 }) => {
160 const link = page.getByTestId(id)
161 await expect(link).toHaveAttribute('href', pathname)
162 })
163 })
164​
165 NAMED_PARAMS_PAIRS.forEach(({ id, pathname }) => {
166 test(`navigation for testid="${id}" succeeds to href="${pathname}"`, async ({
167 page,
168 }) => {
169 const link = page.getByTestId(id)
170 await link.click()
171 await page.waitForLoadState('networkidle')
172 const pagePathname = new URL(page.url()).pathname
173 expect(pagePathname).toBe(pathname)
174 })
175 })
176 })
177​
178 NAMED_PARAMS_PAIRS.forEach(({ pathname, params, destHeadingId }) => {
179 test(`on first-load to "${pathname}" has correct params`, async ({
180 page,
181 }) => {
182 await page.goto(pathname)
183 await page.waitForLoadState('networkidle')
184 const pagePathname = new URL(page.url()).pathname
185 expect(pagePathname).toBe(pathname)
186​
187 const headingEl = page.getByRole('heading', { name: destHeadingId })
188 await expect(headingEl).toBeVisible()
189​
190 const paramsEl = page.getByTestId('params-output')
191 const paramsText = await paramsEl.innerText()
192 const paramsObj = JSON.parse(paramsText)
193 expect(paramsObj).toEqual(params)
194 })
195 })
196​
197 test(`ensure use params doesn't cause excess renders and is stable across various usage options`, async ({
198 page,
199 }) => {
200 await page.goto('/params-ps/named/foo')
201 await page.waitForLoadState('networkidle')
202​
203 const pagePathname = new URL(page.url()).pathname
204 expect(pagePathname).toBe('/params-ps/named/foo')
205​
206 const fooRenderCount = page.getByTestId('foo-render-count')
207 const fooIndexLink = page.getByTestId('params-foo-links-index')
208 const fooBar1Link = page.getByTestId('params-foo-links-bar1')
209 const fooBar2Link = page.getByTestId('params-foo-links-bar2')
210 const fooBarBazLink = page.getByTestId('params-foo-bar-links-baz')
211 const fooValue = page.getByTestId('params-output')
212 const fooBarValue = page.getByTestId('foo-bar-value')
213 const fooBazInBarValue = page.getByTestId('foo-baz-in-bar-value')
214 const fooBarRenderCount = page.getByTestId('foo-bar-render-count')
215 const fooBarBazValue = page.getByTestId('foo-bar-baz-value')
216​
217 await expect(fooRenderCount).toBeInViewport()
218 await expect(fooValue).toBeInViewport()
219 await expect(fooIndexLink).toBeInViewport()
220 await expect(fooBar1Link).toBeInViewport()
221 await expect(fooBar2Link).toBeInViewport()
222 await expect(fooRenderCount).toHaveText('1')
223 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
224​
225 await fooBar1Link.click()
226 await page.waitForLoadState('networkidle')
227 await expect(fooValue).toBeInViewport()
228 await expect(fooRenderCount).toBeInViewport()
229 await expect(fooBarRenderCount).toBeInViewport()
230 await expect(fooBarValue).toBeInViewport()
231 await expect(fooBazInBarValue).toBeInViewport()
232 await expect(fooBarBazLink).toBeInViewport()
233 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
234 await expect(fooRenderCount).toHaveText('1')
235 await expect(fooBarRenderCount).toHaveText('1')
236 await expect(fooBarValue).toHaveText('1')
237 await expect(fooBazInBarValue).toHaveText('no param')
238​
239 await fooBarBazLink.click()
240 await page.waitForLoadState('networkidle')
241 await expect(fooValue).toBeInViewport()
242 await expect(fooRenderCount).toBeInViewport()
243 await expect(fooBarRenderCount).toBeInViewport()
244 await expect(fooBarValue).toBeInViewport()
245 await expect(fooBazInBarValue).toBeInViewport()
246 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
247 await expect(fooRenderCount).toHaveText('1')
248 await expect(fooBarRenderCount).toHaveText('2')
249 await expect(fooBarValue).toHaveText('1')
250 await expect(fooBazInBarValue).toHaveText('1_10')
251 await expect(fooBarBazValue).toHaveText('1_10')
252​
253 await fooBar2Link.click()
254 await page.waitForLoadState('networkidle')
255 await expect(fooValue).toBeInViewport()
256 await expect(fooRenderCount).toBeInViewport()
257 await expect(fooBarValue).toBeInViewport()
258 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
259 await expect(fooRenderCount).toHaveText('1')
260 await expect(fooBarValue).toHaveText('2')
261​
262 await fooIndexLink.click()
263 await page.waitForLoadState('networkidle')
264 await expect(fooValue).toBeInViewport()
265 await expect(fooRenderCount).toBeInViewport()
266 await expect(fooBarValue).not.toBeInViewport()
267 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
268 await expect(fooRenderCount).toHaveText('1')
269 })
270 })
271​
272 test.describe('wildcard param', () => {
273 const WILDCARD_PARAM_PAIRS = [
274 // Test ID | Expected href
275 {
276 id: 'l-to-wildcard-foo',
277 pathname: '/params-ps/wildcard/foo',
278 params: { '*': 'foo', _splat: 'foo' },
279 destHeadingId: 'ParamsWildcardSplat',
280 },
281 {
282 id: 'l-to-wildcard-prefixfoo',
283 pathname: '/params-ps/wildcard/prefixfoo',
284 params: { '*': 'foo', _splat: 'foo' },
285 destHeadingId: 'ParamsWildcardSplatPrefix',
286 },
287 {
288 id: 'l-to-wildcard-foosuffix',
289 pathname: '/params-ps/wildcard/foosuffix',
290 params: { '*': 'foo', _splat: 'foo' },
291 destHeadingId: 'ParamsWildcardSplatSuffix',
292 },
293 ] satisfies Array<{
294 id: string
295 pathname: string
296 params: Record<string, string>
297 destHeadingId: string
298 }>
299​
300 test.describe('Link', () => {
301 WILDCARD_PARAM_PAIRS.forEach(({ id, pathname }) => {
302 test(`interpolation for testid="${id}" has href="${pathname}"`, async ({
303 page,
304 }) => {
305 const link = page.getByTestId(id)
306 await expect(link).toHaveAttribute('href', pathname)
307 })
308 })
309​
310 WILDCARD_PARAM_PAIRS.forEach(({ id, pathname }) => {
311 test(`navigation for testid="${id}" succeeds to href="${pathname}"`, async ({
312 page,
313 }) => {
314 const link = page.getByTestId(id)
315 await link.click()
316 await page.waitForLoadState('networkidle')
317 const pagePathname = new URL(page.url()).pathname
318 expect(pagePathname).toBe(pathname)
319 })
320 })
321 })
322​
323 WILDCARD_PARAM_PAIRS.forEach(({ pathname, params, destHeadingId }) => {
324 test(`on first-load to "${pathname}" has correct params`, async ({
325 page,
326 }) => {
327 await page.goto(pathname)
328 await page.waitForLoadState('networkidle')
329 const pagePathname = new URL(page.url()).pathname
330 expect(pagePathname).toBe(pathname)
331​
332 const headingEl = page.getByRole('heading', { name: destHeadingId })
333 await expect(headingEl).toBeVisible()
334​
335 const paramsEl = page.getByTestId('params-output')
336 const paramsText = await paramsEl.innerText()
337 const paramsObj = JSON.parse(paramsText)
338 expect(paramsObj).toEqual(params)
339 })
340 })
341 })
342})
343​
344test.describe('Unicode route rendering', () => {
345 test('should render non-latin route correctly', async ({ page, baseURL }) => {
346 await page.goto('/λŒ€ν•œλ―Όκ΅­')
347​
348 await expect(page.locator('body')).toContainText('Hello "/λŒ€ν•œλ―Όκ΅­"!')
349​
350 expect(page.url()).toBe(`${baseURL}/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD`)
351 })
352})