updux/dist/middleware.test.js

81 lines
2.4 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-05 01:34:14 +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-05 01:34:14 +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-05 01:34:14 +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-05 01:34:14 +00:00
zzz: { effects: {
2019-10-24 15:52:36 +00:00
foo: tracerEffect('child'),
2019-11-05 01:34:14 +00:00
}
}
2019-10-24 15:52:36 +00:00
},
2019-11-05 01:34:14 +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-05 01:34:14 +00:00
const store = (new _1.default({
2019-10-24 15:52:36 +00:00
effects: {
'*': api => next => action => {
tracer();
next(action);
},
},
2019-11-05 01:34:14 +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-05 01:34:14 +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-05 01:34:14 +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();
});
//# sourceMappingURL=middleware.test.js.map