demuxSync.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var assert = require('assert');
  2. var Machine = require('../');
  3. describe('Machine.prototype.demuxSync()', function (){
  4. describe('with no arguments', function (){
  5. it('should throw when called on a machine that does not declare sync:true', function (){
  6. try {
  7. Machine.build(machineFixtures[0]).demuxSync();
  8. }
  9. catch (e) {
  10. assert.equal(e.code,'E_USAGE');
  11. return;
  12. }
  13. // Should never make it here
  14. assert(false, 'Should have thrown an error');
  15. });
  16. it('should return `true` when called on a configured machine that triggers its success exit', function (){
  17. var result = Machine.build(machineFixtures[1]).demuxSync();
  18. assert.equal(result,true);
  19. });
  20. it('should return `false` when called on a configured machine that triggers any non-success exit and sends back an Error instance', function (){
  21. var m = Machine.build(machineFixtures[2]);
  22. m.configure({value: 'hi'});
  23. assert.equal(m.demuxSync(), false);
  24. });
  25. it('should return `false` when called on a configured machine that triggers any non-success exit and sends back a string', function (){
  26. var m = Machine.build(machineFixtures[2]);
  27. m.configure({value: 'hi'});
  28. assert.equal(m.demuxSync(), false);
  29. });
  30. it('should return `false` when called on a configured machine that triggers any non-success exit and sends back a boolean', function (){
  31. var m = Machine.build(machineFixtures[2]);
  32. m.configure({value: true});
  33. assert.equal(m.demuxSync(), false);
  34. });
  35. it('should return `false` when called on a configured machine that triggers any non-success exit and sends back a number', function (){
  36. var m = Machine.build(machineFixtures[2]);
  37. m.configure({value: -234.2});
  38. assert.equal(m.demuxSync(), false);
  39. });
  40. it('should return `false` when called on a configured machine that triggers any non-success exit and sends back an array', function (){
  41. var m = Machine.build(machineFixtures[2]);
  42. m.configure({value: ['stuff', 'and', 'things']});
  43. assert.equal(m.demuxSync(), false);
  44. });
  45. it('should return `false` when called on a configured machine that triggers any non-success exit and sends back an object', function (){
  46. var m = Machine.build(machineFixtures[2]);
  47. m.configure({value: {
  48. email: 'ricketorsomething@stark.io'
  49. }});
  50. assert.equal(m.demuxSync(), false);
  51. });
  52. });
  53. describe('with custom exit name specified', function (){
  54. it('should return `true` when called on a configured machine that triggers the specified exit', function (){
  55. var result = Machine.build(machineFixtures[2]).demuxSync('someOtherExit');
  56. assert.equal(result,true);
  57. });
  58. it('should return `false` when called on a configured machine that triggers any other exit', function (){
  59. var result = Machine.build(machineFixtures[1]).demuxSync('someOtherExit');
  60. assert.equal(result,false);
  61. });
  62. it('should throw if the exit name passed to `.demuxSync()` does not match a known exit', function (){
  63. assert.throws(function (){
  64. Machine.build(machineFixtures[1]).demuxSync('someFakeExitThatDoesntActuallyExist');
  65. });
  66. });
  67. });
  68. });
  69. var machineFixtures = [
  70. // Standard asynchronous machine
  71. {
  72. inputs: {
  73. foo: {
  74. example: 100
  75. },
  76. bar: {
  77. example: 'foobar'
  78. }
  79. },
  80. exits: {
  81. success: {
  82. example: 'hello'
  83. },
  84. error: {
  85. example: 'world'
  86. }
  87. },
  88. fn: function (inputs, exits) {
  89. exits.success('stuff');
  90. }
  91. },
  92. // Same machine as above, but with sync:true
  93. {
  94. sync: true,
  95. inputs: {
  96. foo: {
  97. example: 100
  98. },
  99. bar: {
  100. example: 'foobar'
  101. }
  102. },
  103. exits: {
  104. success: {
  105. example: 'hello'
  106. },
  107. someOtherExit: {},
  108. error: {
  109. example: 'world'
  110. }
  111. },
  112. fn: function (inputs, exits) {
  113. exits.success('stuff');
  114. }
  115. },
  116. // Sync machine which calls a non-success exit with whatever
  117. // was passed into the `value` input.
  118. {
  119. sync: true,
  120. inputs: {
  121. value: {
  122. typeclass: '*'
  123. }
  124. },
  125. exits: {
  126. success: {
  127. example: 'hello'
  128. },
  129. someOtherExit: {
  130. getExample: function (inputs){
  131. return inputs.value;
  132. }
  133. },
  134. error: {
  135. example: 'world'
  136. }
  137. },
  138. fn: function (inputs, exits) {
  139. exits.someOtherExit(inputs.value);
  140. }
  141. },
  142. // Sync machine which calls a non-success exit with a new Error.
  143. {
  144. sync: true,
  145. inputs: {
  146. value: {
  147. typeclass: '*'
  148. }
  149. },
  150. exits: {
  151. success: {
  152. example: 'hello'
  153. },
  154. someOtherExit: {
  155. getExample: function (inputs){
  156. return inputs.value;
  157. }
  158. },
  159. error: {
  160. example: 'world'
  161. }
  162. },
  163. fn: function (inputs, exits) {
  164. exits.someOtherExit(new Error('deliberate error'));
  165. }
  166. }
  167. ];