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

params.spec.ts

496 lines17.3 KB

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