-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.js
46 lines (40 loc) · 1.2 KB
/
index.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
import ReactDOM from 'react-dom'
import {Simulate} from 'react-dom/test-utils'
import {bindElementToQueries, wait, fireEvent, waitForElement} from 'dom-testing-library'
function render(ui, {container = document.createElement('div')} = {}) {
ReactDOM.render(ui, container)
return {
container,
unmount: () => ReactDOM.unmountComponentAtNode(container),
...bindElementToQueries(container),
}
}
const mountedContainers = new Set()
function renderIntoDocument(ui) {
const container = document.body.appendChild(document.createElement('div'))
mountedContainers.add(container)
return render(ui, {container})
}
function cleanup() {
mountedContainers.forEach(container => {
document.body.removeChild(container)
ReactDOM.unmountComponentAtNode(container)
mountedContainers.delete(container)
})
}
// fallback to synthetic events for React events that the DOM doesn't support
const syntheticEvents = ['change', 'select', 'mouseEnter', 'mouseLeave']
syntheticEvents.forEach(eventName => {
document.addEventListener(eventName.toLowerCase(), e => {
Simulate[eventName](e.target, e)
})
})
export {
render,
Simulate,
wait,
waitForElement,
fireEvent,
renderIntoDocument,
cleanup,
}