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

scroll-into-view.spec.ts

229 lines7.2 KB

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