Skip to content

Instantly share code, notes, and snippets.

@khadorkin
Forked from aarondail/rerender-test.js
Created September 1, 2021 07:59
Show Gist options
  • Save khadorkin/ab4921f3d2c3e12a4b159f5e9180617d to your computer and use it in GitHub Desktop.
Save khadorkin/ab4921f3d2c3e12a4b159f5e9180617d to your computer and use it in GitHub Desktop.
import * as React from 'react';
import { render } from 'react-native-testing-library';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import { MyContainerComponent, MyPresentationalComponent } from '...';
// Create a real redux store, with your reducers and middleware, and a useful initial
// state tree...
const store = createStore(...);
// Note this test only makes sense if MyPresentationalComponent is a PureComponent
test('MyPresentationalComponent should not be re-rendered when any misc action is dispatched ', () => {
const wrapper = render(
<Provider store={store}>
<MyContainerComponent />
</Provider>
);
const presentationalComponentInstance = wrapper.getByType(MyPresentationalComponent).instance;
const renderSpy = jest.spyOn(presentationalComponentInstance, 'render');
store.dispatch({ type: 'DUMMY_ACTION ' });
// render should not have been called... if it was that means MyPresentationalComponent
// is getting changed props when it shouldn't be.
expect(renderSpy).toHaveBeenCalledTimes(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment