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

app.spec.ts

410 lines14.1 KB

1import { expect } from '@playwright/test'
2import { apiTest as test } from '@tanstack/router-e2e-utils'
3import type { Page } from '@playwright/test'
4​
5test.beforeEach(async ({ page }) => {
6 await page.goto('/')
7})
8​
9test('Navigating to a post page', async ({ page }) => {
10 await page.getByRole('link', { name: 'Posts' }).click()
11 await page.getByRole('link', { name: 'sunt aut facere repe' }).click()
12 await expect(page.getByRole('heading')).toContainText('sunt aut facere')
13})
14​
15test('Navigating nested layouts', async ({ page }) => {
16 await page.getByRole('link', { name: 'Layout', exact: true }).click()
17​
18 await expect(page.locator('#app')).toContainText("I'm a layout")
19 await expect(page.locator('#app')).toContainText("I'm a nested layout")
20​
21 await page.getByRole('link', { name: 'Layout A' }).click()
22 await expect(page.locator('#app')).toContainText("I'm layout A!")
23​
24 await page.getByRole('link', { name: 'Layout B' }).click()
25 await expect(page.locator('#app')).toContainText("I'm layout B!")
26})
27​
28test('Navigating to a not-found route', async ({ page }) => {
29 await page.getByRole('link', { name: 'This Route Does Not Exist' }).click()
30 await expect(page.getByRole('paragraph')).toContainText(
31 'This is the notFoundComponent configured on root route',
32 )
33 await page.getByRole('link', { name: 'Start Over' }).click()
34 await expect(page.getByRole('heading')).toContainText('Welcome Home!')
35})
36​
37test('lazy file route errorComponent is rendered when the loader throws', async ({
38 page,
39}) => {
40 await page.goto('/lazy-error')
41​
42 await expect(page.getByTestId('lazy-route-error-component')).toContainText(
43 'Lazy route error component',
44 )
45 await expect(page.locator('body')).not.toContainText('Something went wrong!')
46})
47​
48test("useBlocker doesn't block navigation if condition is not met", async ({
49 page,
50}) => {
51 await page.goto('/editing-a')
52 await expect(page.getByRole('heading')).toContainText('Editing A')
53​
54 await page.getByRole('button', { name: 'Go to next step' }).click()
55 await expect(page.getByRole('heading')).toContainText('Editing B')
56})
57​
58test('useBlocker does block navigation if condition is met', async ({
59 page,
60}) => {
61 await page.goto('/editing-a')
62 await expect(page.getByRole('heading')).toContainText('Editing A')
63​
64 await page.getByLabel('Enter your name:').fill('foo')
65​
66 await page.getByRole('button', { name: 'Go to next step' }).click()
67 await expect(page.getByRole('heading')).toContainText('Editing A')
68​
69 await expect(page.getByRole('button', { name: 'Proceed' })).toBeVisible()
70})
71​
72test('Proceeding through blocked navigation works', async ({ page }) => {
73 await page.goto('/editing-a')
74 await expect(page.getByRole('heading')).toContainText('Editing A')
75​
76 await page.getByLabel('Enter your name:').fill('foo')
77​
78 await page.getByRole('button', { name: 'Go to next step' }).click()
79 await expect(page.getByRole('heading')).toContainText('Editing A')
80​
81 await page.getByRole('button', { name: 'Proceed' }).click()
82 await expect(page.getByRole('heading')).toContainText('Editing B')
83})
84​
85test("legacy useBlocker doesn't block navigation if condition is not met", async ({
86 page,
87}) => {
88 await page.goto('/editing-b')
89 await expect(page.getByRole('heading')).toContainText('Editing B')
90​
91 await page.getByRole('button', { name: 'Go back' }).click()
92 await expect(page.getByRole('heading')).toContainText('Editing A')
93})
94​
95test('legacy useBlocker does block navigation if condition is met', async ({
96 page,
97}) => {
98 await page.goto('/editing-b')
99 await expect(page.getByRole('heading')).toContainText('Editing B')
100​
101 await page.getByLabel('Enter your name:').fill('foo')
102​
103 await page.getByRole('button', { name: 'Go back' }).click()
104 await expect(page.getByRole('heading')).toContainText('Editing B')
105​
106 await expect(page.getByRole('button', { name: 'Proceed' })).toBeVisible()
107})
108​
109test('legacy Proceeding through blocked navigation works', async ({ page }) => {
110 await page.goto('/editing-b')
111 await expect(page.getByRole('heading')).toContainText('Editing B')
112​
113 await page.getByLabel('Enter your name:').fill('foo')
114​
115 await page.getByRole('button', { name: 'Go back' }).click()
116 await expect(page.getByRole('heading')).toContainText('Editing B')
117​
118 await page.getByRole('button', { name: 'Proceed' }).click()
119 await expect(page.getByRole('heading')).toContainText('Editing A')
120})
121​
122test('useCanGoBack correctly disables back button', async ({ page }) => {
123 const getBackButtonDisabled = async () => {
124 const backButton = page.getByTestId('back-button')
125 const isDisabled = (await backButton.getAttribute('disabled')) !== null
126 return isDisabled
127 }
128​
129 expect(await getBackButtonDisabled()).toBe(true)
130​
131 await page.getByRole('link', { name: 'Posts' }).click()
132 await expect(page.getByTestId('posts-links')).toBeInViewport()
133 expect(await getBackButtonDisabled()).toBe(false)
134​
135 await page.getByRole('link', { name: 'sunt aut facere repe' }).click()
136 await expect(page.getByTestId('post-title')).toBeInViewport()
137 expect(await getBackButtonDisabled()).toBe(false)
138​
139 await page.reload()
140 expect(await getBackButtonDisabled()).toBe(false)
141​
142 await page.goBack()
143 expect(await getBackButtonDisabled()).toBe(false)
144​
145 await page.goForward()
146 expect(await getBackButtonDisabled()).toBe(false)
147​
148 await page.goBack()
149 expect(await getBackButtonDisabled()).toBe(false)
150​
151 await page.goBack()
152 expect(await getBackButtonDisabled()).toBe(true)
153​
154 await page.reload()
155 expect(await getBackButtonDisabled()).toBe(true)
156})
157​
158test('useCanGoBack correctly disables back button, using router.history and window.history', async ({
159 page,
160}) => {
161 const getBackButtonDisabled = async () => {
162 const backButton = page.getByTestId('back-button')
163 const isDisabled = (await backButton.getAttribute('disabled')) !== null
164 return isDisabled
165 }
166​
167 await page.getByRole('link', { name: 'Posts' }).click()
168 await expect(page.getByTestId('posts-links')).toBeInViewport()
169 await page.getByRole('link', { name: 'sunt aut facere repe' }).click()
170 await expect(page.getByTestId('post-title')).toBeInViewport()
171 await page.getByTestId('back-button').click()
172 expect(await getBackButtonDisabled()).toBe(false)
173​
174 await page.reload()
175 expect(await getBackButtonDisabled()).toBe(false)
176​
177 await page.getByTestId('back-button').click()
178 expect(await getBackButtonDisabled()).toBe(true)
179​
180 await page.evaluate('window.history.forward()')
181 expect(await getBackButtonDisabled()).toBe(false)
182​
183 await page.evaluate('window.history.forward()')
184 expect(await getBackButtonDisabled()).toBe(false)
185​
186 await page.evaluate('window.history.back()')
187 expect(await getBackButtonDisabled()).toBe(false)
188​
189 await page.evaluate('window.history.back()')
190 expect(await getBackButtonDisabled()).toBe(true)
191​
192 await page.reload()
193 expect(await getBackButtonDisabled()).toBe(true)
194})
195​
196const testCases = [
197 {
198 description: 'Navigating to a route inside a route group',
199 testId: 'link-to-route-inside-group',
200 },
201 {
202 description:
203 'Navigating to a route inside a subfolder inside a route group ',
204 testId: 'link-to-route-inside-group-inside-subfolder',
205 },
206 {
207 description: 'Navigating to a route inside a route group inside a layout',
208 testId: 'link-to-route-inside-group-inside-layout',
209 },
210 {
211 description: 'Navigating to a lazy route inside a route group',
212 testId: 'link-to-lazy-route-inside-group',
213 },
214​
215 {
216 description: 'Navigating to the only route inside a route group ',
217 testId: 'link-to-only-route-inside-group',
218 },
220​
221testCases.forEach(({ description, testId }) => {
222 test(description, async ({ page }) => {
223 await page.getByTestId(testId).click()
224 await expect(page.getByTestId('search-via-hook')).toContainText('world')
225 await expect(page.getByTestId('search-via-route-hook')).toContainText(
226 'world',
227 )
228 await expect(page.getByTestId('search-via-route-api')).toContainText(
229 'world',
230 )
231 })
232})
233​
234test('navigating to an unnested route', async ({ page }) => {
235 const postId = 'hello-world'
236 await page.goto(`/posts/${postId}/edit`)
237 await page.waitForURL(`/posts/${postId}/edit`)
238 await expect(page.getByTestId('params-via-hook')).toContainText(postId)
239 await expect(page.getByTestId('params-via-route-hook')).toContainText(postId)
240 await expect(page.getByTestId('params-via-route-api')).toContainText(postId)
241})
242​
243async function getRenderCount(page: Page) {
244 const renderCount = parseInt(
245 await page.getByTestId('render-count').innerText(),
246 )
247 return renderCount
249async function structuralSharingTest(page: Page, enabled: boolean) {
250 page.goto(`/structural-sharing/${enabled}/?foo=f1&bar=b1`)
251 await expect(page.getByTestId('enabled')).toHaveText(JSON.stringify(enabled))
252​
253 async function checkSearch({ foo, bar }: { foo: string; bar: string }) {
254 expect(page.url().endsWith(`?foo=${foo}&bar=${bar}`)).toBe(true)
255 const expectedSearch = JSON.stringify({ values: [foo, bar] })
256 await expect(page.getByTestId('search-via-hook')).toHaveText(expectedSearch)
257 await expect(page.getByTestId('search-via-route-hook')).toHaveText(
258 expectedSearch,
259 )
260 await expect(page.getByTestId('search-via-route-api-hook')).toHaveText(
261 expectedSearch,
262 )
263 }
264​
265 await checkSearch({ bar: 'b1', foo: 'f1' })
266 await page.getByTestId('link').click()
267 await checkSearch({ bar: 'b2', foo: 'f2' })
269​
270test('structural sharing disabled', async ({ page }) => {
271 await structuralSharingTest(page, false)
272 expect(await getRenderCount(page)).toBe(2)
273 await page.getByTestId('link').click()
274 expect(await getRenderCount(page)).toBeGreaterThan(2)
275})
276​
277test('structural sharing enabled', async ({ page }) => {
278 await structuralSharingTest(page, true)
279 expect(await getRenderCount(page)).toBe(2)
280 await page.getByTestId('link').click()
281 expect(await getRenderCount(page)).toBe(2)
282})
283​
284test('Should change title on client side navigation', async ({ page }) => {
285 await page.goto('/')
286​
287 await page.getByRole('link', { name: 'Posts' }).click()
288​
289 await expect(page).toHaveTitle('Posts page')
290})
291​
292test('Should change post navigating back and forth', async ({ page }) => {
293 await page.goto('/posts/1')
294 await page.getByRole('link', { name: 'sunt aut facere repe' }).click()
295​
296 await page.getByRole('link', { name: 'qui est esse' }).click()
297 await expect(page.getByTestId('post-title')).toContainText('qui est esse')
298​
299 await page.getByRole('link', { name: 'sunt aut facere repe' }).click()
300 await expect(page.getByTestId('post-title')).toContainText('sunt aut facere')
301})
302​
303test('Should not remount deps when remountDeps does not change ', async ({
304 page,
305}) => {
306 await page.goto('/notRemountDeps')
307 await expect(page.getByTestId('component-mounts')).toContainText(
308 'Page component mounts: 1',
309 )
310 await page.getByRole('button', { name: 'Regenerate search param' }).click()
311 await expect(page.getByTestId('component-mounts')).toContainText(
312 'Page component mounts: 1',
313 )
314 await page.getByRole('button', { name: 'Regenerate search param' }).click()
315 await expect(page.getByTestId('component-mounts')).toContainText(
316 'Page component mounts: 1',
317 )
318})
319​
320test('Should remount deps when remountDeps does change ', async ({ page }) => {
321 await page.goto('/remountDeps')
322 await expect(page.getByTestId('component-mounts')).toContainText(
323 'Page component mounts: 1',
324 )
325 await page.getByRole('button', { name: 'Regenerate search param' }).click()
326 await expect(page.getByTestId('component-mounts')).toContainText(
327 'Page component mounts: 2',
328 )
329 await page.getByRole('button', { name: 'Regenerate search param' }).click()
330 await expect(page.getByTestId('component-mounts')).toContainText(
331 'Page component mounts: 3',
332 )
333})
334​
335test.describe('Unicode route rendering', () => {
336 test('should render non-latin route correctly', async ({ page, baseURL }) => {
337 await page.goto('/λŒ€ν•œλ―Όκ΅­')
338​
339 await expect(page.locator('body')).toContainText('Hello "/λŒ€ν•œλ―Όκ΅­"!')
340​
341 expect(page.url()).toBe(`${baseURL}/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD`)
342 })
343})
344​
345test.describe('Pathless layout routes', () => {
346 test('direct navigation to pathless layout route renders correctly', async ({
347 page,
348 }) => {
349 await page.goto('/pathless-layout')
350 await expect(page.getByTestId('pathless-layout-header')).toContainText(
351 'Pathless Layout Section',
352 )
353 await expect(page.getByTestId('pathless-layout-wrapper')).toContainText(
354 'Pathless Layout Wrapper',
355 )
356 await expect(page.getByTestId('pathless-layout-index')).toContainText(
357 'Pathless Layout Index',
358 )
359 })
360​
361 test('client-side navigation to pathless layout route', async ({ page }) => {
362 await page.goto('/')
363 await page.getByTestId('link-to-pathless-layout').click()
364 await expect(page.getByTestId('pathless-layout-header')).toContainText(
365 'Pathless Layout Section',
366 )
367 await expect(page.getByTestId('pathless-layout-wrapper')).toContainText(
368 'Pathless Layout Wrapper',
369 )
370 })
371​
372 test('navigation within pathless layout preserves layout', async ({
373 page,
374 }) => {
375 await page.goto('/pathless-layout')
376 await page.getByTestId('link-to-child').click()
377 await expect(page.getByTestId('pathless-layout-header')).toContainText(
378 'Pathless Layout Section',
379 )
380 await expect(page.getByTestId('pathless-layout-wrapper')).toContainText(
381 'Pathless Layout Wrapper',
382 )
383 await expect(page.getByTestId('pathless-layout-child')).toContainText(
384 'Pathless Layout Child Route',
385 )
386 })
387​
388 test('direct navigation to child of pathless layout', async ({ page }) => {
389 await page.goto('/pathless-layout/child')
390 await expect(page.getByTestId('pathless-layout-header')).toContainText(
391 'Pathless Layout Section',
392 )
393 await expect(page.getByTestId('pathless-layout-wrapper')).toContainText(
394 'Pathless Layout Wrapper',
395 )
396 await expect(page.getByTestId('pathless-layout-child')).toContainText(
397 'Pathless Layout Child Route',
398 )
399 })
400​
401 test('navigating to non-existent route under pathless layout shows not found', async ({
402 page,
403 }) => {
404 await page.goto('/pathless-layout/does-not-exist')
405 await expect(page.getByTestId('pathless-layout-not-found')).toContainText(
406 'Not Found in Pathless Layout',
407 )
408 await expect(page.locator('body')).toContainText('Not Found')
409 })
410})