latus/packages/core/test/middleware.js
2021-01-05 11:25:46 -06:00

72 lines
1.6 KiB
JavaScript

import {expect} from 'chai';
import Middleware from '../src/middleware';
describe('middleware', () => {
it('should construct with middleware', (done) => {
let called = false;
const middleware = new Middleware([
(next) => {
called = true;
next();
},
]);
middleware.dispatch(() => {
expect(called).to.be.true;
done();
});
});
it('should pass args', (done) => {
const middleware = new Middleware([
(arg, next) => {
expect(arg).to.equal(69);
next();
},
]);
middleware.dispatch(69, done);
});
describe('error handling', () => {
it('should skip when necessary', (done) => {
let progress = 0;
const middleware = new Middleware([
(next) => {
progress += 1;
next();
},
(error, next) => {
throw new Error();
},
(next) => {
progress += 1;
next();
},
]);
middleware.dispatch((error) => {
expect(error).to.equal(undefined);
expect(progress).to.equal(2);
done();
});
});
it('should use when necessary', (done) => {
let progress = 0;
const middleware = new Middleware([
(next) => {
progress += 1;
next(new Error());
},
(error, next) => {
next(error);
},
(next) => {
progress += 1;
next();
},
]);
middleware.dispatch((error) => {
expect(error).to.not.equal(undefined);
expect(progress).to.equal(1);
done();
});
});
});
});