This abstract class has a WrappingComponent defined which wraps the component in a Redux Provider,
passing in the return value from the createStore method.
To use this class, you have to extend it, pass in your store state type in the class declaration and
define your own createStore method to return an instance of your Redux store, using the
initialState and middlewares that this class provides - it is important to ensure that your
store uses these, because if it disregards them, none of the methods this class provides will function.
For example:
const component = new WrapperWithRedux(SomeComponent);
describe("when testing a scenario", () => {
let result: ReturnType<typeof component.render>;
it("renders the component", () => {
result = component
.withReduxState({
test: {
value: "Scenario value 1"
}
})
.render();
});
it("renders the initial value", () => {
expect(screen.getByText("Initial value")).toBeDefined();
});
it("dispatches actions.setValue", () => {
result.store.dispatch(actions.setValue("New value"));
});
it("renders the updated value", () => {
expect(screen.getByText("New value")).toBeDefined();
});
});An array of actions that have been dispatched, used when asserting that actions have been dispatched as expected during interactions or in the component lifecycle.
In addition to the public methods provided by the base Wrapper class, the following
methods are available.
Sets the default Redux store state to be used for the wrapper instance.
Sets the scenario-specific Redux store state to be used - cleared after render is called.
Toggles whether arrays get merged or not in Redux state.
Mounts the component with the React Testing Library render function, using the currently-set data.
Returns a RenderResult instance, which also includes a store property.
In addition to the RenderResult values, an updateProps function is also returned, which wraps
a call to the RTL rerender method in an act, as a convenience method to update props within the
component lifecycle.
import * as React from "react";
import { WrapperWithRedux as BaseWrapper } from "react-test-wrapper";
import { createStore, TStoreState } from "../store";
class Wrapper<
C extends React.ComponentType<any>,
S extends {} = TStoreState,
P extends React.ComponentProps<C> = React.ComponentProps<C>
> extends BaseWrapper<C, S, P> {
protected createStore(
initialState: DeepPartial<S>,
middlewares: Middleware[]
) {
return createStore(initialState, middlewares);
}
}