HookCodeFactory.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const HookCodeFactory = require("../HookCodeFactory");
  7. const expectNoSyntaxError = (code) => {
  8. new Function("a, b, c", code);
  9. };
  10. describe("HookCodeFactory", () => {
  11. describe("callTap", () => {
  12. const factoryConfigurations = {
  13. "no args, no intercept": {
  14. args: [],
  15. taps: [
  16. {
  17. type: "sync"
  18. },
  19. {
  20. type: "async"
  21. },
  22. {
  23. type: "promise"
  24. }
  25. ],
  26. interceptors: []
  27. },
  28. "with args, no intercept": {
  29. args: ["a", "b", "c"],
  30. taps: [
  31. {
  32. type: "sync"
  33. },
  34. {
  35. type: "async"
  36. },
  37. {
  38. type: "promise"
  39. }
  40. ],
  41. interceptors: []
  42. },
  43. "with args, with intercept": {
  44. args: ["a", "b", "c"],
  45. taps: [
  46. {
  47. type: "sync"
  48. },
  49. {
  50. type: "async"
  51. },
  52. {
  53. type: "promise"
  54. }
  55. ],
  56. interceptors: [
  57. {
  58. call: () => {},
  59. tap: () => {}
  60. },
  61. {
  62. tap: () => {}
  63. },
  64. {
  65. call: () => {}
  66. },
  67. ]
  68. }
  69. }
  70. for(const configurationName in factoryConfigurations) {
  71. describe(`(${configurationName})`, () => {
  72. let factory;
  73. beforeEach(() => {
  74. factory = new HookCodeFactory();
  75. factory.init(factoryConfigurations[configurationName]);
  76. });
  77. it("sync without onResult", () => {
  78. const code = factory.callTap(0, {
  79. onError: err => `onError(${err});\n`,
  80. onDone: () => "onDone();\n"
  81. });
  82. expect(code).toMatchSnapshot();
  83. expectNoSyntaxError(code);
  84. });
  85. it("sync with onResult", () => {
  86. const code = factory.callTap(0, {
  87. onError: err => `onError(${err});\n`,
  88. onResult: result => `onResult(${result});\n`
  89. });
  90. expect(code).toMatchSnapshot();
  91. expectNoSyntaxError(code);
  92. });
  93. it("async without onResult", () => {
  94. const code = factory.callTap(1, {
  95. onError: err => `onError(${err});\n`,
  96. onDone: () => "onDone();\n"
  97. });
  98. expect(code).toMatchSnapshot();
  99. expectNoSyntaxError(code);
  100. });
  101. it("async with onResult", () => {
  102. const code = factory.callTap(1, {
  103. onError: err => `onError(${err});\n`,
  104. onResult: result => `onResult(${result});\n`
  105. });
  106. expect(code).toMatchSnapshot();
  107. expectNoSyntaxError(code);
  108. });
  109. it("promise without onResult", () => {
  110. const code = factory.callTap(2, {
  111. onError: err => `onError(${err});\n`,
  112. onDone: () => "onDone();\n"
  113. });
  114. expect(code).toMatchSnapshot();
  115. expectNoSyntaxError(code);
  116. });
  117. it("promise with onResult", () => {
  118. const code = factory.callTap(2, {
  119. onError: err => `onError(${err});\n`,
  120. onResult: result => `onResult(${result});\n`
  121. });
  122. expect(code).toMatchSnapshot();
  123. expectNoSyntaxError(code);
  124. });
  125. });
  126. }
  127. });
  128. describe("taps", () => {
  129. const factoryConfigurations = {
  130. "none": {
  131. args: ["a", "b", "c"],
  132. taps: [],
  133. interceptors: []
  134. },
  135. "single sync": {
  136. args: ["a", "b", "c"],
  137. taps: [
  138. {
  139. type: "sync"
  140. }
  141. ],
  142. interceptors: []
  143. },
  144. "multiple sync": {
  145. args: ["a", "b", "c"],
  146. taps: [
  147. {
  148. type: "sync"
  149. },
  150. {
  151. type: "sync"
  152. },
  153. {
  154. type: "sync"
  155. }
  156. ],
  157. interceptors: []
  158. },
  159. "single async": {
  160. args: ["a", "b", "c"],
  161. taps: [
  162. {
  163. type: "async"
  164. }
  165. ],
  166. interceptors: []
  167. },
  168. "single promise": {
  169. args: ["a", "b", "c"],
  170. taps: [
  171. {
  172. type: "promise"
  173. }
  174. ],
  175. interceptors: []
  176. },
  177. "mixed": {
  178. args: ["a", "b", "c"],
  179. taps: [
  180. {
  181. type: "sync"
  182. },
  183. {
  184. type: "async"
  185. },
  186. {
  187. type: "promise"
  188. }
  189. ],
  190. interceptors: []
  191. },
  192. "mixed2": {
  193. args: ["a", "b", "c"],
  194. taps: [
  195. {
  196. type: "async"
  197. },
  198. {
  199. type: "promise"
  200. },
  201. {
  202. type: "sync"
  203. },
  204. ],
  205. interceptors: []
  206. },
  207. }
  208. for(const configurationName in factoryConfigurations) {
  209. describe(`(${configurationName})`, () => {
  210. let factory;
  211. beforeEach(() => {
  212. factory = new HookCodeFactory();
  213. factory.init(factoryConfigurations[configurationName]);
  214. });
  215. it("callTapsSeries", () => {
  216. const code = factory.callTapsSeries({
  217. onError: (i, err) => `onError(${i}, ${err});\n`,
  218. onResult: (i, result, next, doneBreak) => `onResult(${i}, ${result}, () => {\n${next()}}, () => {\n${doneBreak()}});\n`,
  219. onDone: () => "onDone();\n",
  220. rethrowIfPossible: true
  221. });
  222. expect(code).toMatchSnapshot();
  223. expectNoSyntaxError(code);
  224. });
  225. it("callTapsParallel", () => {
  226. const code = factory.callTapsParallel({
  227. onError: (i, err) => `onError(${i}, ${err});\n`,
  228. onResult: (i, result, done, doneBreak) => `onResult(${i}, ${result}, () => {\n${done()}}, () => {\n${doneBreak()}});\n`,
  229. onDone: () => "onDone();\n",
  230. rethrowIfPossible: true
  231. });
  232. expect(code).toMatchSnapshot();
  233. expectNoSyntaxError(code);
  234. });
  235. it("callTapsLooping", () => {
  236. const code = factory.callTapsLooping({
  237. onError: (i, err) => `onError(${i}, ${err});\n`,
  238. onDone: () => "onDone();\n",
  239. rethrowIfPossible: true
  240. });
  241. expect(code).toMatchSnapshot();
  242. expectNoSyntaxError(code);
  243. });
  244. });
  245. }
  246. });
  247. });