mock-redux v0.2.2
🎭 mock-redux
🚧 This repository is still under evaluation! We're not sure yet whether this is the approach we want to take. If you have opinions, let us know!
Mocks out Redux actions and selectors for clean React Jest tests.
Tired of setting up, updating, and debugging through complex Redux states in your React tests? Use this package if you'd like your React component tests to not take dependencies on your full Redux store.
See FAQs for more backing information. 📚
Usage
import { mockRedux } from "mock-redux";mock-redux stubs out connect and the two common Redux hooks used with React components.
Call mockRedux() before your render/mount logic in each test.
Mocking State
mockRedux().state({
title: "Hooray!",
});Sets a root state to be passed to your component's selectors.
it("displays the title when there is a title", () => {
mockRedux().state({
title: "Hooray!",
});
// state => state.title
const view = render(<RendersTitle />);
view.getByText("Hooray!");
});See Selectors for more documentation or Heading for a code example.
Mocking Selectors
mockRedux()
.give(simpleSelector, "Hooray!")
.giveMock(fancySelector, jest.fn().mockReturnValueOnce("Just the once."));Provide results to the useSelector function for individual selectors passed to it.
These work similarly to Jest mocks: .give takes in the return value that will always be passed to the selector.
it("displays the title when there is a title", () => {
mockRedux().give(selectTitle, "Hooray!");
// state => state.title
const view = render(<RendersTitle />);
view.getByText("Hooray!");
});If you'd like more control over the return values, you can use .giveMock to provide a Jest mock.
See Selectors for more documentation or Heading for a code example.
Dispatch Spies
const { dispatch } = mockRedux();The dispatch function returned by useDispatch will be replaced by a jest.fn() spy.
You can then assert against it as with any Jest mock in your tests:
it("dispatches the pageLoaded action when rendered", () => {
const { dispatch } = mockRedux();
// dispatch(pageLoaded())
render(<DispatchesPageLoaded />);
expect(dispatch).toHaveBeenCalledWith(pageLoaded());
});See Dispatches for more documentation or Clicker for a code example.
Gotchas
- The first
mock-reduximport must come before the firstreact-reduximport in your test files. .giveand.giveMockwill only apply when selectors are passed directly touseSelector(e.g.useSelector(selectValue)).- For inline selectors such as
useSelector(state => selectValue(state)), use the.stateAPI to set the root state value. - See FAQs for more tips and tricks.
- For inline selectors such as
Hybrid Usage
You don't have to use mock-redux in every test file in your repository.
Only the test files that import mock-redux will have react-redux stubbed out.
TypeScript Usage
mock-redux is written in TypeScript and generally type safe.
mockRedux()has an optional<State>type which sets the type of the root state passed to.state..givereturn values must match the return types of their selectors..giveMockmocks must match the return types of their selectors.
Heck yes. 🤘
Development
Requires:
After forking the repo from GitHub:
git clone https://github.com/<your-name-here>/mock-redux
cd mock-redux
yarnContribution Guidelines
We'd love to have you contribute!
Check the issue tracker for issues labeled accepting prs to find bug fixes and feature requests the community can work on.
If this is your first time working with this code, the good first issue label indicates good introductory issues.
Please note that this project is released with a Contributor Covenant. By participating in this project you agree to abide by its terms. See CODE_OF_CONDUCT.md.