import u from 'updeep';
import add from 'lodash/fp/add.js';

import { Updux } from '.';

test('README.md', () => {
    const otherDux = new Updux({});

    const dux = new Updux({
        initial: {
            counter: 0,
        },
        actions: {
            inc: null,
        },
        subduxes: {
            otherDux,
        },
    });

    dux.setMutation('inc', (increment) => u({ counter: add(increment) }));

    dux.addEffect('*', (api) => (next) => (action) => {
        next(action);
    });

    const store = dux.createStore();

    store.dispatch.inc(1);

    expect(store.getState().counter).toEqual(1);
});

test('tutorial', () => {
    const todosDux = new Updux<any, any>({
        initial: {
            next_id: 1,
            todos: [],
        },
    });

    todosDux.setAction('addTodo');
    todosDux.setAction('todoDone');

    expect(todosDux.actions.addTodo('write tutorial')).toMatchObject({
        type: 'addTodo',
        payload: 'write tutorial',
    });
});
