Files
react-native/Libraries/Utilities/__tests__/ReactNativeTestTools-test.js
T
Pieter De Baets 168ec03b0f Move ReactNativeTestTools-test to github
Summary: Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D36546209

fbshipit-source-id: 33f7e896e2a3b77dfa1384d0a70ef684dd57a445
2022-05-23 06:15:08 -07:00

104 lines
3.2 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @emails oncall+react_native
*/
import * as React from 'react';
import {byTestID, byTextMatching, enter, tap} from '../ReactNativeTestTools';
import {Text, TextInput, TouchableWithoutFeedback, View} from 'react-native';
const ReactTestRenderer = require('react-test-renderer');
describe('ReactNativeTestTools', () => {
const ExampleNull = () => null;
it('matches byTestID()', () => {
const renderSimple = ReactTestRenderer.create(<ExampleNull testID="foo" />);
const foo = renderSimple.root.find(byTestID('foo'));
expect(foo).toEqual(renderSimple.root);
const renderNested = ReactTestRenderer.create(
<View testID="bar">
<View testID="baz" />
<ExampleNull testID="bing">
<View testID="impossible" />
</ExampleNull>
</View>,
);
const bar = renderNested.root.find(byTestID('bar'));
const baz = renderNested.root.find(byTestID('baz'));
const bing = renderNested.root.find(byTestID('bing'));
expect(bar).toEqual(renderNested.root);
expect(baz.type).toEqual(View);
expect(bing.type).toEqual(ExampleNull);
expect(renderNested.root.findAll(byTestID('impossible'))).toHaveLength(0);
});
it('matches byTextMatching()', () => {
const hasFooText = byTextMatching(/foo/);
const hasBarText = byTextMatching(/bar/);
const renderSimple = ReactTestRenderer.create(<Text>foobarbaz</Text>);
const foo = renderSimple.root.find(hasFooText);
expect(foo).toEqual(renderSimple.root);
expect(foo.type).toEqual(Text);
expect(foo.props.children).toEqual('foobarbaz');
const renderNested = ReactTestRenderer.create(
<Text>
foozy
<Text>woobar</Text>
fobarof
<Text>barwoo</Text>
woofoo
</Text>,
);
const bar = renderNested.root.find(hasBarText);
const barAll = renderNested.root.findAll(hasBarText);
const barAllShallow = renderNested.root.findAll(hasBarText, {deep: false});
expect(bar.props.children.toString()).toEqual(
'foozy,[object Object],fobarof,[object Object],woofoo',
);
expect(barAll).toHaveLength(6);
expect(barAllShallow).toHaveLength(1);
});
it('interacts via tap()', () => {
const touchFn = jest.fn();
const renderTouch = ReactTestRenderer.create(
<TouchableWithoutFeedback onPress={touchFn}>
<ExampleNull />
</TouchableWithoutFeedback>,
);
tap(renderTouch.root);
expect(touchFn).toBeCalled();
// example of tapping <Text />
const textFn = jest.fn();
const renderText = ReactTestRenderer.create(<Text onPress={textFn} />);
tap(renderText.root);
expect(textFn).toBeCalled();
});
it('interacts via enter()', () => {
const changeFn = jest.fn();
const changeTextFn = jest.fn();
const renderInput = ReactTestRenderer.create(
<View>
<TextInput onChange={changeFn} onChangeText={changeTextFn} />
</View>,
);
const text = 'test message text';
enter(renderInput.root, text);
expect(changeFn).toBeCalled();
expect(changeTextFn).toBeCalledWith(text);
});
});