exit-getExample.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var M = require('../');
  4. describe('Machine exits getExample', function() {
  5. ////////////////////////////////
  6. // Valid
  7. ////////////////////////////////
  8. it('should run get example to build up an exits schema', function(done) {
  9. var machine = {
  10. inputs: {
  11. foo: {
  12. example: 'hello'
  13. }
  14. },
  15. exits: {
  16. success: {
  17. getExample: function(inputs, env) {
  18. return {
  19. code: 400,
  20. status: 'ok'
  21. };
  22. }
  23. },
  24. error: {}
  25. },
  26. fn: function (inputs, exits, deps) {
  27. exits.success({ code: 404, status: 'not found' });
  28. }
  29. };
  30. M.build(machine)
  31. .configure({
  32. foo: 'bar'
  33. })
  34. .exec(function(err, result) {
  35. if(err) { return done(err); }
  36. assert.strictEqual(result.code, 404);
  37. assert.strictEqual(result.status, 'not found');
  38. done();
  39. });
  40. });
  41. it('should coerce run-time exit values', function(done) {
  42. var machine = {
  43. inputs: {
  44. foo: {
  45. example: 'hello'
  46. }
  47. },
  48. exits: {
  49. success: {
  50. getExample: function(inputs, env) {
  51. return {
  52. code: 400,
  53. status: 'ok'
  54. };
  55. }
  56. },
  57. error: {}
  58. },
  59. fn: function (inputs, exits, deps) {
  60. exits.success({ code: '404', status: 'not found' });
  61. }
  62. };
  63. M.build(machine)
  64. .configure({
  65. foo: 'world'
  66. })
  67. .exec(function(err, result) {
  68. if(err) { return done(err); }
  69. assert.strictEqual(result.code, 404);
  70. assert.strictEqual(result.status, 'not found');
  71. done();
  72. });
  73. });
  74. describe('if getExample() returns a multi-item array', function (){
  75. it('should use the first item of the array as the pattern', function(done) {
  76. M.build({
  77. inputs: { foo: { example: 'hello' } },
  78. exits: { success: {
  79. getExample: function(inputs, env) { return [true, 'asdf',1234]; }
  80. } },
  81. fn: function (inputs, exits, deps) {
  82. return exits.success(['some string', -43.5, true, 1, 0]);
  83. }
  84. })
  85. .configure({ foo: 'world' })
  86. .exec(function(err, result) {
  87. if(err) { return done(err); }
  88. assert.deepEqual(result, [false, false, true, true, false]);
  89. done();
  90. });
  91. });
  92. });
  93. describe('if getExample() returns a nested multi-item array', function (){
  94. it('should use the first item of the array as the pattern', function(done) {
  95. M.build({
  96. inputs: { foo: { example: 'hello' } },
  97. exits: { success: {
  98. getExample: function(inputs, env) { return {x: [true, 'asdf',1234]}; }
  99. } },
  100. fn: function (inputs, exits, deps) {
  101. return exits.success({x: ['some string', -43.5, true, 1, 0] });
  102. }
  103. })
  104. .configure({ foo: 'world' })
  105. .exec(function(err, result) {
  106. if(err) { return done(err); }
  107. assert.deepEqual(result, {x: [false, false, true, true, false]});
  108. done();
  109. });
  110. });
  111. });
  112. describe('if getExample() returns array w/ `null` item', function (){
  113. it('should strip the null item out of the example', function(done) {
  114. var nov5 = new Date('Thursday, November 5, 1605 GMT');
  115. M.build({
  116. inputs: { foo: { example: 'hello' } },
  117. exits: { success: {
  118. getExample: function(inputs, env) { return [null]; }
  119. } },
  120. fn: function (inputs, exits, deps) {
  121. return exits.success([nov5]);
  122. }
  123. })
  124. .configure({ foo: 'world' })
  125. .exec(function(err, result) {
  126. if(err) { return done(err); }
  127. assert(!_.isEqual(result[0], nov5), 'Should not treat example as [===] when getExample returns [null]! Expected Date reference to be coerced to a JSON string (since example should have been coerced to `[]`)');
  128. assert.strictEqual(result[0], '1605-11-05T00:00:00.000Z');
  129. done();
  130. });
  131. });
  132. });
  133. describe('if getExample() returns dictionary w/ `null` properties', function (){
  134. it('should strip them out of the example', function(done) {
  135. M.build({
  136. inputs: { foo: { example: 'hello' } },
  137. exits: { success: {
  138. getExample: function(inputs, env) { return {a: null, b: null, c: 'things', d: null}; }
  139. } },
  140. fn: function (inputs, exits, deps) {
  141. return exits.success({a:1,b:2,c:3,d:4});
  142. }
  143. })
  144. .configure({ foo: 'world' })
  145. .exec(function(err, result) {
  146. if(err) { return done(err); }
  147. assert.deepEqual(result, {c: '3'});
  148. done();
  149. });
  150. });
  151. });
  152. describe('if getExample() returns null', function (){
  153. it('should treat it as `undefined`/"===" and pass runtime output through by reference', function(done) {
  154. var someStream = new require('stream').Readable();
  155. M.build({
  156. inputs: { foo: { example: 'hello' } },
  157. exits: { success: {
  158. getExample: function(inputs, env) { return null; }
  159. } },
  160. fn: function (inputs, exits, deps) {
  161. return exits.success(someStream);
  162. }
  163. })
  164. .configure({
  165. foo: 'world'
  166. })
  167. .exec(function(err, result) {
  168. if(err) { return done(err); }
  169. assert.strictEqual(result, someStream);
  170. done();
  171. });
  172. });
  173. });
  174. describe('if getExample() returns undefined', function (){
  175. it('should treat it as "===" and pass runtime output through by reference', function(done) {
  176. var someStream = new require('stream').Readable();
  177. M.build({
  178. inputs: { foo: { example: 'hello' } },
  179. exits: { success: {
  180. getExample: function(inputs, env) { return null; }
  181. } },
  182. fn: function (inputs, exits, deps) {
  183. return exits.success(someStream);
  184. }
  185. })
  186. .configure({
  187. foo: 'world'
  188. })
  189. .exec(function(err, result) {
  190. if(err) { return done(err); }
  191. assert.strictEqual(result, someStream);
  192. done();
  193. });
  194. });
  195. });
  196. ////////////////////////////////
  197. // Invalid
  198. ////////////////////////////////
  199. it('should return base types when the run-time inputs don\'t match the results of getExample', function(done) {
  200. var machine = {
  201. inputs: {
  202. foo: {
  203. example: 'hello'
  204. }
  205. },
  206. exits: {
  207. success: {
  208. getExample: function(inputs, env) {
  209. return {
  210. code: 400,
  211. status: 'ok'
  212. };
  213. }
  214. },
  215. error: {}
  216. },
  217. fn: function (inputs, exits, deps) {
  218. exits.success({ foo: 'bar', bar: 'baz' });
  219. }
  220. };
  221. M.build(machine)
  222. .configure({
  223. foo: 'hello'
  224. })
  225. .exec(function(err, result) {
  226. if(err) { return done(err); }
  227. assert.strictEqual(result.code, 0);
  228. assert.strictEqual(result.status, '');
  229. done();
  230. });
  231. });
  232. });