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

scroll-into-view.spec.ts

214 lines6.7 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​
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})