Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Created January 2, 2020 16:58
Show Gist options
  • Save r3dm1ke/193560d083fc7fe922f3b1a034784a72 to your computer and use it in GitHub Desktop.
Save r3dm1ke/193560d083fc7fe922f3b1a034784a72 to your computer and use it in GitHub Desktop.
Unit test for the Counter component
import React from 'react';
import Counter from './Counter';
import {Provider} from 'react-redux';
import configureStore from 'redux-mock-store';
import {render, cleanup, fireEvent} from 'react-native-testing-library';
afterEach(cleanup);
describe('<Counter />', () => {
// configureStore expects a list of middlewares
const mockStore = configureStore([]);
it('should display current count', () => {
const store = mockStore({count: 5});
const rendered = render(
<Provider store={store}><Counter /></Provider>
);
const textComponent = rendered.getByTestId('text');
expect(textComponent.props.children).toContain(5);
});
it('should dispatch increment action', () => {
const store = mockStore({count: 5});
const rendered = render(
<Provider store={store}><Counter /></Provider>
);
const buttonComponent = rendered.getByTestId('button');
fireEvent(buttonComponent, 'press');
// This will return all actions dispatched on this store
const actions = store.getActions();
expect(actions.length).toBe(1);
expect(actions[0].type).toEqual('INCREMENT');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment