Kea Test Utilities

0.2.4 · active · verified Tue Apr 21

Kea Test Utilities (kea-test-utils) is a library designed to streamline the testing of Kea logic stores, offering a fluent API for asserting logic behavior in JavaScript and TypeScript environments. Currently at version 0.2.4, it is actively maintained and ships with TypeScript types, aligning with the Kea 3.x ecosystem. The package provides core utilities such as `expectLogic` and `partial`, enabling developers to dispatch actions and then assert against the resulting state changes and subsequent dispatched actions. A critical aspect of its usage involves integrating the `testUtilsPlugin` with Kea's `resetContext` function before each test to ensure a clean, isolated testing environment. This allows for precise control over the Kea context, preventing state leakage and ensuring reliable test results. Its primary differentiator is its deep integration with Kea's internal mechanisms, providing comprehensive tools for both querying recorded action history and awaiting new actions for verification, making it indispensable for robust Kea application testing.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to test a simple Kea logic using `expectLogic` to dispatch actions, match dispatched actions, and assert against the resulting state, including setup with `resetContext` and `testUtilsPlugin`.

import { kea, resetContext } from 'kea';
import { expectLogic, testUtilsPlugin, partial } from 'kea-test-utils';

interface CounterLogicState {
  count: number;
}

interface CounterLogicActions {
  increment: (amount: number) => { amount: number };
  decrement: (amount: number) => { amount: number };
}

const counterLogic = kea<CounterLogicState, CounterLogicActions>({
  path: ['scenes', 'counter'],
  actions: {
    increment: (amount: number) => ({ amount }),
    decrement: (amount: number) => ({ amount }),
  },
  reducers: {
    count: [
      0,
      {
        increment: (state, { amount }) => state + amount,
        decrement: (state, { amount }) => state - amount,
      },
    ],
  },
});

describe('counterLogic', () => {
  beforeEach(() => {
    // Essential: Reset Kea's context and enable test utilities before each test
    resetContext({ plugins: [testUtilsPlugin] });
  });

  test('should increment the count', async () => {
    await expectLogic(counterLogic, () => {
      // Dispatch actions directly on the logic
      counterLogic.actions.increment(5);
    })
      .toDispatchActions(['increment'])
      .toMatchValues({ count: 5 });
  });

  test('should decrement the count', async () => {
    await expectLogic(counterLogic, () => {
      counterLogic.actions.decrement(2);
    })
      .toDispatchActions(['decrement'])
      .toMatchValues(partial({ count: 3 })); // Using partial to match only specific values
  });

  test('should handle multiple actions', async () => {
    await expectLogic(counterLogic, () => {
      counterLogic.actions.increment(10);
      counterLogic.actions.decrement(3);
    })
      .toDispatchActions([
        'increment',
        'decrement'
      ])
      .toMatchValues({ count: 7 });
  });
});

view raw JSON →