forked from testing-library/native-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-modules.js
86 lines (74 loc) · 2.76 KB
/
mock-modules.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 { getConfig } from '../lib';
import { mockComponent } from './mock-component';
import { mockScrollView } from './mock-scroll-view';
import RefreshControlMock from './mock-refresh-control';
// Un-mock the react-native components so we can do it ourselves
getConfig('coreComponents').forEach(component => {
try {
// try to un-mock the component
jest.unmock(component);
} catch (e) {
// do nothing if we can't
}
});
// Un-mock ReactNative so we can hide annoying `console.warn`s
jest.unmock('react-native/Libraries/Renderer/shims/ReactNative');
// Mock the components we want mocked
getConfig('coreComponents').forEach(component => {
try {
jest.doMock(component, () => mockComponent(component));
} catch (e) {}
});
// Mock the Picker one-off because it's kinda weird
jest.doMock('react-native/Libraries/Components/Picker/Picker', () => {
const React = jest.requireActual('react');
const Picker = mockComponent('react-native/Libraries/Components/Picker/Picker');
Picker.Item = ({ children, ...props }) => React.createElement('Picker.Item', props, children);
return Picker;
});
// Mock some other tricky ones
jest.doMock('react-native/Libraries/Components/ScrollView/ScrollView', () => {
const baseComponent = mockComponent('react-native/Libraries/Components/ScrollView/ScrollView', {
instanceMethods: {
getScrollResponder: jest.fn(),
getScrollableNode: jest.fn(),
getInnerViewNode: jest.fn(),
getInnerViewRef: jest.fn(),
getNativeScrollRef: jest.fn(),
scrollTo: jest.fn(),
scrollToEnd: jest.fn(),
flashScrollIndicators: jest.fn(),
scrollResponderZoomTo: jest.fn(),
scrollResponderScrollNativeHandleToKeyboard: jest.fn(),
},
});
return mockScrollView(baseComponent);
});
jest.doMock(
'react-native/Libraries/Components/RefreshControl/RefreshControl',
() => RefreshControlMock,
);
jest.doMock('react-native/Libraries/Components/TextInput/TextInput', () =>
mockComponent('react-native/Libraries/Components/TextInput/TextInput', {
instanceMethods: {
isFocused: jest.fn(),
clear: jest.fn(),
getNativeRef: jest.fn(),
},
}),
);
jest.doMock('react-native/Libraries/Components/Touchable/TouchableHighlight', () =>
mockComponent('react-native/Libraries/Components/Touchable/TouchableHighlight', {
displayName: 'TouchableHighlight',
}),
);
jest.doMock('react-native/Libraries/Components/Touchable/TouchableOpacity', () =>
mockComponent('react-native/Libraries/Components/Touchable/TouchableOpacity', {
displayName: 'TouchableOpacity',
}),
);
// Re-mock ReactNative
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
jest.mock('react-native/Libraries/Renderer/shims/ReactNative');
// Mock LogBox
jest.mock('react-native/Libraries/LogBox/LogBox');