Skip to content

Instantly share code, notes, and snippets.

@FanchenBao
Last active January 6, 2021 00:31
Show Gist options
  • Save FanchenBao/8d025321def580bb1f651e7cf973752d to your computer and use it in GitHub Desktop.
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"
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