Skip to content

Instantly share code, notes, and snippets.

@goodpic
Created September 12, 2019 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodpic/029ccbd33abfa8b2f2287b3aeb7f29d3 to your computer and use it in GitHub Desktop.
Save goodpic/029ccbd33abfa8b2f2287b3aeb7f29d3 to your computer and use it in GitHub Desktop.
Test React Hooks with react-native-testing-library
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()
})
})
@Shail-Patel-1
Copy link

onSubmitEditing is not passing
the value is not matching with the expected

expect(received).toBe(expected)
expected = "Submitted text"
received = undefined

@goodpic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment