-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new type for validations in route (#2)
* feat: new type for validations in route * feat: added testing fixtures
- Loading branch information
1 parent
32be2b5
commit 0fb9147
Showing
7 changed files
with
8,701 additions
and
13,527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,7 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# npm | ||
package-lock.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
'use strict'; | ||
|
||
const serviceFake = (req, res) => res.json({ done: 'done' }); | ||
|
||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const _ = require('lodash'); | ||
|
||
const hasUserPrototype = (obj) => obj.constructor !== Object; | ||
|
||
const createFixture = async (serviceManager) => { | ||
const { middlewares, controllers } = serviceManager; | ||
|
||
return { | ||
start: () => {}, | ||
stop: () => {}, | ||
middlewares: _.mapValues(middlewares, (service) => | ||
_.mapValues(service, (method) => { | ||
if (method.callback) { | ||
const callbackMock = sinon.stub().returns(sinon.stub().callsArg(2)); | ||
callbackMock.callback = true; | ||
return callbackMock; | ||
} | ||
|
||
const methodStub = sinon.stub().callsArg(2); | ||
methodStub.validations = _.mapValues(method.validations, () => sinon.stub().callsArg(2)); | ||
|
||
return [...Object.values(methodStub.validations), methodStub]; | ||
}) | ||
), | ||
controllers: Object.keys(controllers).reduce((result, key) => { | ||
const target = controllers[key]; | ||
|
||
if (hasUserPrototype(target)) { | ||
result[key] = Object.getOwnPropertyNames(Object.getPrototypeOf(target)) | ||
.filter((_key) => _key !== 'constructor') | ||
.reduce((_result, _key) => { | ||
_result[_key] = sinon.stub().callsFake(serviceFake); | ||
return _result; | ||
}, {}); | ||
} else { | ||
result[key] = Object.keys(target).reduce((_result, _key) => { | ||
const validations = _.mapValues(controllers[key][_key].validations, () => sinon.stub().callsArg(2)); | ||
const controllerStub = sinon.stub().callsFake(serviceFake); | ||
controllerStub.validations = validations; | ||
_result[_key] = [...Object.values(validations), controllerStub]; | ||
return _result; | ||
}, {}); | ||
} | ||
|
||
return result; | ||
}, {}), | ||
clients: { io: { bindEvents: () => {} }, socketio: { attach: () => {} } }, | ||
services: { socket: null }, | ||
}; | ||
}; | ||
|
||
const checkFixture = (fixture, expected) => { | ||
const { middlewares, controllers } = fixture; | ||
|
||
const validations = { middlewares: {}, controllers: {} }; | ||
|
||
Object.keys(controllers).forEach((controller) => { | ||
if (!(controller in expected.controllers)) expected.controllers[controller] = {}; | ||
Object.keys(controllers[controller]).forEach((method) => { | ||
const expressMiddlewares = controllers[controller][method]; | ||
const controllerMethod = expressMiddlewares[expressMiddlewares.length - 1]; | ||
|
||
if (controllerMethod.validations) { | ||
if (!validations.controllers[controller]) validations.controllers[controller] = {}; | ||
|
||
validations.controllers[controller][method] = _.mapValues(controllerMethod.validations, (stub) => stub.callCount); | ||
} | ||
|
||
controllers[controller][method] = controllerMethod.callCount; | ||
if (!(method in expected.controllers[controller])) expected.controllers[controller][method] = 0; | ||
}); | ||
}); | ||
|
||
Object.keys(middlewares).forEach((service) => { | ||
if (!(service in expected.middlewares)) expected.middlewares[service] = {}; | ||
Object.keys(middlewares[service]).forEach((method) => { | ||
const expressMiddlewares = middlewares[service][method]; | ||
const middlewareMethod = expressMiddlewares[expressMiddlewares.length - 1]; | ||
|
||
if (middlewareMethod.validations) { | ||
if (!validations.middlewares[service]) validations.middlewares[service] = {}; | ||
|
||
validations.middlewares[service][method] = _.mapValues(middlewareMethod.validations, (stub) => stub.callCount); | ||
} | ||
|
||
if (middlewareMethod.callback) { | ||
middlewares[service][method] = middlewareMethod().callCount; | ||
} else { | ||
middlewares[service][method] = middlewareMethod.callCount; | ||
} | ||
if (!(method in expected.middlewares[service])) expected.middlewares[service][method] = 0; | ||
}); | ||
}); | ||
|
||
// seed the expected object with zeros | ||
// middlewares or controllers | ||
Object.keys(validations).forEach((group) => { | ||
if (!(group in expected.validations)) expected.validations[group] = {}; | ||
// class in a middleware or controller | ||
Object.keys(validations[group]).forEach((service) => { | ||
if (!(service in expected.validations[group])) expected.validations[group][service] = {}; | ||
// method in a class | ||
Object.keys(validations[group][service]).forEach((method) => { | ||
if (!(method in expected.validations[group][service])) expected.validations[group][service][method] = {}; | ||
// validation in a method | ||
Object.keys(validations[group][service][method]).forEach((validation) => { | ||
if (!(validation in expected.validations[group][service][method])) expected.validations[group][service][method][validation] = 0; | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
expect(controllers).to.deep.eql(expected.controllers); | ||
expect(middlewares).to.deep.eql(expected.middlewares); | ||
expect(validations).to.deep.eql(expected.validations); | ||
}; | ||
|
||
module.exports = { | ||
createFixture, | ||
checkFixture, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.