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 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' });
});
});