aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/withMessageAsyncTest.js
diff options
context:
space:
mode:
authornanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-21 06:29:31 +0000
committernanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-21 06:29:31 +0000
commitee8e1a13b60a6adfdc691b2a9b57289188397641 (patch)
tree096633208d9b8b6b59b67f4034a0cbb41e1f4c5d /node_modules/express-validator/test/withMessageAsyncTest.js
parent689df70a38ace2f88cfef6ab50f10dc546b48f00 (diff)
need pull
Diffstat (limited to 'node_modules/express-validator/test/withMessageAsyncTest.js')
-rw-r--r--node_modules/express-validator/test/withMessageAsyncTest.js143
1 files changed, 143 insertions, 0 deletions
diff --git a/node_modules/express-validator/test/withMessageAsyncTest.js b/node_modules/express-validator/test/withMessageAsyncTest.js
new file mode 100644
index 0000000..624dbe1
--- /dev/null
+++ b/node_modules/express-validator/test/withMessageAsyncTest.js
@@ -0,0 +1,143 @@
+var chai = require('chai');
+var expect = chai.expect;
+var Promise = require('bluebird');
+
+describe('#withMessage()', function() {
+ describe('on async validators', function() {
+
+ before(function() {
+ delete require.cache[require.resolve('../lib/express_validator')];
+ });
+
+ it('should not affect a passing validation', function(done) {
+ var validator = require('../lib/express_validator')({
+ customValidators: {
+ test: function() {
+ // A dummy passing validator
+ return new Promise(function(resolve) {
+ resolve();
+ });
+ }
+ }
+ });
+
+ var req = {
+ body: {
+ testParam: 100
+ }
+ };
+
+ validator(req, {}, function() {});
+ req.check('testParam', 'Default Message')
+ .test().withMessage('Custom Message');
+
+ verify(done, function() {
+ return req.asyncValidationErrors()
+ .catch(function() {
+ expect.fail(null, null, 'No errors should have been generated by the test');
+ });
+ });
+
+ });
+
+ it('should provide a custom message when a validation fails', function(done) {
+ var validator = require('../lib/express_validator')({
+ customValidators: {
+ test: function() {
+ // A dummy failing validator
+ return new Promise(function(resolve, reject) {
+ reject();
+ });
+ }
+ }
+ });
+
+ var req = {
+ body: {
+ testParam: 100
+ }
+ };
+
+ validator(req, {}, function() {});
+
+ req.check('testParam', 'Default Message')
+ .test().withMessage('Custom Message');
+
+ verify(done, function() {
+ return req.asyncValidationErrors()
+ .catch(function(err) {
+ expect(err).to.deep.equal([{
+ msg: 'Custom Message',
+ param: 'testParam',
+ value: 100
+ }]);
+ });
+ });
+ });
+
+ it('should provide the default message if it was not the validation to fail', function(done) {
+ var validator = require('../lib/express_validator')({
+ customValidators: {
+ pass: function() {
+ return new Promise(function(resolve) { resolve(); });
+ },
+ fail: function() {
+ return new Promise(function(resolve, reject) { reject(); });
+ }
+ }
+ });
+
+ var req = {
+ body: {
+ testParam: 100
+ }
+ };
+
+ validator(req, {}, function() {});
+
+ req.check('testParam', 'Default Message')
+ .fail() // Default Message
+ .pass().withMessage('Passing Message')
+ .fail() // Default Message
+ .fail().withMessage('Failing Message')
+ .fail() // Default Message
+ .fail().withMessage('Failing Message 1')
+ .fail().withMessage('Failing Message 2');
+
+ var expected = [
+ 'Default Message',
+ 'Default Message',
+ 'Failing Message',
+ 'Default Message',
+ 'Failing Message 1',
+ 'Failing Message 2'];
+
+ verify(done, function() {
+ return req.asyncValidationErrors()
+ .catch(function(err) {
+ expect(err.map(function(e) {return e.msg;})).to.deep.equal(expected);
+ });
+ });
+ });
+
+ // Helper to handle resolving tests async
+ function verify(done, f) {
+ try {
+ var test = f();
+ if (test && test.then) {
+ test.then(function() {
+ done();
+ })
+ .catch(function(err) {
+ done(err);
+ });
+ } else {
+ done();
+ }
+ }
+ catch (e) {
+ done(e);
+ }
+ }
+ });
+}); \ No newline at end of file