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

relative.spec.ts

273 lines8.5 KB

1import { expect, test } from '@playwright/test'
2import combinateImport from 'combinate'
3import { getTestServerPort } from '@tanstack/router-e2e-utils'
4import packageJson from '../package.json' with { type: 'json' }
5​
6// somehow playwright does not correctly import default exports
7const combinate = (combinateImport as any).default as typeof combinateImport
8​
9const PORT = await getTestServerPort(packageJson.name)
10​
11test.describe('relative_routing', () => {
12 const internalNavigationTestMatrix = combinate({
13 navigation: ['link', 'useNavigate'] as const,
14 })
15​
16 internalNavigationTestMatrix.forEach(({ navigation }) => {
17 test(`simple relative navigation. navigation: ${navigation}`, async ({
18 page,
19 }) => {
20 await page.waitForLoadState('networkidle')
21 await page.goto(`/relative/${navigation}`)
22 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
23​
24 await expect(
25 page.getByTestId(`relative-${navigation}-header`),
26 ).toBeInViewport()
27​
28 const indexLink = page.getByTestId(`relative-${navigation}-index`)
29 const backLink = page.getByTestId(`relative-${navigation}-back`)
30 const relativeRouteA = page.getByTestId(`relative-${navigation}-a`)
31 const relativeRouteB = page.getByTestId(`relative-${navigation}-b`)
32​
33 await relativeRouteA.click()
34​
35 await page.waitForURL(
36 `http://localhost:${PORT}/relative/${navigation}/relative-${navigation}-a`,
37 )
38​
39 await expect(
40 page.getByTestId(`relative-${navigation}-header`),
41 ).toBeInViewport()
42​
43 await expect(
44 page.getByTestId(`relative-${navigation}-a-header`),
45 ).toBeInViewport()
46​
47 await relativeRouteB.click()
48​
49 await page.waitForURL(
50 `http://localhost:${PORT}/relative/${navigation}/relative-${navigation}-b`,
51 )
52​
53 await expect(
54 page.getByTestId(`relative-${navigation}-header`),
55 ).toBeInViewport()
56​
57 await expect(
58 page.getByTestId(`relative-${navigation}-b-header`),
59 ).toBeInViewport()
60​
61 await indexLink.click()
62​
63 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
64​
65 await expect(
66 page.getByTestId(`relative-${navigation}-header`),
67 ).toBeInViewport()
68​
69 await expect(
70 page.getByTestId(`relative-${navigation}-b-header`),
71 ).not.toBeInViewport()
72​
73 await backLink.click()
74​
75 await page.waitForURL(`http://localhost:${PORT}/relative`)
76​
77 await expect(page.getByTestId(`relative-routing-home`)).toBeInViewport()
78 })
79​
80 test(`nested chilren. navigation: ${navigation}`, async ({ page }) => {
81 await page.waitForLoadState('networkidle')
82 await page.goto(`/relative/${navigation}`)
83 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
84​
85 await expect(
86 page.getByTestId(`relative-${navigation}-header`),
87 ).toBeInViewport()
88​
89 const backLink = page.getByTestId(`relative-${navigation}-back`)
90​
91 const deeplyNestedChildRoute = page.getByTestId(
92 `relative-${navigation}-deeply-nested`,
93 )
94​
95 await deeplyNestedChildRoute.click()
96​
97 await page.waitForURL(
98 `http://localhost:${PORT}/relative/${navigation}/nested/deep`,
99 )
100​
101 await expect(
102 page.getByTestId(`relative-${navigation}-nested-deep-header`),
103 ).toBeInViewport()
104​
105 await backLink.click()
106​
107 await page.waitForURL(
108 `http://localhost:${PORT}/relative/${navigation}/nested`,
109 )
110​
111 await expect(
112 page.getByTestId(`relative-${navigation}-nested-header`),
113 ).toBeInViewport()
114 })
115​
116 test(`with path params. navigation: ${navigation}`, async ({ page }) => {
117 await page.waitForLoadState('networkidle')
118 await page.goto(`/relative/${navigation}`)
119 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
120​
121 await expect(
122 page.getByTestId(`relative-${navigation}-header`),
123 ).toBeInViewport()
124​
125 const pathParamRoute = page.getByTestId(`relative-${navigation}-path`)
126​
127 await pathParamRoute.click()
128​
129 await page.waitForURL(
130 `http://localhost:${PORT}/relative/${navigation}/path/a`,
131 )
132​
133 expect(page.url().endsWith('/path/a')).toBe(true)
134​
135 await expect(
136 page.getByTestId(`relative-${navigation}-path-param-header`),
137 ).toBeInViewport()
138​
139 const switchParamLink = page.getByTestId(
140 `relative-${navigation}-path-param-switchAB`,
141 )
142​
143 await switchParamLink.click()
144​
145 await page.waitForURL(
146 `http://localhost:${PORT}/relative/${navigation}/path/b`,
147 )
148​
149 expect(page.url().endsWith('/path/b')).toBe(true)
150​
151 await expect(
152 page.getByTestId(`relative-${navigation}-path-param-header`),
153 ).toBeInViewport()
154 })
155​
156 test(`with search params. navigation: ${navigation}`, async ({ page }) => {
157 await page.waitForLoadState('networkidle')
158 await page.goto(`/relative/${navigation}`)
159 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
160​
161 await expect(
162 page.getByTestId(`relative-${navigation}-header`),
163 ).toBeInViewport()
164​
165 const searchParamRoute = page.getByTestId(
166 `relative-${navigation}-withSearch`,
167 )
168​
169 await searchParamRoute.click()
170​
171 let expectedUrl = new URL(
172 `http://localhost:${PORT}/relative/${navigation}/with-search?searchParam="1"`,
173 )
174​
175 await page.waitForLoadState('domcontentloaded')
176​
177 expect(page.url().toString()).toBe(expectedUrl.toString())
178​
179 await expect(
180 page.getByTestId(`relative-${navigation}-withSearch-header`),
181 ).toBeInViewport()
182​
183 await expect(
184 page.getByTestId(`relative-${navigation}-withSearch-header`),
185 ).toContainText('searchParam: 1')
186​
187 const updateSearchLink = page.getByTestId(
188 `relative-${navigation}-withSearch-update-param`,
189 )
190​
191 await updateSearchLink.click()
192​
193 await page.waitForLoadState('domcontentloaded')
194​
195 expectedUrl = new URL(
196 `http://localhost:${PORT}/relative/${navigation}/with-search?searchParam="2"`,
197 )
198​
199 expect(page.url().toString()).toBe(expectedUrl.toString())
200​
201 await expect(
202 page.getByTestId(`relative-${navigation}-withSearch-header`),
203 ).toContainText('searchParam: 2')
204​
205 await expect(
206 page.getByTestId(`relative-${navigation}-withSearch-header`),
207 ).toBeInViewport()
208 })
209​
210 test(`shouldn't cause excessive rendering. navigation: ${navigation}`, async ({
211 page,
212 }) => {
213 let navigateMsgs = 0
214​
215 page.on('console', (msg) => {
216 if (msg.text() === 'navigate') {
217 navigateMsgs++
218 }
219 })
220​
221 await page.waitForLoadState('networkidle')
222 await page.goto(`/relative/${navigation}`)
223 await page.waitForURL(`http://localhost:${PORT}/relative/${navigation}`)
224​
225 await expect(
226 page.getByTestId(`relative-${navigation}-header`),
227 ).toBeInViewport()
228​
229 expect(navigateMsgs).toBe(1)
230 const indexLink = page.getByTestId(`relative-${navigation}-index`)
231 const backLink = page.getByTestId(`relative-${navigation}-back`)
232 const relativeRouteA = page.getByTestId(`relative-${navigation}-a`)
233 const relativeRouteB = page.getByTestId(`relative-${navigation}-b`)
234 const deeplyNestedChildRoute = page.getByTestId(
235 `relative-${navigation}-deeply-nested`,
236 )
237 const pathParamRoute = page.getByTestId(`relative-${navigation}-path`)
238 const searchParamRoute = page.getByTestId(
239 `relative-${navigation}-withSearch`,
240 )
241​
242 await relativeRouteA.click()
243 await page.waitForLoadState('domcontentloaded')
244 await relativeRouteB.click()
245 await page.waitForLoadState('domcontentloaded')
246 await deeplyNestedChildRoute.click()
247 await page.waitForLoadState('domcontentloaded')
248 await backLink.click()
249 await page.waitForLoadState('domcontentloaded')
250 await pathParamRoute.click()
251 await page.waitForLoadState('domcontentloaded')
252 const switchParamLink = page.getByTestId(
253 `relative-${navigation}-path-param-switchAB`,
254 )
255 await switchParamLink.click()
256 await page.waitForLoadState('domcontentloaded')
257 await searchParamRoute.click()
258 await page.waitForLoadState('domcontentloaded')
259​
260 const updateSearchLink = page.getByTestId(
261 `relative-${navigation}-withSearch-update-param`,
262 )
263​
264 await updateSearchLink.click()
265 await page.waitForLoadState('domcontentloaded')
266​
267 await indexLink.click()
268 await page.waitForLoadState('domcontentloaded')
269​
270 expect(navigateMsgs).toBe(1)
271 })
272 })
273})