1.0.0 • Published 5 months ago

rtk-mock-store v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

rtk-mock-store build-workflow check-code-coverage

Modern redux store jest mock helper function for testing reducers using @reduxjs/toolkit slices.

Installation

To install the rtk-mock-store package, use the following command:

npm install rtk-mock-store

Examples

API Documentation

createMockStore

Creates a mock store for testing redux actions and reducers using redux-toolkit. It can be used to test a single reducer in isolation or multiple reducers together.

Type Parameters
  • MockRootState: The root state of the mock store.
Parameters
  • reducerToTest: The reducer to test (key of MockRootState).
  • reducers: All reducers in the store (ReducersMapObject).
  • initialRootState: The initial state of the store (MockRootState).
Returns
  • RTKMockStore<MockRootState>: The mock store instance.
Example
// reducer.test.ts
import createMockStore from 'rtk-mock-store';
import { INITIAL_STATE, reducer, testAction } from './reducer';

const mockStore = createMockStore(
  'reducerToTest',
  { reducerToTest: reducer },
  { reducerToTest: INITIAL_STATE }
);

describe('reducerToTest', () => {
  afterEach(() => {
    mockStore.reset();
  });

  it('should have initial state', () => {
    expect(mockStore.getReducerState()).toEqual({ test: false });
  });

  it('should set test to true', () => {
    mockStore.dispatch(testAction());
    expect(mockStore.dispatch.mock.calls[0][0].type).toEqual('reducerToTest/TEST_ACTION');
    expect(mockStore.getReducerState()).toEqual({ test: true });
  });
});

RTKMockStore Properties

PropertyTypeComments
rootStateSnapshotsRecord<Action['type'], MockRootState>[]The snapshots of the root state of the mock store.
rootStateMockRootStateThe root state of the mock store.
extraArgumentobjectThe extra argument of the mock store.
getRootState() => MockRootStateReturns the root state of the mock store.
getReducerState() => MockRootState[keyof MockRootState]Returns the state of the reducer being tested.
setRootState(state: MockRootState) => voidSets the root state of the mock store.
setReducerState(state: MockRootState[keyof MockRootState]) => voidSets the state of the reducer being tested.
getRootStateSnapshots() => Record<Action['type'], MockRootState>[]Returns the snapshots of the root state of the mock store.
getRootStateSnapshot(actionType: Action['type']) => Record<Action['type'], MockRootState> \| undefinedReturns the snapshot of the root state of the mock store for a specific action type.
getReducerStateSnapshots() => Record<Action['type'], MockRootState[keyof MockRootState]>[]Returns the snapshots of the reducer being tested.
getReducerStateSnapshot(actionType: Action['type']) => Record<Action['type'], MockRootState[keyof MockRootState]> \| undefinedReturns the snapshot of the reducer being tested for a specific action type.
printReducerStateSnapshots() => voidPrints the snapshots of the reducer being tested.
printRootStateSnapshots() => voidPrints the snapshots of the root state of the mock store.
next(action: Action) => voidThe next middleware function.
runThunk(action: ThunkAction<any, MockRootState, any, Action>) => voidRuns a thunk action.
getState() => MockRootStateReturns the root state of the mock store.
dispatch(action: Action) => voidDispatches an action to the mock store.
reset() => voidResets the mock store state to intialRootState.

Contribution

If you want to contribute to this project, send a PR directly instead of creating an issue.

1.0.0

5 months ago