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

params.spec.ts

505 lines17.7 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 {
149 id: 'l-to-named-foo-special-characters',
150 pathname: '/params-ps/named/foo%25%5C%2F%F0%9F%9A%80%EB%8C%80',
151 params: { foo: 'foo%\\/πŸš€λŒ€' },
152 destHeadingId: 'ParamsNamedFoo',
153 },
154 ] satisfies Array<{
155 id: string
156 pathname: string
157 params: Record<string, string>
158 destHeadingId: string
159 }>
160​
161 test.describe('Link', () => {
162 NAMED_PARAMS_PAIRS.forEach(({ id, pathname }) => {
163 test(`interpolation for testid="${id}" has href="${pathname}"`, async ({
164 page,
165 }) => {
166 const link = page.getByTestId(id)
167 await expect(link).toHaveAttribute('href', pathname)
168 })
169 })
170​
171 NAMED_PARAMS_PAIRS.forEach(({ id, pathname }) => {
172 test(`navigation for testid="${id}" succeeds to href="${pathname}"`, async ({
173 page,
174 }) => {
175 const link = page.getByTestId(id)
176 await link.click()
177 await page.waitForLoadState('networkidle')
178 const pagePathname = new URL(page.url()).pathname
179 expect(pagePathname).toBe(pathname)
180 })
181 })
182 })
183​
184 NAMED_PARAMS_PAIRS.forEach(({ pathname, params, destHeadingId }) => {
185 test(`on first-load to "${pathname}" has correct params`, async ({
186 page,
187 }) => {
188 await page.goto(pathname)
189 await page.waitForLoadState('networkidle')
190 const pagePathname = new URL(page.url()).pathname
191 expect(pagePathname).toBe(pathname)
192​
193 const headingEl = page.getByRole('heading', { name: destHeadingId })
194 await expect(headingEl).toBeVisible()
195​
196 const paramsEl = page.getByTestId('params-output')
197 const paramsText = await paramsEl.innerText()
198 const paramsObj = JSON.parse(paramsText)
199 expect(paramsObj).toEqual(params)
200 })
201 })
202​
203 test(`ensure use params doesn't cause excess renders and is stable across various usage options`, async ({
204 page,
205 }) => {
206 await page.goto('/params-ps/named/foo')
207 await page.waitForLoadState('networkidle')
208​
209 const pagePathname = new URL(page.url()).pathname
210 expect(pagePathname).toBe('/params-ps/named/foo')
211​
212 const fooRenderCount = page.getByTestId('foo-render-count')
213 const fooIndexLink = page.getByTestId('params-foo-links-index')
214 const fooBar1Link = page.getByTestId('params-foo-links-bar1')
215 const fooBar2Link = page.getByTestId('params-foo-links-bar2')
216 const fooBarBazLink = page.getByTestId('params-foo-bar-links-baz')
217 const fooValue = page.getByTestId('params-output')
218 const fooBarValue = page.getByTestId('foo-bar-value')
219 const fooBazInBarValue = page.getByTestId('foo-baz-in-bar-value')
220 const fooBarRenderCount = page.getByTestId('foo-bar-render-count')
221 const fooBarBazValue = page.getByTestId('foo-bar-baz-value')
222​
223 await expect(fooRenderCount).toBeInViewport()
224 await expect(fooValue).toBeInViewport()
225 await expect(fooIndexLink).toBeInViewport()
226 await expect(fooBar1Link).toBeInViewport()
227 await expect(fooBar2Link).toBeInViewport()
228 await expect(fooRenderCount).toHaveText('1')
229 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
230​
231 await fooBar1Link.click()
232 await page.waitForLoadState('networkidle')
233 await expect(fooValue).toBeInViewport()
234 await expect(fooRenderCount).toBeInViewport()
235 await expect(fooBarRenderCount).toBeInViewport()
236 await expect(fooBarValue).toBeInViewport()
237 await expect(fooBazInBarValue).toBeInViewport()
238 await expect(fooBarBazLink).toBeInViewport()
239 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
240 await expect(fooRenderCount).toHaveText('1')
241 await expect(fooBarRenderCount).toHaveText('1')
242 await expect(fooBarValue).toHaveText('1')
243 await expect(fooBazInBarValue).toHaveText('no param')
244​
245 await fooBarBazLink.click()
246 await page.waitForLoadState('networkidle')
247 await expect(fooValue).toBeInViewport()
248 await expect(fooRenderCount).toBeInViewport()
249 await expect(fooBarRenderCount).toBeInViewport()
250 await expect(fooBarValue).toBeInViewport()
251 await expect(fooBazInBarValue).toBeInViewport()
252 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
253 await expect(fooRenderCount).toHaveText('1')
254 await expect(fooBarRenderCount).toHaveText('2')
255 await expect(fooBarValue).toHaveText('1')
256 await expect(fooBazInBarValue).toHaveText('1_10')
257 await expect(fooBarBazValue).toHaveText('1_10')
258​
259 await fooBar2Link.click()
260 await page.waitForLoadState('networkidle')
261 await expect(fooValue).toBeInViewport()
262 await expect(fooRenderCount).toBeInViewport()
263 await expect(fooBarValue).toBeInViewport()
264 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
265 await expect(fooRenderCount).toHaveText('1')
266 await expect(fooBarValue).toHaveText('2')
267​
268 await fooIndexLink.click()
269 await page.waitForLoadState('networkidle')
270 await expect(fooValue).toBeInViewport()
271 await expect(fooRenderCount).toBeInViewport()
272 await expect(fooBarValue).not.toBeInViewport()
273 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
274 await expect(fooRenderCount).toHaveText('1')
275 })
276 })
277​
278 test.describe('wildcard param', () => {
279 const WILDCARD_PARAM_PAIRS = [
280 // Test ID | Expected href
281 {
282 id: 'l-to-wildcard-foo',
283 pathname: '/params-ps/wildcard/foo',
284 params: { '*': 'foo', _splat: 'foo' },
285 destHeadingId: 'ParamsWildcardSplat',
286 },
287 {
288 id: 'l-to-wildcard-prefixfoo',
289 pathname: '/params-ps/wildcard/prefixfoo',
290 params: { '*': 'foo', _splat: 'foo' },
291 destHeadingId: 'ParamsWildcardSplatPrefix',
292 },
293 {
294 id: 'l-to-wildcard-foosuffix',
295 pathname: '/params-ps/wildcard/foosuffix',
296 params: { '*': 'foo', _splat: 'foo' },
297 destHeadingId: 'ParamsWildcardSplatSuffix',
298 },
299 {
300 id: 'l-to-wildcard-escaped',
301 pathname: `/params-ps/wildcard/test[s%5C/.%5C/parameter%25!%F0%9F%9A%80]`,
302 params: {
303 _splat: 'test[s\\/.\\/parameter%!πŸš€]',
304 '*': 'test[s\\/.\\/parameter%!πŸš€]',
305 },
306 destHeadingId: 'ParamsWildcardSplat',
307 },
308 {
309 id: 'l-to-wildcard-prefix-escaped',
310 pathname: `/params-ps/wildcard/prefix@%EB%8C%80test[s%5C/.%5C/parameter%25!%F0%9F%9A%80]`,
311 params: {
312 _splat: 'test[s\\/.\\/parameter%!πŸš€]',
313 '*': 'test[s\\/.\\/parameter%!πŸš€]',
314 },
315 destHeadingId: 'ParamsWildcardSplatPrefix',
316 },
317 {
318 id: 'l-to-wildcard-suffix-escaped',
319 pathname: `/params-ps/wildcard/test[s%5C/.%5C/parameter%25!%F0%9F%9A%80]suffix@%EB%8C%80`,
320 params: {
321 _splat: 'test[s\\/.\\/parameter%!πŸš€]',
322 '*': 'test[s\\/.\\/parameter%!πŸš€]',
323 },
324 destHeadingId: 'ParamsWildcardSplatSuffix',
325 },
326 {
327 id: 'l-to-wildcard-encoded',
328 pathname:
329 '/params-ps/wildcard/%25EB%258C%2580%25ED%2595%259C%25EB%25AF%25BC%25EA%25B5%25AD',
330 params: {
331 '*': '%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD',
332 _splat: '%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD',
333 },
334 destHeadingId: 'ParamsWildcardSplat',
335 },
336 ] satisfies Array<{
337 id: string
338 pathname: string
339 params: Record<string, string>
340 destHeadingId: string
341 }>
342​
343 test.describe('Link', () => {
344 WILDCARD_PARAM_PAIRS.forEach(({ id, pathname }) => {
345 test(`interpolation for testid="${id}" has href="${pathname}"`, async ({
346 page,
347 }) => {
348 const link = page.getByTestId(id)
349 await expect(link).toHaveAttribute('href', pathname)
350 })
351 })
352​
353 WILDCARD_PARAM_PAIRS.forEach(({ id, pathname }) => {
354 test(`navigation for testid="${id}" succeeds to href="${pathname}"`, async ({
355 page,
356 }) => {
357 const link = page.getByTestId(id)
358 await link.click()
359 await page.waitForLoadState('networkidle')
360 const pagePathname = new URL(page.url()).pathname
361 expect(pagePathname).toBe(pathname)
362 })
363 })
364 })
365​
366 WILDCARD_PARAM_PAIRS.forEach(({ pathname, params, destHeadingId }) => {
367 test(`on first-load to "${pathname}" has correct params`, async ({
368 page,
369 }) => {
370 await page.goto(pathname)
371 await page.waitForLoadState('networkidle')
372 const pagePathname = new URL(page.url()).pathname
373 expect(pagePathname).toBe(pathname)
374​
375 const headingEl = page.getByRole('heading', { name: destHeadingId })
376 await expect(headingEl).toBeVisible()
377​
378 const paramsEl = page.getByTestId('params-output')
379 const paramsText = await paramsEl.innerText()
380 const paramsObj = JSON.parse(paramsText)
381 expect(paramsObj).toEqual(params)
382 })
383 })
384 })
385})
386​
387test.describe('Unicode params', () => {
388 test('should render non-latin route correctly across multiple params', async ({
389 page,
390 baseURL,
391 }) => {
392 await page.goto('/params-ps')
393 await page.waitForURL('/params-ps')
394 const fooLink = page.getByTestId('l-to-named-foo-special-characters')
395​
396 await fooLink.click()
397 await page.waitForURL('/params-ps/named/foo%25%5C%2F%F0%9F%9A%80%EB%8C%80')
398​
399 expect(page.url()).toBe(
400 `${baseURL}/params-ps/named/foo%25%5C%2F%F0%9F%9A%80%EB%8C%80`,
401 )
402​
403 const headingEl = page.getByRole('heading', { name: 'ParamsNamedFoo' })
404 await expect(headingEl).toBeVisible()
405 let paramsEl = page.getByTestId('params-output')
406 let paramsText = await paramsEl.innerText()
407 expect(paramsText).toEqual(JSON.stringify({ foo: 'foo%\\/πŸš€λŒ€' }))
408​
409 const barLink = page.getByTestId('params-foo-links-bar-special-characters')
410​
411 await barLink.click()
412​
413 await page.waitForURL(
414 '/params-ps/named/foo%25%5C%2F%F0%9F%9A%80%EB%8C%80/%F0%9F%9A%80%252F%2Fabc%EB%8C%80',
415 )
416​
417 expect(page.url()).toBe(
418 `${baseURL}/params-ps/named/foo%25%5C%2F%F0%9F%9A%80%EB%8C%80/%F0%9F%9A%80%252F%2Fabc%EB%8C%80`,
419 )
420​
421 paramsEl = page.getByTestId('foo-bar-value')
422 paramsText = await paramsEl.innerText()
423 expect(paramsText).toEqual('πŸš€%2F/abcλŒ€')
424 })
425​
426 test.describe('should handle routes with non-latin paths and params correctly', () => {
427 const testCases = [
428 {
429 name: 'named',
430 childPath: 'πŸš€',
431 latinParams: 'foo',
432 unicodeParams: 'foo%\\/πŸš€λŒ€',
433 },
434 {
435 name: 'wildcard',
436 childPath: 'wildcard',
437 latinParams: 'foo/bar',
438 unicodeParams: 'foo%\\/πŸš€λŒ€',
439 },
440 ]
441​
442 testCases.forEach(({ name, childPath, latinParams, unicodeParams }) => {
443 test(`${name} params`, async ({ page, baseURL }) => {
444 const pascalCaseName = name.charAt(0).toUpperCase() + name.slice(1)
445 const routeParentPath = '/λŒ€ν•œλ―Όκ΅­'
446 const encodedRouteParentPath = encodeURI(routeParentPath)
447 const childRoutePath = `${routeParentPath}/${childPath}`
448 const encodedChildRoutePath = encodeURI(childRoutePath)
449​
450 await page.goto(routeParentPath)
451 await page.waitForURL(encodedRouteParentPath)
452​
453 const headingRootEl = page.getByTestId('unicode-heading')
454​
455 expect(await headingRootEl.innerText()).toBe('Hello "/λŒ€ν•œλ―Όκ΅­"!')
456​
457 const latinLink = page.getByTestId(`l-to-${name}-latin`)
458 const unicodeLink = page.getByTestId(`l-to-${name}-unicode`)
459​
460 await expect(latinLink).not.toContainClass('font-bold')
461 await expect(unicodeLink).not.toContainClass('font-bold')
462​
463 await latinLink.click()
464​
465 await page.waitForURL(`${encodedChildRoutePath}/${latinParams}`)
466​
467 expect(page.url()).toBe(
468 `${baseURL}${encodedChildRoutePath}/${latinParams}`,
469 )
470​
471 await expect(latinLink).toContainClass('font-bold')
472 await expect(unicodeLink).not.toContainClass('font-bold')
473​
474 const headingEl = page.getByTestId(`unicode-${name}-heading`)
475 const paramsEl = page.getByTestId(`unicode-${name}-params`)
476​
477 expect(await headingEl.innerText()).toBe(
478 `Unicode ${pascalCaseName} Params`,
479 )
480 expect(await paramsEl.innerText()).toBe(latinParams)
481​
482 await unicodeLink.click()
483​
484 const encodedParams =
485 name === 'wildcard'
486 ? encodeURI(unicodeParams)
487 : encodeURIComponent(unicodeParams)
488​
489 await page.waitForURL(`${encodedChildRoutePath}/${encodedParams}`)
490​
491 expect(page.url()).toBe(
492 `${baseURL}${encodedChildRoutePath}/${encodedParams}`,
493 )
494​
495 await expect(latinLink).not.toContainClass('font-bold')
496 await expect(unicodeLink).toContainClass('font-bold')
497​
498 expect(await headingEl.innerText()).toBe(
499 `Unicode ${pascalCaseName} Params`,
500 )
501 expect(await paramsEl.innerText()).toBe(unicodeParams)
502 })
503 })
504 })
505})