MultiHook.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. require("babel-polyfill");
  7. const MultiHook = require("../MultiHook");
  8. describe("MultiHook", () => {
  9. const redirectedMethods = ["tap", "tapAsync", "tapPromise"];
  10. for(const name of redirectedMethods) {
  11. it(`should redirect ${name}`, () => {
  12. const calls = [];
  13. const fakeHook = {
  14. [name]: (options, fn) => {
  15. calls.push({ options, fn });
  16. }
  17. };
  18. new MultiHook([fakeHook, fakeHook])[name]("options", "fn");
  19. expect(calls).toEqual([
  20. { options: "options", fn: "fn" },
  21. { options: "options", fn: "fn" }
  22. ]);
  23. });
  24. }
  25. it("should redirect intercept", () => {
  26. const calls = [];
  27. const fakeHook = {
  28. intercept: (interceptor) => {
  29. calls.push(interceptor);
  30. }
  31. };
  32. new MultiHook([fakeHook, fakeHook]).intercept("interceptor");
  33. expect(calls).toEqual([
  34. "interceptor",
  35. "interceptor"
  36. ]);
  37. });
  38. it("should redirect withOptions", () => {
  39. const calls = [];
  40. const fakeHook = {
  41. withOptions: (options) => {
  42. calls.push(options);
  43. return {
  44. tap: (options, fn) => {
  45. calls.push({ options, fn });
  46. }
  47. }
  48. }
  49. };
  50. const newHook = new MultiHook([fakeHook, fakeHook]).withOptions("options");
  51. newHook.tap("options", "fn")
  52. expect(calls).toEqual([
  53. "options",
  54. "options",
  55. { options: "options", fn: "fn" },
  56. { options: "options", fn: "fn" }
  57. ]);
  58. });
  59. it("should redirect isUsed", () => {
  60. const calls = [];
  61. const fakeHook1 = {
  62. isUsed: () => {
  63. return true;
  64. }
  65. };
  66. const fakeHook2 = {
  67. isUsed: () => {
  68. return false;
  69. }
  70. };
  71. expect(new MultiHook([fakeHook1, fakeHook1]).isUsed()).toEqual(true);
  72. expect(new MultiHook([fakeHook1, fakeHook2]).isUsed()).toEqual(true);
  73. expect(new MultiHook([fakeHook2, fakeHook1]).isUsed()).toEqual(true);
  74. expect(new MultiHook([fakeHook2, fakeHook2]).isUsed()).toEqual(false);
  75. });
  76. });