/** * 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(); const foo = renderSimple.root.find(byTestID('foo')); expect(foo).toEqual(renderSimple.root); const renderNested = ReactTestRenderer.create( , ); 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(foobarbaz); 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( foozy woobar fobarof barwoo woofoo , ); 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( , ); tap(renderTouch.root); expect(touchFn).toBeCalled(); // example of tapping const textFn = jest.fn(); const renderText = ReactTestRenderer.create(); tap(renderText.root); expect(textFn).toBeCalled(); }); it('interacts via enter()', () => { const changeFn = jest.fn(); const changeTextFn = jest.fn(); const renderInput = ReactTestRenderer.create( , ); const text = 'test message text'; enter(renderInput.root, text); expect(changeFn).toBeCalled(); expect(changeTextFn).toBeCalledWith(text); }); });