Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 9eac5ba

Browse files
author
Brandon Carroll
committed
fix: change the way mocks work
1 parent 55ce35c commit 9eac5ba

File tree

5 files changed

+47
-34
lines changed

5 files changed

+47
-34
lines changed

src/preset/mock-components.js renamed to src/preset/mock-component.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
'use strict';
2-
31
import React from 'react';
42

5-
import { getConfig } from '../lib';
3+
import { mockNativeMethods } from './mock-native-methods';
64

75
function mockComponent(component, path = component) {
86
const RealComponent = jest.requireActual(path);
9-
const { mockNativeMethods } = jest.requireActual('./mock-native-methods');
107

118
const displayName =
129
RealComponent.displayName ||
@@ -49,18 +46,4 @@ function mockComponent(component, path = component) {
4946
return Component;
5047
}
5148

52-
getConfig('coreComponents').forEach(component => {
53-
try {
54-
jest.doMock(component, () => mockComponent(component));
55-
} catch (e) {
56-
//
57-
}
58-
});
59-
60-
jest.doMock('Picker', () => {
61-
const Picker = mockComponent('Picker');
62-
Picker.Item = ({ children, ...props }) => React.createElement('Picker.Item', props, children);
63-
return Picker;
64-
});
65-
6649
export { mockComponent };

src/preset/mock-modules.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { getConfig } from '../lib';
2+
import { mockComponent } from './mock-component';
3+
import { mockNativeMethods } from './mock-native-methods';
4+
5+
// Un-mock the react-native components so we can do it ourselves
6+
getConfig('coreComponents').forEach(component => {
7+
try {
8+
// try to un-mock the component
9+
jest.unmock(component);
10+
} catch (e) {
11+
// do nothing if we can't
12+
}
13+
});
14+
15+
// Un-mock ReactNative so we can hide annoying `console.warn`s
16+
jest.unmock('ReactNative');
17+
18+
// Mock the components we want mocked
19+
getConfig('coreComponents').forEach(component => {
20+
try {
21+
jest.doMock(component, () => mockComponent(component));
22+
} catch (e) {
23+
//
24+
}
25+
});
26+
27+
// Mock the Picker one-off because it's kinda weird
28+
jest.doMock('Picker', () => {
29+
const React = jest.requireActual('react');
30+
const Picker = mockComponent('Picker');
31+
Picker.Item = ({ children, ...props }) => React.createElement('Picker.Item', props, children);
32+
return Picker;
33+
});
34+
35+
// Re-mock ReactNative with native methods mocked
36+
jest.mock('NativeAnimatedHelper').mock('ReactNative', () => {
37+
const ReactNative = jest.requireActual('ReactNative');
38+
const NativeMethodsMixin =
39+
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.NativeMethodsMixin;
40+
41+
Object.assign(NativeMethodsMixin, mockNativeMethods);
42+
Object.assign(ReactNative.NativeComponent.prototype, mockNativeMethods);
43+
44+
return ReactNative;
45+
});

src/preset/mock-native-methods.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const mockNativeMethods = {
42
measure: jest.fn(),
53
measureInWindow: jest.fn(),

src/preset/setup.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
import './configure';
33

44
// Un-mock the things that we'll be mocking
5-
import './unmock';
6-
7-
// Mock lean core components
8-
import './mock-components';
5+
import './mock-modules';

src/preset/unmock.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)