Skip to content

Commit

Permalink
change jest native method mocks to jest functions (#24337)
Browse files Browse the repository at this point in the history
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: #24337

Differential Revision: D14822126

Pulled By: cpojer

fbshipit-source-id: 2199b8c8da8e289d38823bdcd2c43c82f3f635c9
  • Loading branch information
bcarroll22 authored and facebook-github-bot committed Apr 7, 2019
1 parent 12c9712 commit a2b699d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
14 changes: 8 additions & 6 deletions Libraries/Components/TextInput/__tests__/TextInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ describe('TextInput tests', () => {
it('has expected instance functions', () => {
expect(input.instance.isFocused).toBeInstanceOf(Function); // Would have prevented S168585
expect(input.instance.clear).toBeInstanceOf(Function);
expect(input.instance.focus).toBeInstanceOf(Function);
expect(input.instance.blur).toBeInstanceOf(Function);
expect(input.instance.setNativeProps).toBeInstanceOf(Function);
expect(input.instance.measure).toBeInstanceOf(Function);
expect(input.instance.measureInWindow).toBeInstanceOf(Function);
expect(input.instance.measureLayout).toBeInstanceOf(Function);
expect(input.instance.focus).toBeInstanceOf(jest.fn().constructor);
expect(input.instance.blur).toBeInstanceOf(jest.fn().constructor);
expect(input.instance.setNativeProps).toBeInstanceOf(jest.fn().constructor);
expect(input.instance.measure).toBeInstanceOf(jest.fn().constructor);
expect(input.instance.measureInWindow).toBeInstanceOf(
jest.fn().constructor,
);
expect(input.instance.measureLayout).toBeInstanceOf(jest.fn().constructor);
});
it('calls onChange callbacks', () => {
expect(input.props.value).toBe(initialValue);
Expand Down
29 changes: 6 additions & 23 deletions jest/MockNativeMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,13 @@

'use strict';

const mockNativeFunction = methodName => {
let warned = false;
return function() {
if (warned) {
return;
}
warned = true;
console.warn(
'Calling .' +
methodName +
'() in the test renderer environment is not supported. Instead, mock ' +
'out your components that use findNodeHandle with replacements that ' +
"don't rely on the native environment.",
);
};
};

const MockNativeMethods = {
measure: mockNativeFunction('measure'),
measureInWindow: mockNativeFunction('measureInWindow'),
measureLayout: mockNativeFunction('measureLayout'),
setNativeProps: mockNativeFunction('setNativeProps'),
focus: mockNativeFunction('focus'),
blur: mockNativeFunction('blur'),
measure: jest.fn(),
measureInWindow: jest.fn(),
measureLayout: jest.fn(),
setNativeProps: jest.fn(),
focus: jest.fn(),
blur: jest.fn(),
};

module.exports = MockNativeMethods;

0 comments on commit a2b699d

Please sign in to comment.