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