custom-validate-input.test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var assert = require('assert');
  2. var M = require('../lib/Machine.constructor');
  3. describe('Custom `validate()` for an input', function() {
  4. ////////////////////////////////
  5. // Valid
  6. ////////////////////////////////
  7. it('should run when validate returns true', function(done) {
  8. var machine = {
  9. inputs: {
  10. foo: {
  11. validate: function(inputs, env) {
  12. return true;
  13. }
  14. }
  15. },
  16. exits: {
  17. success: {},
  18. error: {}
  19. },
  20. fn: function (inputs, exits, deps) {
  21. exits();
  22. }
  23. };
  24. M.build(machine)
  25. .configure({
  26. foo: {
  27. bar: 'hello',
  28. baz: 123
  29. }
  30. })
  31. .exec(function(err, result) {
  32. if(err) return done(err);
  33. done();
  34. });
  35. });
  36. ////////////////////////////////
  37. // Invalid
  38. ////////////////////////////////
  39. it('should fail when validate returns false', function(done) {
  40. var machine = {
  41. inputs: {
  42. foo: {
  43. validate: function(inputs, env) {
  44. return false;
  45. }
  46. }
  47. },
  48. exits: {
  49. success: {},
  50. error: {}
  51. },
  52. fn: function (inputs, exits, deps) {
  53. exits();
  54. }
  55. };
  56. M.build(machine)
  57. .configure({
  58. foo: {
  59. bar: 'hello',
  60. foo: 123
  61. }
  62. })
  63. .exec(function(err, result) {
  64. assert(err);
  65. done();
  66. });
  67. });
  68. it('should fail when an input is defined with a valid key', function() {
  69. var machine = {
  70. inputs: {
  71. foo: {
  72. valid: true
  73. }
  74. },
  75. exits: {
  76. success: {},
  77. error: {}
  78. },
  79. fn: function (inputs, exits, deps) {
  80. exits();
  81. }
  82. };
  83. assert.throws(function() {
  84. M.build(machine);
  85. });
  86. });
  87. });