updux/src/reducer.test.ts

32 lines
874 B
TypeScript

import { test, expect } from 'vitest';
import { buildReducer } from './reducer.js';
import Updux from './Updux.js';
test('buildReducer, initialState state', () => {
const reducer = buildReducer({ a: 1 });
expect(reducer(undefined, { type: 'foo' })).toEqual({ a: 1 });
});
test('buildReducer, mutation', () => {
const reducer = buildReducer(1, [
{
matcher: ({ type }) => type === 'inc',
mutation: () => (state) => state + 1,
terminal: false,
},
]);
expect(reducer(undefined, { type: 'foo' })).toEqual(1);
expect(reducer(undefined, { type: 'inc' })).toEqual(2);
});
test.todo('basic reducer', () => {
const dux = new Updux({ initialState: { a: 3 } });
expect(dux.reducer).toBeTypeOf('function');
expect(dux.reducer({ a: 1 }, { type: 'foo' })).toMatchObject({ a: 1 }); // noop
});