Created
September 12, 2019 17:12
-
-
Save goodpic/029ccbd33abfa8b2f2287b3aeb7f29d3 to your computer and use it in GitHub Desktop.
Test React Hooks with react-native-testing-library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
import { fireEvent, render } from 'react-native-testing-library' | |
import { HookTest } from '../HookTest' | |
describe('Test Hooks', () => { | |
test('changeText', () => { | |
const { getByTestId, getByPlaceholder, queryByDisplayValue } = render( | |
<HookTest />) | |
const output = getByTestId('subText') | |
const input = getByPlaceholder(/Input Text/i) | |
expect(input).not.toBeNull() | |
expect(queryByDisplayValue('Default text')).not.toBeNull() | |
const testText = 'Changed text' | |
fireEvent(input, 'onChangeText', testText) | |
expect(queryByDisplayValue('Default text')).toBeNull() | |
expect(output.props.children).toBe(testText) | |
}) | |
test('onSubmitEditing', () => { | |
const { getByTestId, getByPlaceholder } = render( | |
<HookTest />) | |
const output = getByTestId('subText') | |
const input = getByPlaceholder(/Input Text/i) | |
const testText = 'Submitted text' | |
fireEvent(input, 'onSubmitEditing', { nativeEvent: { text: testText } }) | |
expect(output.props.children).toBe(testText) | |
}) | |
test('mocked callback', () => { | |
const myMock = jest.fn() | |
const { getByTestId } = render( | |
<HookTest myMock={myMock} />) | |
const onPressComponent = getByTestId('onPressComponent') | |
fireEvent(onPressComponent, 'onPress') | |
expect(myMock).toHaveBeenCalled() | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
onSubmitEditing
is not passingthe value is not matching with the expected
expect(received).toBe(expected)
expected = "Submitted text"
received = undefined
@goodpic