test with ts

This commit is contained in:
Yanick Champoux 2021-10-15 15:10:19 -04:00
parent a657813415
commit 69a8781b4b
2 changed files with 40 additions and 56 deletions

View File

@ -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 },
});
});

View File

@ -1,16 +1,11 @@
import { test } from 'tap'; import { buildMiddleware } from '.';
import sinon from 'sinon'; import { action } from '../actions';
import { buildMiddleware } from './index.js'; test('single effect', async () => {
import { action } from '../actions.js';
test('single effect', async (t) => {
const effect = (api) => (next) => (action) => { const effect = (api) => (next) => (action) => {
t.same(action, { type: 'foo' }); expect(action).toMatchObject({ type: 'foo' });
t.has(api, { expect(api).toHaveProperty('actions');
actions: {}, expect(api).toHaveProperty('selectors');
selectors: {},
});
next(); next();
}; };
@ -21,19 +16,17 @@ test('single effect', async (t) => {
}); });
}); });
test('augmented api', async (t) => { test('augmented api', async () => {
const effect = (api) => (next) => async (action) => { const effect = (api) => (next) => (action) => {
await t.test('selectors', async (t) => { // selectors
t.same(api.getState.getB(), 1); expect(api.getState.getB()).toEqual(1);
t.same(api.selectors.getB({ a: { b: 2 } }), 2); 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) => { // dispatch
t.same(api.actions.actionOne(), { type: 'actionOne' }); expect(api.actions.actionOne()).toMatchObject({ type: 'actionOne' });
api.dispatch.actionOne('the payload'); api.dispatch.actionOne('the payload');
});
next(); next();
}; };
@ -48,8 +41,8 @@ test('augmented api', async (t) => {
} }
); );
const getState = sinon.fake.returns({ a: { b: 1 } }); const getState = jest.fn(() => ({ a: { b: 1 } }));
const dispatch = sinon.fake.returns(); const dispatch = jest.fn(() => null);
await new Promise((resolve) => { await new Promise((resolve) => {
middleware({ middleware({
@ -58,12 +51,14 @@ test('augmented api', async (t) => {
})(resolve)({ type: 'foo' }); })(resolve)({ type: 'foo' });
}); });
t.ok(dispatch.calledOnce); expect(dispatch).toBeCalledTimes(1);
expect(dispatch).toBeCalledWith({
t.ok(dispatch.calledWith({ type: 'actionOne', payload: 'the payload' })); type: 'actionOne',
payload: 'the payload',
});
}); });
test('subduxes', async (t) => { test('subduxes', async () => {
const effect1 = (api) => (next) => (action) => { const effect1 = (api) => (next) => (action) => {
next({ next({
type: action.type, type: action.type,
@ -94,27 +89,26 @@ test('subduxes', async (t) => {
); );
mw({})(({ payload }) => { mw({})(({ payload }) => {
t.same(payload, [1, 2, 3]); expect(payload).toMatchObject([1, 2, 3]);
})({ type: 'foo', payload: [] }); })({ type: 'foo', payload: [] });
t.test('api for subduxes', async (t) => { // api for subduxes
const effect = (api) => (next) => (action) => { const effect = (api) => (next) => (action) => {
t.same(api.getState(), 3); expect(api.getState()).toEqual(3);
next(); next();
}; };
const mwInner = buildMiddleware([effect]); const mwInner = buildMiddleware([effect]);
const mwOuter = buildMiddleware( const mwOuter = buildMiddleware(
[], [],
{}, {},
{}, {},
{ alpha: { middleware: mwInner } } { alpha: { middleware: mwInner } }
); );
await new Promise((resolve) => { await new Promise((resolve) => {
mwOuter({ mwOuter({
getState: () => ({ alpha: 3 }), getState: () => ({ alpha: 3 }),
})(resolve)({ type: 'foo' }); })(resolve)({ type: 'foo' });
});
}); });
}); });