Redux Logic Test Utilities

2.0.0 · active · verified Tue Apr 21

redux-logic-test provides testing utilities specifically designed to simplify the integration and unit testing of Redux Logic modules. It offers a `createMockStore` function that sets up a Redux store with a `redux-logic` middleware instance, allowing developers to easily inject logic, initial state, and mocked dependencies (like an API service) for isolated testing. The utility also includes built-in action tracking and a `whenComplete` helper to ensure all asynchronous logic operations have finished before assertions are made. The current stable version is 2.0.0, which primarily relaxed peer dependency constraints for Redux and Redux Logic without introducing API breaking changes. Release cadence is tied to updates in its core dependencies, `redux` and `redux-logic`, aiming for compatibility rather than frequent feature additions. Its key differentiator is its direct integration with `redux-logic`'s specific patterns, offering a more tailored testing experience than generic Redux testing utilities.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates setting up a mock Redux store with `redux-logic-test`, including a sample logic, mocked dependencies, initial state, and reducer. It dispatches actions, waits for logic completion, and logs the resulting state and dispatched actions for verification.

import { createMockStore } from 'redux-logic-test';
import { createLogic } from 'redux-logic';

const API = {
  get: (url) => Promise.resolve({ data: `Fetched data from ${url}` })
};

const fooLogic = createLogic({
  type: 'FOO',
  process({ API, getState, action }, dispatch, done) {
    API.get(`/api/${action.payload}`)
      .then(results => {
        dispatch({ type: 'FOO_SUCCESS', payload: results.data });
      })
      .catch(error => {
        dispatch({ type: 'FOO_FAILURE', payload: error.message });
      })
      .finally(() => done());
  }
});

const logic = [fooLogic];
const injectedDeps = { API };

const initialState = { some: 'state' };
const reducer = (state = initialState, action) => {
  switch(action.type) {
    case 'FOO_SUCCESS': return { ...state, lastFetch: action.payload };
    default: return state;
  }
};

async function runTest() {
  const store = createMockStore({
    initialState,
    reducer,
    logic,
    injectedDeps
  });

  store.dispatch({ type: 'FOO', payload: 'item123' });
  store.dispatch({ type: 'BAR' }); // Will be tracked but not processed by logic

  await store.whenComplete(); // Wait for fooLogic to finish

  console.log('Final State:', store.getState());
  console.log('Dispatched Actions:', store.actions);

  // Example assertions (in a real test framework like Jest)
  // expect(store.getState().lastFetch).toBe('Fetched data from /api/item123');
  // expect(store.actions).toEqual([
  //   { type: 'FOO', payload: 'item123' },
  //   { type: 'BAR' },
  //   { type: 'FOO_SUCCESS', payload: 'Fetched data from /api/item123' }
  // ]);
}

runTest();

view raw JSON →