Skip to content

Instantly share code, notes, and snippets.

@xiongemi
Created April 8, 2022 19:29
Show Gist options
  • Save xiongemi/e75a3ec2c7ef18fd155fbe458944e057 to your computer and use it in GitHub Desktop.
Save xiongemi/e75a3ec2c7ef18fd155fbe458944e057 to your computer and use it in GitHub Desktop.
example unit test with different store
import { mockFilmEntity } from '@studio-ghibli-search-engine/models';
import {
initialRootState,
RootState,
} from '@studio-ghibli-search-engine/store';
import { render } from '@testing-library/react-native';
import React from 'react';
import { Provider } from 'react-redux';
import configureStore, { MockStoreEnhanced } from 'redux-mock-store';
import Film from './film';
jest.mock('@react-navigation/native', () => {
return {
useNavigation: () => ({
navigate: jest.fn(),
dispatch: jest.fn(),
}),
useRoute: () => ({
params: {
id: '123',
},
}),
};
});
describe('Film', () => {
const mockStore = configureStore<RootState>([]);
let store: MockStoreEnhanced<RootState>;
it('should render successfully for initial state', () => {
store = mockStore(initialRootState);
store.dispatch = jest.fn();
const { container } = render(
<Provider store={store}>
<Film />
</Provider>
);
expect(container).toBeTruthy();
});
it('should display film if it exists in store', () => {
store = mockStore({
...initialRootState,
films: {
loadingStatus: 'loaded',
ids: ['123'],
entities: { '123': mockFilmEntity },
},
});
store.dispatch = jest.fn();
const { getByTestId } = render(
<Provider store={store}>
<Film />
</Provider>
);
expect(store.dispatch).toHaveBeenCalled();
expect(getByTestId('title')).toHaveTextContent(mockFilmEntity.title);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment