/
index.test.ts
58 lines (46 loc) · 1.91 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
describe('router autoscrolling on navigation with css modules', () => {
const { next } = nextTestSetup({
files: __dirname,
})
type BrowserInterface = Awaited<ReturnType<(typeof next)['browser']>>
const getTopScroll = async (browser: BrowserInterface) =>
await browser.eval('document.documentElement.scrollTop')
const getLeftScroll = async (browser: BrowserInterface) =>
await browser.eval('document.documentElement.scrollLeft')
const waitForScrollToComplete = (
browser,
options: { x: number; y: number }
) =>
check(async () => {
const top = await getTopScroll(browser)
const left = await getLeftScroll(browser)
return top === options.y && left === options.x
? 'success'
: JSON.stringify({ top, left })
}, 'success')
const scrollTo = async (
browser: BrowserInterface,
options: { x: number; y: number }
) => {
await browser.eval(`window.scrollTo(${options.x}, ${options.y})`)
await waitForScrollToComplete(browser, options)
}
describe('vertical scroll when page imports css modules', () => {
it('should scroll to top of document when navigating between to pages without layout when', async () => {
const browser: BrowserInterface = await next.browser('/1')
await scrollTo(browser, { x: 0, y: 1000 })
expect(await getTopScroll(browser)).toBe(1000)
await browser.elementById('lower').click()
await waitForScrollToComplete(browser, { x: 0, y: 0 })
})
it('should scroll when clicking in JS', async () => {
const browser: BrowserInterface = await next.browser('/1')
await scrollTo(browser, { x: 0, y: 1000 })
expect(await getTopScroll(browser)).toBe(1000)
await browser.eval(() => document.getElementById('lower').click())
await waitForScrollToComplete(browser, { x: 0, y: 0 })
})
})
})