-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.setup.js
45 lines (34 loc) · 1.11 KB
/
jest.setup.js
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
const { fireEvent } = require('@testing-library/dom')
global.window.scrollTo = async (scrollX, scrollY) => {
const target = { scrollX, scrollY }
return fireEvent.scroll(window, { target })
}
// Simulate window resize event
const resizeEvent = document.createEvent('Event')
resizeEvent.initEvent('resize', true, true)
global.window.resizeTo = (width, height) => {
global.window.innerWidth = width || global.window.innerWidth
global.window.innerHeight = height || global.window.innerHeight
global.window.dispatchEvent(resizeEvent)
}
global.beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
// Set default viewport size
window.resizeTo(1024, 768)
// Mock the scroll height of the body
Object.defineProperty(document.body, 'scrollHeight', {
writable: true, value: 2000
})
// Mock the document.hidden property
Object.defineProperty(document, 'hidden', {
writable: true, value: false
})
// Mock the document.readyState property
Object.defineProperty(document, 'readyState', {
writable: true, value: 'loading'
})
})
global.afterEach(() => {
jest.runAllTimers();
})