Skip to content

Commit a2b699d

Browse files
bcarroll22facebook-github-bot
authored andcommitted
change jest native method mocks to jest functions (facebook#24337)
Summary: Currently calling native methods on internal react native components throw a warning. I believe this is problematic because _users_ aren't calling native methods on internal components, the _component_ is making the call. So for instance, if I unmount a component that has a form with a few uses of `TextInput`, which is a perfectly valid test case, my test output will be full of warnings that I can't call `.blur()` in the test renderer environment. That's very misleading, because I didn't, the internal component did. In fact, as far as I can tell, there's not really even anything I can do to stop that call or use the output from it, its all internal. `TextInput` is a black box, and 99% of users writing tests probably won't even know it calls `.blur()` under the hood on unmount. I want to change these to `jest.fn()` because I think this eliminates a lot of chatter in test output, but also doesn't send users down a rabbit hole of trying to find workarounds that may involve filtering console output, which could potentially lead them to inadvertently filter out real warnings that they should see. So I'm willing to change the implementation of how I did this, but I don't think its right to warn users that they called a native method when they didn't. If they build a component that calls these methods, I believe it's on them to do something similar to this, and maybe we can make this exposed as a helper that can be used for third party component mocks? [General] [Changes] - Changed MockNativeMethods for core components to `jest.fn()` instead of function that warns about calling native methods. Pull Request resolved: facebook#24337 Differential Revision: D14822126 Pulled By: cpojer fbshipit-source-id: 2199b8c8da8e289d38823bdcd2c43c82f3f635c9
1 parent 12c9712 commit a2b699d

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

Libraries/Components/TextInput/__tests__/TextInput-test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ describe('TextInput tests', () => {
5050
it('has expected instance functions', () => {
5151
expect(input.instance.isFocused).toBeInstanceOf(Function); // Would have prevented S168585
5252
expect(input.instance.clear).toBeInstanceOf(Function);
53-
expect(input.instance.focus).toBeInstanceOf(Function);
54-
expect(input.instance.blur).toBeInstanceOf(Function);
55-
expect(input.instance.setNativeProps).toBeInstanceOf(Function);
56-
expect(input.instance.measure).toBeInstanceOf(Function);
57-
expect(input.instance.measureInWindow).toBeInstanceOf(Function);
58-
expect(input.instance.measureLayout).toBeInstanceOf(Function);
53+
expect(input.instance.focus).toBeInstanceOf(jest.fn().constructor);
54+
expect(input.instance.blur).toBeInstanceOf(jest.fn().constructor);
55+
expect(input.instance.setNativeProps).toBeInstanceOf(jest.fn().constructor);
56+
expect(input.instance.measure).toBeInstanceOf(jest.fn().constructor);
57+
expect(input.instance.measureInWindow).toBeInstanceOf(
58+
jest.fn().constructor,
59+
);
60+
expect(input.instance.measureLayout).toBeInstanceOf(jest.fn().constructor);
5961
});
6062
it('calls onChange callbacks', () => {
6163
expect(input.props.value).toBe(initialValue);

jest/MockNativeMethods.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,13 @@
99

1010
'use strict';
1111

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-
2912
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'),
13+
measure: jest.fn(),
14+
measureInWindow: jest.fn(),
15+
measureLayout: jest.fn(),
16+
setNativeProps: jest.fn(),
17+
focus: jest.fn(),
18+
blur: jest.fn(),
3619
};
3720

3821
module.exports = MockNativeMethods;

0 commit comments

Comments
 (0)