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