updux/src/reactions.test.js

51 lines
1.3 KiB
JavaScript

import { test, expect, vi } from 'vitest';
import { Updux } from './Updux.js';
test('basic reactions', async () => {
const spyA = vi.fn();
const spyB = vi.fn();
const foo = new Updux({
initial: { i: 0 },
reactions: [() => spyA],
actions: { inc: null },
mutations: {
inc: () => (state) => ({ ...state, i: state.i + 1 }),
},
});
foo.addReaction((api) => spyB);
const store = foo.createStore();
store.dispatch.inc();
expect(spyA).toHaveBeenCalledOnce();
expect(spyB).toHaveBeenCalledOnce();
});
test('subduxes reactions', async () => {
const spyA = vi.fn();
const spyB = vi.fn();
const foo = new Updux({
subduxes: {
a: new Updux({
initial: 1,
reactions: [() => (state) => spyA(state)],
actions: { inc: null },
mutations: {
inc: () => (state) => state + 1,
},
}),
b: new Updux({ initial: 10, reactions: [() => spyB] }),
},
});
const store = foo.createStore();
store.dispatch.inc();
store.dispatch.inc();
expect(spyA).toHaveBeenCalledTimes(2);
expect(spyA).toHaveBeenCalledWith(3);
expect(spyB).toHaveBeenCalledOnce(); // the original inc initialized the state
});