-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup-jest.js
60 lines (49 loc) · 1.59 KB
/
setup-jest.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// A solution for React 16 complaining of missing rAF.
import 'intersection-observer'
const jsdom = require('jsdom')
const { JSDOM } = jsdom
const dom = new JSDOM(
'<!DOCTYPE html><html><head><style>body:after{content:"root";}</style></head><body></body></html>'
)
global.window = dom.window
global.document = dom.window.document
global.window.innerWidth = 1024
// 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.requestAnimationFrame = function(callback) {
setTimeout(callback, 0)
}
// global.document.createElement = jest.fn(() => ({
// style: {
// transition: 'opacity 300ms ease',
// animation: 'test 300ms'
// }
// }))
Object.defineProperty(document, 'currentStyle', {
value: document.createElement('style')
})
if (window.Element && !Element.prototype.closest) {
Element.prototype.closest = function(s) {
const matches = (this.document || this.ownerDocument).querySelectorAll(s)
let i
let el = this
// eslint-disable-next-line no-cond-assign
do {
i = matches.length
while (--i >= 0 && matches.item(i) !== el) {} // eslint-disable-line
} while (i < 0 && (el = el.parentElement))
return el
}
}
Object.defineProperty(window, 'matchMedia', {
value: jest.fn(a => ({
matches: global.innerWidth > parseInt(a.match(/\d+/)[0], 10)
}))
})
window.log = console.log // eslint-disable-line