54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
import { test, expect } from 'vitest';
|
|
|
|
import u from 'updeep';
|
|
import { Updux } from '../src/index.js';
|
|
|
|
const done = () => (state) => ({...state, done: true});
|
|
|
|
const todo = new Updux({
|
|
initial: { id: 0, done: false },
|
|
actions: {
|
|
done: null,
|
|
doneAll: null,
|
|
},
|
|
mutations: {
|
|
done,
|
|
doneAll: done,
|
|
},
|
|
});
|
|
|
|
const todos = new Updux({
|
|
initial: [],
|
|
subduxes: { '*': todo },
|
|
actions: { addTodo: null },
|
|
mutations: {
|
|
addTodo: text => state => [ ...state, { text } ]
|
|
}
|
|
});
|
|
|
|
todos.setMutation(
|
|
todo.actions.done,
|
|
(text,action) => u.map(u.if(u.is('text',text), todo.upreducer(action))),
|
|
true // prevents the subduxes mutations to run automatically
|
|
);
|
|
|
|
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
|
|
]);
|
|
|
|
});
|