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