Open History to see the last commit
1import { expect } from '@playwright/test'
2import { apiTest as test } from '@tanstack/router-e2e-utils'
3import type { Page } from '@playwright/test'
4β
5const anchors = {
6 defaultAnchor: 'default-anchor',
7 noScrollIntoView: 'false-anchor',
8 smoothScroll: 'smooth-scroll',
9} as const
10β
11const formTestIds = {
12 targetAnchor: 'hash-select',
13 scrollIntoView: 'with-scroll',
14 behaviorSelect: 'behavior-select',
15 blockSelect: 'block-select',
16 inlineSelect: 'inline-select',
17 navigateButton: 'navigate-button',
18}
19β
20const shownSuffix = '(shown)'
21β
22const activeClass = 'font-bold active'
23β
24function getAnchorTarget(page: Page, anchor: string) {
25 return page.getByTestId(`heading-${anchor}`)
26}
27β
28function getAnchorLink(page: Page, anchor: string) {
29 return page.getByTestId(`link-${anchor}`)
30}
31β
32test.beforeEach(async ({ page }) => {
33 await page.goto('/anchor')
34})
35β
36// Testing the `Link` component with the `hashScrollIntoView` prop
37test('Navigating via anchor `Link` with default hash scrolling behavior', async ({
38 page,
39}) => {
40 await expect(getAnchorTarget(page, anchors.defaultAnchor)).not.toContainText(
41 shownSuffix,
42 )
43β
44 await getAnchorLink(page, anchors.defaultAnchor).click()
45β
46 await expect(getAnchorTarget(page, anchors.defaultAnchor)).toBeVisible()
47 await expect(getAnchorTarget(page, anchors.defaultAnchor)).toContainText(
48 shownSuffix,
49 )
50β
51 await expect(getAnchorLink(page, anchors.defaultAnchor)).toHaveClass(
52 activeClass,
53 )
54})
55β
56test('Navigating via anchor `Link` with hash scrolling disabled', async ({
57 page,
58}) => {
59 const initialScrollPosition = await page.evaluate(() => window.scrollY)
60β
61 await expect(
62 getAnchorTarget(page, anchors.noScrollIntoView),
63 ).not.toContainText(shownSuffix)
64β
65 await getAnchorLink(page, anchors.noScrollIntoView).click()
66β
67 // The active anchor should have updated
68 await expect(getAnchorLink(page, anchors.noScrollIntoView)).toHaveClass(
69 activeClass,
70 )
71β
72 // The anchor should not have been visible, because the scroll should not have been activated
73 await expect(getAnchorTarget(page, anchors.defaultAnchor)).not.toContainText(
74 shownSuffix,
75 )
76β
77 // Expect the same scroll position as before
78 expect(await page.evaluate(() => window.scrollY)).toBe(initialScrollPosition)
79})
80β
81test('Navigating via anchor `Link` with smooth hash scrolling behavior', async ({
82 page,
83}) => {
84 await expect(getAnchorTarget(page, anchors.smoothScroll)).not.toContainText(
85 shownSuffix,
86 )
87β
88 await getAnchorLink(page, anchors.smoothScroll).click()
89 await expect(getAnchorTarget(page, anchors.smoothScroll)).toBeVisible()
90β
91 // Smooth scrolling should activate the IntersectionObserver on all headings, making them all render "(shown)"
92 await expect(getAnchorTarget(page, anchors.defaultAnchor)).toContainText(
93 shownSuffix,
94 )
95 await expect(getAnchorTarget(page, anchors.noScrollIntoView)).toContainText(
96 shownSuffix,
97 )
98 await expect(getAnchorTarget(page, anchors.smoothScroll)).toContainText(
99 shownSuffix,
100 )
101β
102 await expect(getAnchorLink(page, anchors.smoothScroll)).toHaveClass(
103 activeClass,
104 )
105})
106β
107// Testing the `useNavigate` hook with the `hashScrollIntoView` option
108test('Navigating via `useNavigate` with instant scroll behavior', async ({
109 page,
110}) => {
111 await expect(getAnchorTarget(page, anchors.smoothScroll)).not.toContainText(
112 shownSuffix,
113 )
114β
115 // Scroll to the last anchor instantly, should not activate Intersection Observers for the other anchors
116 await page.getByTestId(formTestIds.targetAnchor).selectOption('Smooth Scroll')
117 await page.getByTestId(formTestIds.scrollIntoView).check()
118 await page.getByTestId(formTestIds.behaviorSelect).selectOption('instant')
119 await page.getByTestId(formTestIds.blockSelect).selectOption('start')
120 await page.getByTestId(formTestIds.inlineSelect).selectOption('nearest')
121 await page.getByTestId(formTestIds.navigateButton).click()
122β
123 await expect(getAnchorTarget(page, anchors.defaultAnchor)).not.toContainText(
124 shownSuffix,
125 )
126β
127 await expect(
128 getAnchorTarget(page, anchors.noScrollIntoView),
129 ).not.toContainText(shownSuffix)
130β
131 await expect(getAnchorTarget(page, anchors.smoothScroll)).toContainText(
132 shownSuffix,
133 )
134β
135 await expect(getAnchorLink(page, anchors.smoothScroll)).toBeVisible()
136β
137 await expect(getAnchorLink(page, anchors.smoothScroll)).toHaveClass(
138 activeClass,
139 )
140})
141β
142test('Navigating via `useNavigate` with scrollIntoView disabled', async ({
143 page,
144}) => {
145 const initialScrollPosition = await page.evaluate(() => window.scrollY)
146β
147 await expect(getAnchorTarget(page, anchors.defaultAnchor)).not.toContainText(
148 shownSuffix,
149 )
150β
151 await expect(
152 getAnchorTarget(page, anchors.noScrollIntoView),
153 ).not.toContainText(shownSuffix)
154β
155 await expect(getAnchorTarget(page, anchors.smoothScroll)).not.toContainText(
156 shownSuffix,
157 )
158β
159 // Navigate to the last anchor, but with scrollIntoView disabled should not activate Intersection Observers for any anchors
160 await page.getByTestId(formTestIds.targetAnchor).selectOption('Smooth Scroll')
161 await page.getByTestId(formTestIds.scrollIntoView).uncheck()
162 await page.getByTestId(formTestIds.navigateButton).click()
163β
164 await expect(getAnchorTarget(page, anchors.defaultAnchor)).not.toContainText(
165 shownSuffix,
166 )
167β
168 await expect(
169 getAnchorTarget(page, anchors.noScrollIntoView),
170 ).not.toContainText(shownSuffix)
171β
172 await expect(getAnchorTarget(page, anchors.smoothScroll)).not.toContainText(
173 shownSuffix,
174 )
175β
176 await expect(getAnchorLink(page, anchors.smoothScroll)).toHaveClass(
177 activeClass,
178 )
179β
180 // Expect the same scroll position as before
181 expect(await page.evaluate(() => window.scrollY)).toBe(initialScrollPosition)
182})
183β
184test('Navigating via `useNavigate` with smooth scroll behavior', async ({
185 page,
186}) => {
187 await expect(getAnchorTarget(page, anchors.smoothScroll)).not.toContainText(
188 shownSuffix,
189 )
190β
191 // Scroll to the last anchor smoothly, should activate Intersection Observers for the other anchors, making them all render "(shown)"
192 await page.getByTestId(formTestIds.targetAnchor).selectOption('Smooth Scroll')
193 await page.getByTestId(formTestIds.scrollIntoView).check()
194 await page.getByTestId(formTestIds.behaviorSelect).selectOption('smooth')
195 await page.getByTestId(formTestIds.blockSelect).selectOption('start')
196 await page.getByTestId(formTestIds.inlineSelect).selectOption('nearest')
197 await page.getByTestId(formTestIds.navigateButton).click()
198β
199 await expect(getAnchorTarget(page, anchors.defaultAnchor)).toContainText(
200 shownSuffix,
201 )
202β
203 await expect(getAnchorTarget(page, anchors.noScrollIntoView)).toContainText(
204 shownSuffix,
205 )
206β
207 await expect(getAnchorTarget(page, anchors.smoothScroll)).toContainText(
208 shownSuffix,
209 )
210β
211 await expect(getAnchorLink(page, anchors.smoothScroll)).toHaveClass(
212 activeClass,
213 )
214})