This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
68 lines (59 loc) 路 1.71 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
import React from 'react';
import TR from 'react-test-renderer';
import AppContainer from 'react-native/Libraries/ReactNative/AppContainer';
import {
fireEvent as rntlFireEvent,
getQueriesForElement,
NativeEvent,
prettyPrint,
proxyUnsafeProperties as proxy,
} from './lib';
import './preset/serializer';
import act from './act-compat';
function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
const wrapUiIfNeeded = innerElement =>
WrapperComponent ? (
<AppContainer>
<WrapperComponent>{innerElement}</WrapperComponent>
</AppContainer>
) : (
<AppContainer>{innerElement}</AppContainer>
);
let testRenderer;
act(() => {
testRenderer = TR.create(wrapUiIfNeeded(ui), options);
});
const wrappers = proxy(testRenderer.root).findAll(n => n.getProp('pointerEvents') === 'box-none');
const baseElement = wrappers[0]; // Includes YellowBox and your render
const container = wrappers[1]; // Includes only your render
return {
baseElement,
container,
debug: (el = baseElement) => console.log(prettyPrint(el)),
unmount: () => testRenderer.unmount(),
rerender: rerenderUi => {
act(() => {
testRenderer.update(wrapUiIfNeeded(rerenderUi));
});
},
...getQueriesForElement(baseElement, queries),
};
}
function fireEvent(...args) {
let returnValue;
act(() => {
returnValue = rntlFireEvent(...args);
});
return returnValue;
}
Object.keys(rntlFireEvent).forEach(key => {
fireEvent[key] = (...args) => {
let returnValue;
act(() => {
returnValue = rntlFireEvent[key](...args);
});
return returnValue;
};
});
export * from './lib';
export { act, fireEvent, render, NativeEvent };