Last active
January 6, 2021 00:31
-
-
Save FanchenBao/8d025321def580bb1f651e7cf973752d to your computer and use it in GitHub Desktop.
This gist accompanies the UPDATED version of the medium article "Test hooks on 'keyboardDidShow' in React Native Functional Component"
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 {Keyboard} from 'react-native'; | |
import {render, act, cleanup} from '@testing-library/react-native'; // a better testing framework for event triggering | |
import {MyComponent} from 'mycomponent.js'; | |
describe('Test MyComponent', () => { | |
afterAll(cleanup); | |
test('Text shown by default', () => { | |
const {queryByText} = render(<MyComponent />); | |
expect(queryByText(/bear/i)).not.toBeNull(); | |
}); | |
test('When Keyboard shows up, text disappears; otherwise, text appears', () => { | |
const {queryByText} = render(<MyComponent />); | |
act(() => Keyboard.emit('keyboardDidShow')); | |
expect(queryByText(/bear/i)).toBeNull(); // keyboard is up, text disappears | |
act(() => Keyboard.emit('keyboardDidHide')); | |
expect(queryByText(/bear/i)).not.toBeNull(); // keyboard is down, text appears | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment