From 69a8781b4b7f7de28b8f1d9a4a4bfdd679cae4fa Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 15 Oct 2021 15:10:19 -0400 Subject: [PATCH] test with ts --- src/buildInitial/test.js | 10 ----- src/buildMiddleware/test.js | 86 +++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 56 deletions(-) delete mode 100644 src/buildInitial/test.js diff --git a/src/buildInitial/test.js b/src/buildInitial/test.js deleted file mode 100644 index 5a1eb12..0000000 --- a/src/buildInitial/test.js +++ /dev/null @@ -1,10 +0,0 @@ -import { test } from 'tap'; - -import { buildInitial } from './index.ts'; - -test('basic', async (t) => { - t.same(buildInitial({ a: 1 }, { b: { initial: { c: 2 } } }), { - a: 1, - b: { c: 3 }, - }); -}); diff --git a/src/buildMiddleware/test.js b/src/buildMiddleware/test.js index 6736e01..e34c441 100644 --- a/src/buildMiddleware/test.js +++ b/src/buildMiddleware/test.js @@ -1,16 +1,11 @@ -import { test } from 'tap'; -import sinon from 'sinon'; +import { buildMiddleware } from '.'; +import { action } from '../actions'; -import { buildMiddleware } from './index.js'; -import { action } from '../actions.js'; - -test('single effect', async (t) => { +test('single effect', async () => { const effect = (api) => (next) => (action) => { - t.same(action, { type: 'foo' }); - t.has(api, { - actions: {}, - selectors: {}, - }); + expect(action).toMatchObject({ type: 'foo' }); + expect(api).toHaveProperty('actions'); + expect(api).toHaveProperty('selectors'); next(); }; @@ -21,19 +16,17 @@ test('single effect', async (t) => { }); }); -test('augmented api', async (t) => { - const effect = (api) => (next) => async (action) => { - await t.test('selectors', async (t) => { - t.same(api.getState.getB(), 1); - t.same(api.selectors.getB({ a: { b: 2 } }), 2); +test('augmented api', async () => { + const effect = (api) => (next) => (action) => { + // selectors + expect(api.getState.getB()).toEqual(1); + expect(api.selectors.getB({ a: { b: 2 } })).toEqual(2); - t.same(api.getState(), { a: { b: 1 } }); - }); + expect(api.getState()).toMatchObject({ a: { b: 1 } }); - await t.test('dispatch', async (t) => { - t.same(api.actions.actionOne(), { type: 'actionOne' }); - api.dispatch.actionOne('the payload'); - }); + // dispatch + expect(api.actions.actionOne()).toMatchObject({ type: 'actionOne' }); + api.dispatch.actionOne('the payload'); next(); }; @@ -48,8 +41,8 @@ test('augmented api', async (t) => { } ); - const getState = sinon.fake.returns({ a: { b: 1 } }); - const dispatch = sinon.fake.returns(); + const getState = jest.fn(() => ({ a: { b: 1 } })); + const dispatch = jest.fn(() => null); await new Promise((resolve) => { middleware({ @@ -58,12 +51,14 @@ test('augmented api', async (t) => { })(resolve)({ type: 'foo' }); }); - t.ok(dispatch.calledOnce); - - t.ok(dispatch.calledWith({ type: 'actionOne', payload: 'the payload' })); + expect(dispatch).toBeCalledTimes(1); + expect(dispatch).toBeCalledWith({ + type: 'actionOne', + payload: 'the payload', + }); }); -test('subduxes', async (t) => { +test('subduxes', async () => { const effect1 = (api) => (next) => (action) => { next({ type: action.type, @@ -94,27 +89,26 @@ test('subduxes', async (t) => { ); mw({})(({ payload }) => { - t.same(payload, [1, 2, 3]); + expect(payload).toMatchObject([1, 2, 3]); })({ type: 'foo', payload: [] }); - t.test('api for subduxes', async (t) => { - const effect = (api) => (next) => (action) => { - t.same(api.getState(), 3); - next(); - }; + // api for subduxes + const effect = (api) => (next) => (action) => { + expect(api.getState()).toEqual(3); + next(); + }; - const mwInner = buildMiddleware([effect]); - const mwOuter = buildMiddleware( - [], - {}, - {}, - { alpha: { middleware: mwInner } } - ); + const mwInner = buildMiddleware([effect]); + const mwOuter = buildMiddleware( + [], + {}, + {}, + { alpha: { middleware: mwInner } } + ); - await new Promise((resolve) => { - mwOuter({ - getState: () => ({ alpha: 3 }), - })(resolve)({ type: 'foo' }); - }); + await new Promise((resolve) => { + mwOuter({ + getState: () => ({ alpha: 3 }), + })(resolve)({ type: 'foo' }); }); });