test with ts

typescript
Yanick Champoux 2021-10-15 13:35:43 -04:00
parent 440c76d408
commit a657813415
1 changed files with 45 additions and 39 deletions

View File

@ -1,11 +1,9 @@
import tap from 'tap';
import sinon from 'sinon';
import u from '@yanick/updeep';
import u from 'updeep';
import { Updux } from './Updux.js';
import { action } from './actions.js';
import { Updux } from './Updux';
import { action } from './actions';
tap.test('subscriptions', async () => {
test('subscriptions', () => {
const inc = action('inc');
const set_copy = action('set_copy');
@ -34,15 +32,15 @@ tap.test('subscriptions', async () => {
store.dispatch(inc());
tap.same(store.getState(), { x: 1, copy: 1 });
expect(store.getState()).toMatchObject({ x: 1, copy: 1 });
store.dispatch(inc());
store.dispatch(inc());
tap.same(store.getState(), { x: 3, copy: 2 }, 'we unsubscribed');
expect(store.getState()).toMatchObject({ x: 3, copy: 2 });
});
tap.test('subduxes subscriptions', async (t) => {
test('subduxes subscriptions', () => {
const inc_top = action('inc_top');
const inc_bar = action('inc_bar');
const transform_bar = action('transform_bar');
@ -54,7 +52,7 @@ tap.test('subduxes subscriptions', async (t) => {
inc_bar: () => (state) => state + 'a',
transform_bar: (outcome) => () => outcome,
},
subscriptions: [
reactions: [
(store) => (state, previous, unsubscribe) => {
if (state.length <= 2) return;
unsubscribe();
@ -79,7 +77,7 @@ tap.test('subduxes subscriptions', async (t) => {
next(action);
},
},
subscriptions: [
reactions: [
(store) => {
return ({ count }, { count: previous } = {}) => {
if (count !== previous) {
@ -96,20 +94,23 @@ tap.test('subduxes subscriptions', async (t) => {
store.dispatch(inc_top());
store.dispatch(inc_top());
t.same(store.getState(), {
expect(store.getState()).toMatchObject({
count: 2,
bar: 'look at look at aaa',
});
store.dispatch(inc_top());
t.same(store.getState(), {
expect(store.getState()).toMatchObject({
count: 3,
bar: 'look at look at aaaa',
});
});
tap.test('subscription within subduxes', { todo: false }, async (t) => {
let innerState = sinon.fake.returns(null);
let outerState = sinon.fake.returns(null);
test('subscription within subduxes', () => {
let innerState = jest.fn(() => null);
let outerState = jest.fn(() => null);
const resetMocks = () =>
[innerState, outerState].map((f) => f.mockReset());
const inner = new Updux({
initial: 1,
@ -117,7 +118,7 @@ tap.test('subscription within subduxes', { todo: false }, async (t) => {
mutations: {
inc: () => (state) => state + 1,
},
subscriptions: [
reactions: [
(store) => (state, previous, unsub) => {
if (!previous) return;
store.subscribe(innerState);
@ -135,7 +136,7 @@ tap.test('subscription within subduxes', { todo: false }, async (t) => {
mutations: {
incOuter: () => (state) => ({ ...state, x: state.x + 1 }),
},
subscriptions: [
reactions: [
(store) => (state, previous, unsub) => {
if (!previous) return;
store.subscribe(outerState);
@ -149,39 +150,44 @@ tap.test('subscription within subduxes', { todo: false }, async (t) => {
store.dispatch({ type: 'noop' });
store.dispatch({ type: 'noop' });
t.notOk(innerState.called);
t.notOk(outerState.called);
expect(innerState).not.toHaveBeenCalled();
expect(outerState).not.toHaveBeenCalled();
store.dispatch.inc();
// not called yet
t.notOk(innerState.called);
t.notOk(outerState.called);
expect(innerState).not.toBeCalled();
expect(outerState).not.toBeCalled();
store.dispatch.inc();
t.ok(outerState.calledOnce);
t.same(outerState.firstCall.args[0], { inner: 3, x: 0 });
expect(outerState).toBeCalledTimes(1);
expect(outerState).toHaveBeenCalledWith(
{ inner: 3, x: 0 },
undefined,
expect.anything()
);
t.ok(innerState.calledOnce);
t.same(innerState.firstCall.args[0], 3);
expect(innerState).toHaveBeenCalled();
expect(innerState).toHaveBeenCalledWith(3, undefined, expect.anything());
innerState.resetHistory();
outerState.resetHistory();
resetMocks();
store.dispatch.inc();
t.ok(outerState.calledOnce);
t.same(outerState.firstCall.args[0], { inner: 4, x: 0 });
expect(outerState).toBeCalled();
expect(outerState).toBeCalledWith(
{ inner: 4, x: 0 },
expect.anything(),
expect.anything()
);
t.ok(innerState.calledOnce);
t.same(innerState.firstCall.args[0], 4);
expect(innerState).toBeCalledWith(4, 3, expect.anything());
await t.test('state of subdux doesnt change', async (t) => {
innerState.resetHistory();
outerState.resetHistory();
store.dispatch.incOuter();
// state of subdux doesnt change
resetMocks();
t.ok(outerState.calledOnce);
t.notOk(innerState.called);
});
store.dispatch.incOuter();
expect(outerState).toBeCalled();
expect(innerState).not.toBeCalled();
});