graphql-validity
Version:
Make business logic validation easy on the graphql side without adding any declarations or modifications to the existing graphql schema.
73 lines • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const sinon = require("sinon");
const chai_1 = require("chai");
const validation = require("../src/validation");
const profiling_1 = require("../src/profiling");
const express_middleware_1 = require("../src/express-middleware");
const mocks_1 = require("./helpers/mocks");
describe('express-middleware', () => {
let profilingResultHandler = {
handler: profiling_1.defaultProfilingResultHandler
};
let req;
let write;
let send;
let res;
let next;
let result;
let resFake;
let applyValidationFake;
let sandbox;
beforeEach(() => {
resFake = sinon.fake();
req = { __graphQLValidity: undefined };
write = () => { };
send = () => { };
res = {
write,
send: resFake
};
next = () => { };
applyValidationFake = sinon.fake();
const mockValidation = mocks_1.mockModule(validation, {
applyValidation: applyValidationFake
});
sandbox = sinon.createSandbox();
mockValidation(sandbox);
result = express_middleware_1.default(profilingResultHandler);
});
afterEach(() => {
sandbox.restore();
});
it('should return wrapper function', () => {
chai_1.expect(result).to.be.an('function');
});
describe('graphQLValidityExpressMiddleware', () => {
it('should replace original write and send functions with wrappers and add validity data to request', () => {
result(req, res, next);
chai_1.expect(res.write).to.not.equal(write);
chai_1.expect(res.send).to.not.equal(send);
});
it('should add validity data to request', () => {
chai_1.expect(req.__graphQLValidity).to.not.be.null;
});
});
describe('wrapOriginalResponder', () => {
it('should call original function after execution', () => {
res.send('{}');
chai_1.expect(resFake.callCount).to.equal(1);
});
it('should call validation function only once', () => {
result(req, res, next);
res.send('{}');
res.send('{}');
chai_1.expect(applyValidationFake.callCount).to.equal(1);
});
it('should not throw exception if output data is not in JSON format', () => {
result(req, res, next);
chai_1.expect(res.write).to.not.throw('');
});
});
});
//# sourceMappingURL=express-middleware.js.map