@uistate/event-test 1.0.0

Event-sequence testing for UIstate stores. Trigger state changes and assert values, types, and event firings in a chainable API.

Install

npm install @uistate/event-test @uistate/core

@uistate/core is a peer dependency.

License

Proprietary. Free for personal use, open-source projects, and educational use. Commercial use requires a separate license. See LICENSE.md for full terms.

Contact: ajdika@live.com

Quick Start

import { createEventTest, test, runTests } from '@uistate/event-test';

test('counter increments', () => {
  const t = createEventTest({ count: 0 });

  t.store.subscribe('intent.increment', () => {
    t.store.set('count', t.store.get('count') + 1);
  });

  t.trigger('intent.increment', true)
   .assertPath('count', 1)
   .assertEventFired('count', 1);
});

runTests();

createEventTest(initialState)

createEventTest(initial?: object) → eventTest

Creates a test harness wrapping a fresh createEventState store. The harness tracks all events fired during the test for assertion.

const t = createEventTest({ user: { name: 'Alice' }, items: [] });
// t.store is the underlying eventState store

trigger(path, value)

t.trigger(path: string, value: any) → t (chainable)

Set a value on the store and record the event. This is the primary way to drive state changes in tests.

t.trigger('intent.addItem', { text: 'Buy milk' })
 .trigger('intent.addItem', { text: 'Walk dog' });

assertPath(path, expected)

t.assertPath(path: string, expected: any) → t (chainable)

Assert that the current value at a path equals the expected value. Uses deep equality for objects/arrays.

t.assertPath('count', 1);
t.assertPath('user.name', 'Alice');

assertType(path, type)

t.assertType(path: string, type: string) → t (chainable)

Assert the typeof the value at a path.

t.assertType('count', 'number');
t.assertType('user', 'object');
t.assertType('user.name', 'string');

assertArrayLength(path, length)

t.assertArrayLength(path: string, length: number) → t (chainable)

Assert that the value at a path is an array with the expected length.

t.assertArrayLength('items', 2);

assertShape(path, shape)

t.assertShape(path: string, shape: object) → t (chainable)

Assert that the value at a path contains all keys in the shape object.

t.assertShape('user', { name: 'string', email: 'string' });

assertEventFired(path, count)

t.assertEventFired(path: string, count: number) → t (chainable)

Assert that a specific path was set exactly count times during the test.

t.trigger('count', 1)
 .trigger('count', 2)
 .assertEventFired('count', 2);

test(name, fn)

test(name: string, fn: () → void) → void

Register a named test. Tests are collected and run together with runTests().

test('user updates', () => {
  const t = createEventTest({ user: { name: 'Alice' } });
  t.trigger('user.name', 'Bob')
   .assertPath('user.name', 'Bob');
});

runTests()

runTests() → void

Run all registered tests and log results to the console.

runTests();
// ✅ user updates (2ms)
// ✅ counter increments (1ms)
// 2/2 passed