45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
|
import { test, expect } from 'vitest';
|
||
|
|
||
|
import u from 'updeep';
|
||
|
import { action, Updux } from '../src/index.js';
|
||
|
|
||
|
const addTodoWithId = action('addTodoWithId');
|
||
|
const incNextId = action('incNextId');
|
||
|
const addTodo = action('addTodo');
|
||
|
|
||
|
const addTodoEffect = ({ getState, dispatch }) => next => action => {
|
||
|
const id = getState.nextId();
|
||
|
|
||
|
dispatch.incNextId();
|
||
|
|
||
|
next(action);
|
||
|
|
||
|
dispatch.addTodoWithId({ description: action.payload, id });
|
||
|
}
|
||
|
|
||
|
const todosDux = new Updux({
|
||
|
initial: { nextId: 1, todos: [] },
|
||
|
actions: { addTodo, incNextId, addTodoWithId },
|
||
|
selectors: {
|
||
|
nextId: ({nextId}) => nextId,
|
||
|
},
|
||
|
mutations: {
|
||
|
addTodoWithId: (todo) => u({ todos: (todos) => [...todos, todo] }),
|
||
|
incNextId: () => u({ nextId: id => id+1 }),
|
||
|
},
|
||
|
effects: {
|
||
|
'addTodo': addTodoEffect
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const store = todosDux.createStore();
|
||
|
|
||
|
test( "tutorial example", async () => {
|
||
|
store.dispatch.addTodo('Do the thing');
|
||
|
|
||
|
expect( store.getState() ).toMatchObject({
|
||
|
nextId:2, todos: [ 'Do the thing' ]
|
||
|
})
|
||
|
|
||
|
});
|