Open History to see the last commit
1import { expect } from '@playwright/test'
2import { apiTest as test } from '@tanstack/router-e2e-utils'
3β
4test.describe('params operations + prefix/suffix', () => {
5 test.beforeEach(async ({ page }) => {
6 await page.goto('/params-ps')
7 })
8β
9 test.describe('named params', () => {
10 const NAMED_PARAMS_PAIRS = [
11 // Test ID | Expected href
12 {
13 id: 'l-to-named-foo',
14 pathname: '/params-ps/named/foo',
15 params: { foo: 'foo' },
16 destHeadingId: 'ParamsNamedFoo',
17 },
18 {
19 id: 'l-to-named-prefixfoo',
20 pathname: '/params-ps/named/prefixfoo',
21 params: { foo: 'foo' },
22 destHeadingId: 'ParamsNamedFooPrefix',
23 },
24 {
25 id: 'l-to-named-foosuffix',
26 pathname: '/params-ps/named/foosuffix',
27 params: { foo: 'foo' },
28 destHeadingId: 'ParamsNamedFooSuffix',
29 },
30 ] satisfies Array<{
31 id: string
32 pathname: string
33 params: Record<string, string>
34 destHeadingId: string
35 }>
36β
37 test.describe('Link', () => {
38 NAMED_PARAMS_PAIRS.forEach(({ id, pathname }) => {
39 test(`interpolation for testid="${id}" has href="${pathname}"`, async ({
40 page,
41 }) => {
42 const link = page.getByTestId(id)
43 await expect(link).toHaveAttribute('href', pathname)
44 })
45 })
46β
47 NAMED_PARAMS_PAIRS.forEach(({ id, pathname }) => {
48 test(`navigation for testid="${id}" succeeds to href="${pathname}"`, async ({
49 page,
50 }) => {
51 const link = page.getByTestId(id)
52 await link.click()
53 await page.waitForLoadState('networkidle')
54 const pagePathname = new URL(page.url()).pathname
55 expect(pagePathname).toBe(pathname)
56 })
57 })
58 })
59β
60 NAMED_PARAMS_PAIRS.forEach(({ pathname, params, destHeadingId }) => {
61 test(`on first-load to "${pathname}" has correct params`, async ({
62 page,
63 }) => {
64 await page.goto(pathname)
65 await page.waitForLoadState('networkidle')
66 const pagePathname = new URL(page.url()).pathname
67 expect(pagePathname).toBe(pathname)
68β
69 const headingEl = page.getByRole('heading', { name: destHeadingId })
70 await expect(headingEl).toBeVisible()
71β
72 const paramsEl = page.getByTestId('params-output')
73 const paramsText = await paramsEl.innerText()
74 const paramsObj = JSON.parse(paramsText)
75 expect(paramsObj).toEqual(params)
76 })
77 })
78 })
79β
80 test.describe('wildcard param', () => {
81 const WILDCARD_PARAM_PAIRS = [
82 // Test ID | Expected href
83 {
84 id: 'l-to-wildcard-foo',
85 pathname: '/params-ps/wildcard/foo',
86 params: { '*': 'foo', _splat: 'foo' },
87 destHeadingId: 'ParamsWildcardSplat',
88 },
89 {
90 id: 'l-to-wildcard-prefixfoo',
91 pathname: '/params-ps/wildcard/prefixfoo',
92 params: { '*': 'foo', _splat: 'foo' },
93 destHeadingId: 'ParamsWildcardSplatPrefix',
94 },
95 {
96 id: 'l-to-wildcard-foosuffix',
97 pathname: '/params-ps/wildcard/foosuffix',
98 params: { '*': 'foo', _splat: 'foo' },
99 destHeadingId: 'ParamsWildcardSplatSuffix',
100 },
101 ] satisfies Array<{
102 id: string
103 pathname: string
104 params: Record<string, string>
105 destHeadingId: string
106 }>
107β
108 test.describe('Link', () => {
109 WILDCARD_PARAM_PAIRS.forEach(({ id, pathname }) => {
110 test(`interpolation for testid="${id}" has href="${pathname}"`, async ({
111 page,
112 }) => {
113 const link = page.getByTestId(id)
114 await expect(link).toHaveAttribute('href', pathname)
115 })
116 })
117β
118 WILDCARD_PARAM_PAIRS.forEach(({ id, pathname }) => {
119 test(`navigation for testid="${id}" succeeds to href="${pathname}"`, async ({
120 page,
121 }) => {
122 const link = page.getByTestId(id)
123 await link.click()
124 await page.waitForLoadState('networkidle')
125 const pagePathname = new URL(page.url()).pathname
126 expect(pagePathname).toBe(pathname)
127 })
128 })
129 })
130β
131 WILDCARD_PARAM_PAIRS.forEach(({ pathname, params, destHeadingId }) => {
132 test(`on first-load to "${pathname}" has correct params`, async ({
133 page,
134 }) => {
135 await page.goto(pathname)
136 await page.waitForLoadState('networkidle')
137 const pagePathname = new URL(page.url()).pathname
138 expect(pagePathname).toBe(pathname)
139β
140 const headingEl = page.getByRole('heading', { name: destHeadingId })
141 await expect(headingEl).toBeVisible()
142β
143 const paramsEl = page.getByTestId('params-output')
144 const paramsText = await paramsEl.innerText()
145 const paramsObj = JSON.parse(paramsText)
146 expect(paramsObj).toEqual(params)
147 })
148 })
149 })
150})