Skip to content

Commit 5d4c542

Browse files
yungstersfacebook-github-bot
authored andcommittedMay 9, 2018
RN: Refactor MockNativeMethods in Jest
Reviewed By: sahrens Differential Revision: D7917498 fbshipit-source-id: 97636080588bf2641a56256688cb0f2ec81ae463
1 parent 67dbcbd commit 5d4c542

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed
 

‎jest/MockNativeMethods.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
const mockNativeFunction = methodName => {
13+
let warned = false;
14+
return function() {
15+
if (warned) {
16+
return;
17+
}
18+
warned = true;
19+
console.warn(
20+
'Calling .' +
21+
methodName +
22+
'() in the test renderer environment is not supported. Instead, mock ' +
23+
'out your components that use findNodeHandle with replacements that ' +
24+
"don't rely on the native environment.",
25+
);
26+
};
27+
};
28+
29+
const MockNativeMethods = {
30+
measure: mockNativeFunction('measure'),
31+
measureInWindow: mockNativeFunction('measureInWindow'),
32+
measureLayout: mockNativeFunction('measureLayout'),
33+
setNativeProps: mockNativeFunction('setNativeProps'),
34+
focus: mockNativeFunction('focus'),
35+
blur: mockNativeFunction('blur'),
36+
};
37+
38+
module.exports = MockNativeMethods;

‎jest/setup.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
'use strict';
88

9+
const MockNativeMethods = require.requireActual('./MockNativeMethods');
910
const mockComponent = require.requireActual('./mockComponent');
1011

1112
require.requireActual('../Libraries/polyfills/babelHelpers.js');
@@ -81,33 +82,9 @@ jest
8182
const NativeMethodsMixin =
8283
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.NativeMethodsMixin;
8384

84-
const mockFunction = (key) => {
85-
let warned = false;
86-
return function() {
87-
if (warned) {
88-
return;
89-
}
90-
warned = true;
91-
console.warn(
92-
'Calling .' + key + '() in the test renderer environment is not ' +
93-
'supported. Instead, mock out your components that use ' +
94-
'findNodeHandle with replacements that don\'t rely on the ' +
95-
'native environment.',
96-
);
97-
};
98-
};
85+
Object.assign(NativeMethodsMixin, MockNativeMethods);
86+
Object.assign(ReactNative.NativeComponent.prototype, MockNativeMethods);
9987

100-
[
101-
'measure',
102-
'measureInWindow',
103-
'measureLayout',
104-
'setNativeProps',
105-
'focus',
106-
'blur',
107-
].forEach((key) => {
108-
NativeMethodsMixin[key] = mockFunction(key);
109-
ReactNative.NativeComponent.prototype[key] = mockFunction(key);
110-
});
11188
return ReactNative;
11289
})
11390
.mock('ensureComponentIsNative', () => () => true);

0 commit comments

Comments
 (0)
Please sign in to comment.