updux/dist/tutorial/recipes.test.js

34 lines
1.2 KiB
JavaScript

import { test, expect } from 'vitest';
import u from '@yanick/updeep-remeda';
import * as R from 'remeda';
import Updux from '../index.js';
const done = (text) => text;
const todo = new Updux({
initial: { text: '', done: false },
actions: { done, doneAll: null }, // doneAll is a synonym for done for this dux
});
todo.addMutation('done', () => u({ done: true }));
todo.addMutation('doneAll', () => u({ done: true }));
const todos = new Updux({
initialState: [],
actions: Object.assign({ addTodo: null }, todo.actions),
})
.addMutation('addTodo', (text) => R.concat([{ text }]))
.addMutation('done', (text, action) => u.mapIf({ text }, todo.upreducer(action)));
todos.setDefaultMutation((_payload, action) => R.map(todo.upreducer(action)));
test('tutorial', async () => {
const store = todos.createStore();
store.dispatch.addTodo('one');
store.dispatch.addTodo('two');
store.dispatch.addTodo('three');
store.dispatch.done('two');
expect(store.getState()[1].done).toBeTruthy();
expect(store.getState()[2].done).toBeFalsy();
store.dispatch.doneAll();
expect(store.getState().map(({ done }) => done)).toEqual([
true,
true,
true,
]);
});