2017-06-08 01:55:15 +05:30
|
|
|
import sinon from 'sinon';
|
2019-06-30 19:02:50 +05:30
|
|
|
import expect from 'test/unexpected';
|
2016-07-30 23:52:33 +05:30
|
|
|
|
|
|
|
import PromiseMiddlewareLayer from 'services/request/PromiseMiddlewareLayer';
|
|
|
|
|
|
|
|
describe('PromiseMiddlewareLayer', () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('#add()', () => {
|
|
|
|
let layer;
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
layer = new PromiseMiddlewareLayer();
|
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should have no middlewares by default', () => {
|
|
|
|
expect(layer.middlewares, 'to have length', 0);
|
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should add middleware into layer', () => {
|
|
|
|
layer.add({});
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(layer.middlewares, 'to have length', 1);
|
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('throws if middleware is not object', () => {
|
|
|
|
expect(() => layer.add(1), 'to throw', 'A middleware must be an object');
|
2016-07-30 23:52:33 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('#run()', () => {
|
|
|
|
it('should return promise', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(layer.run('then'), 'to be a', Promise);
|
|
|
|
});
|
|
|
|
|
|
|
|
testAction('then');
|
|
|
|
testAction('catch');
|
|
|
|
|
|
|
|
function testAction(name) {
|
|
|
|
describe(`run('${name}')`, () => {
|
|
|
|
it('should run middleware', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
2019-11-27 14:33:32 +05:30
|
|
|
const middleware = {
|
|
|
|
[name]: sinon
|
|
|
|
.spy(() => Promise.resolve())
|
|
|
|
.named(`middleware.${name}`),
|
|
|
|
};
|
|
|
|
|
|
|
|
layer.add(middleware);
|
2016-07-30 23:52:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return layer.run(name).then(() => {
|
|
|
|
expect(middleware[name], 'was called once');
|
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should pass data', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
2019-11-27 14:33:32 +05:30
|
|
|
const middleware = {
|
|
|
|
[name]: sinon
|
|
|
|
.spy(() => Promise.resolve())
|
|
|
|
.named(`middleware.${name}`),
|
|
|
|
};
|
|
|
|
const data = {};
|
|
|
|
|
|
|
|
layer.add(middleware);
|
|
|
|
|
|
|
|
return layer.run(name, data).then(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
expect(middleware[name], 'to have a call satisfying', [data]);
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
});
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
it('should call multiple middlewares', async () => {
|
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
|
|
|
const initialValue = 'initialValue';
|
|
|
|
const options = { foo: 'bar' };
|
2019-11-27 14:33:32 +05:30
|
|
|
const firstMethod = name === 'catch' ? 'reject' : 'resolve';
|
|
|
|
const middleware1 = {
|
|
|
|
[name]: sinon
|
2019-12-07 16:58:52 +05:30
|
|
|
.spy(() => (Promise[firstMethod] as any)('new data'))
|
2019-11-27 14:33:32 +05:30
|
|
|
.named(`middleware1.${name}`),
|
|
|
|
};
|
|
|
|
const middleware2 = {
|
|
|
|
[name]: sinon
|
|
|
|
.spy(() => Promise.resolve('the last data'))
|
|
|
|
.named(`middleware2.${name}`),
|
|
|
|
};
|
|
|
|
|
|
|
|
layer.add(middleware1);
|
|
|
|
layer.add(middleware2);
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
const resp = await layer.run(name, initialValue, options);
|
|
|
|
|
|
|
|
expect(middleware1[name], 'to have a call satisfying', [
|
|
|
|
initialValue,
|
|
|
|
options,
|
|
|
|
]);
|
|
|
|
expect(middleware2[name], 'to have a call satisfying', [
|
|
|
|
'new data',
|
|
|
|
options,
|
|
|
|
]);
|
|
|
|
expect(resp, 'to equal', 'the last data');
|
2016-07-30 23:52:33 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not call wrong actions', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
2019-11-27 14:33:32 +05:30
|
|
|
const middleware = {
|
|
|
|
then: () => {},
|
|
|
|
wrongAction: sinon
|
|
|
|
.spy(() => Promise.resolve())
|
|
|
|
.named('middleware.wrongAction'),
|
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
layer.add(middleware as any);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return layer.run('then').then(() => {
|
|
|
|
expect(middleware.wrongAction, 'was not called');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call next catch middleware if previous was resolved', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
2019-11-27 14:33:32 +05:30
|
|
|
const middleware1 = {
|
|
|
|
catch: () => Promise.resolve(),
|
|
|
|
};
|
|
|
|
const middleware2 = {
|
|
|
|
catch: sinon.spy(() => {}).named('middleware2.catch'),
|
|
|
|
};
|
|
|
|
|
|
|
|
layer.add(middleware1);
|
|
|
|
layer.add(middleware2);
|
|
|
|
|
|
|
|
return layer.run('catch').then(() => {
|
|
|
|
expect(middleware2.catch, 'was not called');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should pass restart callback if any', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const layer: any = new PromiseMiddlewareLayer();
|
2019-11-27 14:33:32 +05:30
|
|
|
const middleware = {
|
|
|
|
catch: sinon.spy(() => Promise.resolve()).named('middleware.catch'),
|
|
|
|
};
|
2019-12-07 16:58:52 +05:30
|
|
|
const callback = async () => {};
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
layer.add(middleware);
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return layer.run('catch', {}, {}, callback).then(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(middleware.catch, 'to have a call satisfying', [
|
2019-12-07 16:58:52 +05:30
|
|
|
{},
|
2019-11-27 14:33:32 +05:30
|
|
|
{},
|
|
|
|
expect.it('to be', callback),
|
|
|
|
]);
|
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-07-30 23:52:33 +05:30
|
|
|
});
|