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