Open History to see the last commit
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})