updux/dist/middleware.test.js

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-10-22 22:15:08 +00:00
"use strict";
2019-10-24 15:52:36 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = __importDefault(require("."));
2019-10-22 22:15:08 +00:00
test('simple effect', () => {
2019-10-24 15:52:36 +00:00
const tracer = jest.fn();
2019-11-07 00:07:06 +00:00
const store = new _1.default({
2019-10-24 15:52:36 +00:00
effects: {
foo: (api) => (next) => (action) => {
tracer();
next(action);
},
},
2019-11-07 00:07:06 +00:00
}).createStore();
2019-10-24 15:52:36 +00:00
expect(tracer).not.toHaveBeenCalled();
store.dispatch({ type: 'bar' });
expect(tracer).not.toHaveBeenCalled();
store.dispatch.foo();
expect(tracer).toHaveBeenCalled();
2019-10-22 22:15:08 +00:00
});
test('effect and sub-effect', () => {
2019-10-24 15:52:36 +00:00
const tracer = jest.fn();
const tracerEffect = (signature) => (api) => (next) => (action) => {
tracer(signature);
next(action);
};
2019-11-07 00:07:06 +00:00
const store = new _1.default({
2019-10-22 22:15:08 +00:00
effects: {
2019-10-24 15:52:36 +00:00
foo: tracerEffect('root'),
},
subduxes: {
2019-11-07 00:07:06 +00:00
zzz: {
effects: {
2019-10-24 15:52:36 +00:00
foo: tracerEffect('child'),
2019-11-07 00:07:06 +00:00
},
},
2019-10-24 15:52:36 +00:00
},
2019-11-07 00:07:06 +00:00
}).createStore();
2019-10-24 15:52:36 +00:00
expect(tracer).not.toHaveBeenCalled();
store.dispatch({ type: 'bar' });
expect(tracer).not.toHaveBeenCalled();
store.dispatch.foo();
expect(tracer).toHaveBeenNthCalledWith(1, 'root');
expect(tracer).toHaveBeenNthCalledWith(2, 'child');
2019-10-22 22:15:08 +00:00
});
test('"*" effect', () => {
2019-10-24 15:52:36 +00:00
const tracer = jest.fn();
2019-11-07 00:07:06 +00:00
const store = new _1.default({
2019-10-24 15:52:36 +00:00
effects: {
'*': api => next => action => {
tracer();
next(action);
},
},
2019-11-07 00:07:06 +00:00
}).createStore();
2019-10-24 15:52:36 +00:00
expect(tracer).not.toHaveBeenCalled();
store.dispatch({ type: 'bar' });
expect(tracer).toHaveBeenCalled();
2019-10-22 22:15:08 +00:00
});
test('async effect', async () => {
2019-10-24 15:52:36 +00:00
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
2019-10-22 22:15:08 +00:00
}
2019-10-24 15:52:36 +00:00
const tracer = jest.fn();
2019-11-07 00:07:06 +00:00
const store = new _1.default({
2019-10-24 15:52:36 +00:00
effects: {
foo: api => next => async (action) => {
next(action);
await timeout(1000);
tracer();
},
},
2019-11-07 00:07:06 +00:00
}).createStore();
2019-10-24 15:52:36 +00:00
expect(tracer).not.toHaveBeenCalled();
store.dispatch.foo();
expect(tracer).not.toHaveBeenCalled();
await timeout(1000);
expect(tracer).toHaveBeenCalled();
});
2019-11-07 00:07:06 +00:00
test('getState is local', () => {
let childState;
let rootState;
let rootFromChild;
const child = new _1.default({
initial: { alpha: 12 },
effects: {
doIt: ({ getState, getRootState }) => next => action => {
childState = getState();
rootFromChild = getRootState();
next(action);
},
},
});
const root = new _1.default({
initial: { beta: 24 },
subduxes: { child },
effects: {
doIt: ({ getState }) => next => action => {
rootState = getState();
next(action);
},
},
});
const store = root.createStore();
store.dispatch.doIt();
expect(rootState).toEqual({ beta: 24, child: { alpha: 12 } });
expect(rootFromChild).toEqual({ beta: 24, child: { alpha: 12 } });
expect(childState).toEqual({ alpha: 12 });
});
2019-10-24 15:52:36 +00:00
//# sourceMappingURL=middleware.test.js.map