Spencer Ahrens a00940693e New TextInput-test that would have prevented S168585
Summary: Adds a basic test that would have prevented S168585. We should expand coverage of this and other components as well.

Reviewed By: TheSavior

Differential Revision: D13038064

fbshipit-source-id: 14cf4742efd53d7bca2a3f8d1c5c34ebc6227674
2018-11-15 20:51:52 -08:00

71 lines
2.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @format
* @flow-strict
*/
'use strict';
const React = require('React');
const ReactTestRenderer = require('react-test-renderer');
const TextInput = require('TextInput');
import Component from '@reactions/component';
const {enter} = require('ReactNativeTestTools');
jest.unmock('TextInput');
describe('TextInput tests', () => {
let input;
let onChangeListener;
let onChangeTextListener;
const initialValue = 'initialValue';
beforeEach(() => {
onChangeListener = jest.fn();
onChangeTextListener = jest.fn();
const renderTree = ReactTestRenderer.create(
<Component initialState={{text: initialValue}}>
{({setState, state}) => (
<TextInput
value={state.text}
onChangeText={text => {
onChangeTextListener(text);
setState({text});
}}
onChange={event => {
onChangeListener(event);
}}
/>
)}
</Component>,
);
input = renderTree.root.findByType(TextInput);
});
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);
});
it('calls onChange callbacks', () => {
expect(input.props.value).toBe(initialValue);
const message = 'This is a test message';
enter(input, message);
expect(input.props.value).toBe(message);
expect(onChangeTextListener).toHaveBeenCalledWith(message);
expect(onChangeListener).toHaveBeenCalledWith({
nativeEvent: {text: message},
});
});
});