updux/docs/tutorial-effects.test.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-08-28 23:29:31 +00:00
import { test, expect } from 'vitest';
2023-09-06 19:09:45 +00:00
process.env.UPDEEP_MODE = "dangerously_never_freeze";
// [effects-1]
import u from '@yanick/updeep-remeda';
import * as R from 'remeda';
import Updux, { createAction, withPayload } from 'updux';
const addTodoWithId = createAction('addTodoWithId', withPayload());
const incNextId = createAction('incNextId');
const addTodo = createAction('addTodo', withPayload());
2022-08-28 23:29:31 +00:00
const todosDux = new Updux({
2023-09-06 19:09:45 +00:00
initialState: { nextId: 1, todos: [] },
2022-08-28 23:29:31 +00:00
actions: { addTodo, incNextId, addTodoWithId },
selectors: {
2023-09-06 19:09:45 +00:00
nextId: ({ nextId }) => nextId,
2022-08-28 23:29:31 +00:00
},
});
2023-09-06 19:09:45 +00:00
todosDux.addMutation(addTodoWithId, (todo) => state => state
// u({
// todos: R.concat([u(todo, { done: false })])
// })
);
todosDux.addMutation(incNextId, () => state => state); //u({ nextId: id => id + 1 }));
// todosDux.addEffect(addTodo, ({ getState, dispatch }) => next => action => {
// const id = getState.nextId();
// dispatch.incNextId();
// next(action);
// dispatch.addTodoWithId({ id, description: action.payload });
// });
2022-08-28 23:29:31 +00:00
const store = todosDux.createStore();
2023-09-06 19:09:45 +00:00
store.dispatch.addTodo('write tutorial');
// [effects-1]
test('basic', () => {
expect(store.getState()).toMatchObject({
nextId: 2,
todos: [{ id: 1, description: 'write tutorial', done: false }]
});
2022-08-28 23:29:31 +00:00
});