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

params.spec.ts

195 lines7.4 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('ensure only applicable params are returned and updated with multiple params', async ({
60 page,
61}) => {
62 await page.goto('/params-ps/named/foo')
63 await page.waitForLoadState('networkidle')
64​
65 const pagePathname = new URL(page.url()).pathname
66 expect(pagePathname).toBe('/params-ps/named/foo')
67​
68 const fooRenderCount = page.getByTestId('foo-render-count')
69 const fooIndexLink = page.getByTestId('params-foo-links-index')
70 const fooBar1Link = page.getByTestId('params-foo-links-bar1')
71 const fooBar2Link = page.getByTestId('params-foo-links-bar2')
72 const fooBarBazLink = page.getByTestId('params-foo-bar-links-baz')
73 const fooValue = page.getByTestId('params-output')
74 const fooBarValue = page.getByTestId('foo-bar-value')
75 const fooBazInBarValue = page.getByTestId('foo-baz-in-bar-value')
76 const fooBarRenderCount = page.getByTestId('foo-bar-render-count')
77 const fooBazInBarRenderCount = page.getByTestId('foo-baz-in-bar-render-count')
78 const fooBarBazValue = page.getByTestId('foo-bar-baz-value')
79​
80 await expect(fooRenderCount).toBeInViewport()
81 await expect(fooValue).toBeInViewport()
82 await expect(fooIndexLink).toBeInViewport()
83 await expect(fooBar1Link).toBeInViewport()
84 await expect(fooBar2Link).toBeInViewport()
85 await expect(fooRenderCount).toHaveText('1')
86 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
87​
88 await fooBar1Link.click()
89 await page.waitForLoadState('networkidle')
90 await expect(fooValue).toBeInViewport()
91 await expect(fooRenderCount).toBeInViewport()
92 await expect(fooBarRenderCount).toBeInViewport()
93 await expect(fooBarValue).toBeInViewport()
94 await expect(fooBazInBarValue).toBeInViewport()
95 await expect(fooBarBazLink).toBeInViewport()
96 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
97 await expect(fooRenderCount).toHaveText('1')
98 await expect(fooBarRenderCount).toHaveText('1')
99 await expect(fooBarValue).toHaveText('1')
100 await expect(fooBazInBarValue).toHaveText('no param')
101 await expect(fooBazInBarRenderCount).toHaveText('1')
102​
103 await fooBarBazLink.click()
104 await page.waitForLoadState('networkidle')
105 await expect(fooValue).toBeInViewport()
106 await expect(fooRenderCount).toBeInViewport()
107 await expect(fooBarRenderCount).toBeInViewport()
108 await expect(fooBarValue).toBeInViewport()
109 await expect(fooBazInBarValue).toBeInViewport()
110 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
111 await expect(fooRenderCount).toHaveText('1')
112 await expect(fooBarRenderCount).toHaveText('1')
113 await expect(fooBarValue).toHaveText('1')
114 await expect(fooBazInBarValue).toHaveText('1_10')
115 await expect(fooBarBazValue).toHaveText('1_10')
116 await expect(fooBazInBarRenderCount).toHaveText('2')
117​
118 await fooBar2Link.click()
119 await expect(fooValue).toBeInViewport()
120 await expect(fooRenderCount).toBeInViewport()
121 await expect(fooBarValue).toBeInViewport()
122 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
123 await expect(fooRenderCount).toHaveText('1')
124 await expect(fooBarValue).toHaveText('2')
125​
126 await fooIndexLink.click()
127 await expect(fooValue).toBeInViewport()
128 await expect(fooRenderCount).toBeInViewport()
129 await expect(fooBarValue).not.toBeInViewport()
130 await expect(fooValue).toHaveText(JSON.stringify({ foo: 'foo' }))
131 await expect(fooRenderCount).toHaveText('1')
132})
133​
134test.describe('params operations + non-nested routes', () => {
135 test.beforeEach(async ({ page }) => {
136 await page.goto('/params-ps/non-nested')
137 })
138​
139 test('useParams must resolve non-nested path params', async ({ page }) => {
140 await page.waitForURL('/params-ps/non-nested')
141​
142 const fooBarLink = page.getByTestId('l-to-non-nested-foo-bar')
143​
144 const foo2Bar2Link = page.getByTestId('l-to-non-nested-foo2-bar2')
145​
146 await expect(fooBarLink).toHaveAttribute(
147 'href',
148 '/params-ps/non-nested/foo/bar',
149 )
150 await fooBarLink.click()
151 await page.waitForURL('/params-ps/non-nested/foo/bar')
152​
153 const pagePathname = new URL(page.url()).pathname
154 expect(pagePathname).toBe('/params-ps/non-nested/foo/bar')
155​
156 const fooParamsValue = page.getByTestId('foo-params-value')
157 const fooParamsText = await fooParamsValue.innerText()
158 const fooParamsObj = JSON.parse(fooParamsText)
159 expect(fooParamsObj).toEqual({ foo: 'foo' })
160​
161 const paramsValue = page.getByTestId('foo-bar-params-value')
162 const paramsText = await paramsValue.innerText()
163 const paramsObj = JSON.parse(paramsText)
164 expect(paramsObj).toEqual({ foo: 'foo', bar: 'bar' })
165​
166 await expect(foo2Bar2Link).toHaveAttribute(
167 'href',
168 '/params-ps/non-nested/foo2/bar2',
169 )
170 await foo2Bar2Link.click()
171 await page.waitForURL('/params-ps/non-nested/foo2/bar2')
172 const pagePathname2 = new URL(page.url()).pathname
173 expect(pagePathname2).toBe('/params-ps/non-nested/foo2/bar2')
174​
175 const foo2ParamsValue = page.getByTestId('foo-params-value')
176 const foo2ParamsText = await foo2ParamsValue.innerText()
177 const foo2ParamsObj = JSON.parse(foo2ParamsText)
178 if (useExperimentalNonNestedRoutes) {
179 expect(foo2ParamsObj).toEqual({ foo: 'foo2' })
180 } else {
181 // this is a bug that is resolved in the new experimental flag
182 expect(foo2ParamsObj).toEqual({ foo: 'foo' })
183 }
184​
185 const params2Value = page.getByTestId('foo-bar-params-value')
186 const params2Text = await params2Value.innerText()
187 const params2Obj = JSON.parse(params2Text)
188 if (useExperimentalNonNestedRoutes) {
189 expect(params2Obj).toEqual({ foo: 'foo2', bar: 'bar2' })
190 } else {
191 // this is a bug that is resolved in the new experimental flag
192 expect(params2Obj).toEqual({ foo: 'foo', bar: 'bar2' })
193 }
194 })
195})