From 6fb14a26d0b6a813a4003d7a7618b792eb6531da Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 15 Oct 2021 10:31:37 -0400 Subject: [PATCH] tests in ts --- src/actions.test.js | 12 ++++------ src/documentation.test.ts | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/documentation.test.ts diff --git a/src/actions.test.js b/src/actions.test.js index 88baf25..094ac1a 100644 --- a/src/actions.test.js +++ b/src/actions.test.js @@ -1,15 +1,13 @@ -import { test } from 'tap'; - import { action } from './actions.js'; -test('action generators', async (t) => { +test('action generators', () => { const foo = action('foo'); - t.equal(foo.type, 'foo'); - t.same(foo(), { type: 'foo' }); + expect(foo.type).toEqual( 'foo'); + expect(foo()).toMatchObject( { type: 'foo' }); const bar = action('bar'); - t.equal(bar.type, 'bar'); - t.same(bar(), { type: 'bar' }); + expect(bar.type).toEqual( 'bar'); + expect(bar()).toMatchObject( { type: 'bar' }); }); diff --git a/src/documentation.test.ts b/src/documentation.test.ts new file mode 100644 index 0000000..b0669d9 --- /dev/null +++ b/src/documentation.test.ts @@ -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', + }); +});