92 lines
1.8 KiB
JavaScript
92 lines
1.8 KiB
JavaScript
import { test, expect } from 'vitest';
|
|
|
|
import u from 'updeep';
|
|
import R from 'remeda';
|
|
|
|
import { Updux } from '../src/index.js';
|
|
|
|
const nextIdDux = new Updux({
|
|
initial: 1,
|
|
actions: {
|
|
incrementNextId: null,
|
|
},
|
|
selectors: {
|
|
getNextId: state => state
|
|
},
|
|
mutations: {
|
|
incrementNextId: () => state => state + 1,
|
|
}
|
|
});
|
|
|
|
const matches = conditions => target => Object.entries(conditions).every(
|
|
([key,value]) => typeof value === 'function' ? value(target[key]) : target[key] === value
|
|
);
|
|
|
|
const todoDux = new Updux({
|
|
initial: {
|
|
id: 0,
|
|
description: "",
|
|
done: false,
|
|
},
|
|
actions: {
|
|
todoDone: null,
|
|
},
|
|
mutations: {
|
|
todoDone: id => u.if( matches({id}), { done: true })
|
|
},
|
|
selectors: {
|
|
desc: R.prop('description'),
|
|
}
|
|
});
|
|
|
|
const todosDux = new Updux({
|
|
initial: [],
|
|
subduxes: {
|
|
'*': todoDux
|
|
},
|
|
actions: {
|
|
addTodoWithId: (description, id) => ({description, id} )
|
|
},
|
|
findSelectors: {
|
|
getTodoById: state => id => state.find(matches({id}))
|
|
},
|
|
mutations: {
|
|
addTodoWithId: todo => todos => [...todos, todo]
|
|
}
|
|
});
|
|
|
|
const mainDux = new Updux({
|
|
subduxes: {
|
|
nextId: nextIdDux,
|
|
todos: todosDux,
|
|
},
|
|
actions: {
|
|
addTodo: null
|
|
},
|
|
effects: {
|
|
addTodo: ({ getState, dispatch }) => next => action => {
|
|
const id = getState.getNextId();
|
|
|
|
dispatch.incrementNextId()
|
|
|
|
next(action);
|
|
|
|
dispatch.addTodoWithId( action.payload, id );
|
|
}
|
|
}
|
|
});
|
|
|
|
const store = mainDux.createStore();
|
|
|
|
|
|
test( "basic tests", () => {
|
|
|
|
const myDesc = 'do the thing';
|
|
store.dispatch.addTodo(myDesc);
|
|
|
|
expect( store.getState.getTodoById(1).desc() ).toEqual(myDesc);
|
|
|
|
|
|
|
|
});
|