tests in ts

This commit is contained in:
Yanick Champoux 2021-10-15 10:31:37 -04:00
parent 33ded1448e
commit 6fb14a26d0
2 changed files with 54 additions and 7 deletions

View File

@ -1,15 +1,13 @@
import { test } from 'tap';
import { action } from './actions.js'; import { action } from './actions.js';
test('action generators', async (t) => { test('action generators', () => {
const foo = action('foo'); const foo = action('foo');
t.equal(foo.type, 'foo'); expect(foo.type).toEqual( 'foo');
t.same(foo(), { type: 'foo' }); expect(foo()).toMatchObject( { type: 'foo' });
const bar = action('bar'); const bar = action('bar');
t.equal(bar.type, 'bar'); expect(bar.type).toEqual( 'bar');
t.same(bar(), { type: 'bar' }); expect(bar()).toMatchObject( { type: 'bar' });
}); });

49
src/documentation.test.ts Normal file
View File

@ -0,0 +1,49 @@
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({
initial: {
next_id: 1,
todos: [],
},
});
todosDux.setAction('addTodo');
todosDux.setAction('todoDone');
expect(todosDux.actions.addTodo('write tutorial')).toMatchObject({
type: 'addTodo',
payload: 'write tutorial',
});
});