42
loading...
This website collects cookies to deliver better user experience
"What changes when testing a react (native) + redux application ?"
import { render } from '@testing-library/react-native';
import Component from 'component'
describe('When testing a component', () => {
it('Usually only render the component itself', () => {
render(<Component />);
})
})
could not find react-redux context value; please ensure the component is wrapped in a <Provider>
import { render } from '@testing-library/react-native';
import Component from 'component'
import { Provider } from 'react-redux';
import yourStore from './store';
describe('When testing a redux component', () => {
it('Should be wrapped by a provider', () => {
render(
<Provider store={yourStore}>
<Component />
</Provider>
);
})
})
"jest": {
"preset": "react-native"
}
"jest": {
"preset": "react-native",
"setupFiles": ["<rootDir>/setupTests.js"]
}
import { configureStore } from '@reduxjs/toolkit';
import reducers from '<pathToYourReducers>'; // this is not a real path
import { Provider } from 'react-redux';
import { render } from '@testing-library/react-native';
export function renderWithRedux(renderedComponent){
const store = configureStore({
reducer: {
...reducers
},
});
return render(<Provider store={store}>{renderedComponent}</Provider>)
}
In this example I use redux toolkit to create my store, but you can use whichever method you like, as long as it generates a valid store at the end
import { renderWithRedux } from "<pathToTestHelpers>/renderWithRedux"; //Not a real file
global.renderWithRedux = renderWithRedux;
global.renderWithRedux = renderWithRedux;
import ComponentWithReduxData from 'componentWithReduxData'
describe('When rendering a component that uses redux data with the renderWithRedux global', () => {
it('Should render correctly', () => {
renderWithRedux(<ComponentWithReduxData />);
})
})