forked from testing-library/native-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (72 loc) · 1.97 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React from 'react';
import TR from 'react-test-renderer';
import AppContainer from 'react-native/Libraries/ReactNative/AppContainer';
import {
toJSON,
fireEvent as rntlFireEvent,
getQueriesForElement,
NativeTestEvent,
prettyPrint,
proxyElement,
} from './lib';
import act from './act-compat';
const renderers = new Set();
function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
const { debug, ...rest } = options;
const wrapUiIfNeeded = innerElement =>
WrapperComponent ? (
<AppContainer>
<WrapperComponent>{innerElement}</WrapperComponent>
</AppContainer>
) : (
<AppContainer>{innerElement}</AppContainer>
);
let testRenderer;
act(() => {
testRenderer = TR.create(wrapUiIfNeeded(ui), rest);
});
renderers.add(testRenderer);
const wrappers = proxyElement(testRenderer.root).findAll(n => n.type === 'View');
const baseElement = wrappers[0];
const container = wrappers[1]; // Includes only your render
return {
baseElement,
container,
debug: (el = baseElement) => console.log(prettyPrint(el, undefined, { debug })),
unmount: () => testRenderer.unmount(),
rerender: rerenderUi => {
act(() => {
testRenderer.update(wrapUiIfNeeded(rerenderUi));
});
},
asJSON: () => {
return toJSON(container);
},
...getQueriesForElement(baseElement, queries),
};
}
function cleanup() {
renderers.forEach(cleanupRenderer);
}
function cleanupRenderer(renderer) {
renderer.unmount();
renderers.delete(renderer);
}
function fireEvent(...args) {
let returnValue;
act(() => {
returnValue = rntlFireEvent(...args);
});
return returnValue;
}
Object.keys(rntlFireEvent).forEach(typeArg => {
fireEvent[typeArg] = (...args) => {
let returnValue;
act(() => {
returnValue = rntlFireEvent[typeArg](...args);
});
return returnValue;
};
});
export * from './lib';
export { act, cleanup, fireEvent, render, NativeTestEvent };