forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (33 loc) · 1.01 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
import ReactDOM from 'react-dom'
import {Simulate} from 'react-dom/test-utils'
// we may expose this eventually
function select(id) {
return `[data-testid="${id}"]`
}
// we may expose this eventually
function queryByTestId(div, id) {
return div.querySelector(select(id))
}
// we may expose this eventually
function getByTestId(div, id) {
const el = queryByTestId(div, id)
if (!el) {
throw new Error(`Unable to find element by ${select(id)}`)
}
return el
}
function render(ui, {container = document.createElement('div')} = {}) {
ReactDOM.render(ui, container)
return {
container,
unmount: () => ReactDOM.unmountComponentAtNode(container),
queryByTestId: queryByTestId.bind(null, container),
getByTestId: getByTestId.bind(null, container),
}
}
// this returns a new promise and is just a simple way to
// wait until the next tick so resolved promises chains will continue
function flushPromises() {
return new Promise(resolve => setImmediate(resolve))
}
export {render, flushPromises, Simulate}