SyncHook.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SyncHook = require("../SyncHook");
  8. describe("SyncHook", () => {
  9. it("should allow to create sync hooks", async () => {
  10. const h0 = new SyncHook();
  11. const h1 = new SyncHook(["test"]);
  12. const h2 = new SyncHook(["test", "arg2"]);
  13. const h3 = new SyncHook(["test", "arg2", "arg3"]);
  14. h0.call();
  15. await h0.promise();
  16. await new Promise(resolve => h0.callAsync(resolve));
  17. const mock0 = jest.fn();
  18. h0.tap("A", mock0);
  19. h0.call();
  20. expect(mock0).toHaveBeenLastCalledWith();
  21. const mock1 = jest.fn();
  22. h0.tap("B", mock1)
  23. h0.call();
  24. expect(mock1).toHaveBeenLastCalledWith();
  25. const mock2 = jest.fn();
  26. const mock3 = jest.fn();
  27. const mock4 = jest.fn();
  28. const mock5 = jest.fn();
  29. h1.tap("C", mock2);
  30. h2.tap("D", mock3);
  31. h3.tap("E", mock4);
  32. h3.tap("F", mock5);
  33. h1.call("1");
  34. h2.call("1", 2);
  35. h3.call("1", 2, 3);
  36. expect(mock2).toHaveBeenLastCalledWith("1");
  37. expect(mock3).toHaveBeenLastCalledWith("1", 2);
  38. expect(mock4).toHaveBeenLastCalledWith("1", 2, 3);
  39. expect(mock5).toHaveBeenLastCalledWith("1", 2, 3);
  40. await new Promise(resolve => h1.callAsync("a", resolve));
  41. await h2.promise("a", "b");
  42. await new Promise(resolve => h3.callAsync("a", "b", "c", resolve));
  43. expect(mock2).toHaveBeenLastCalledWith("a");
  44. expect(mock3).toHaveBeenLastCalledWith("a", "b");
  45. expect(mock4).toHaveBeenLastCalledWith("a", "b", "c");
  46. expect(mock5).toHaveBeenLastCalledWith("a", "b", "c");
  47. await h3.promise("x", "y");
  48. expect(mock4).toHaveBeenLastCalledWith("x", "y", undefined);
  49. expect(mock5).toHaveBeenLastCalledWith("x", "y", undefined);
  50. });
  51. it("should allow to intercept calls", () =>{
  52. const hook = new SyncHook(["arg1", "arg2"]);
  53. const mockCall = jest.fn();
  54. const mock0 = jest.fn();
  55. const mockRegister = jest.fn(x => ({
  56. name: "huh",
  57. type: "sync",
  58. fn: mock0
  59. }));
  60. const mock1 = jest.fn();
  61. hook.tap("Test1", mock1);
  62. hook.intercept({
  63. call: mockCall,
  64. register: mockRegister
  65. });
  66. const mock2 = jest.fn();
  67. hook.tap("Test2", mock2);
  68. hook.call(1, 2);
  69. expect(mockCall).toHaveBeenLastCalledWith(1, 2);
  70. expect(mockRegister).toHaveBeenLastCalledWith({
  71. type: "sync",
  72. name: "Test2",
  73. fn: mock2
  74. });
  75. expect(mock1).not.toHaveBeenLastCalledWith(1, 2);
  76. expect(mock2).not.toHaveBeenLastCalledWith(1, 2);
  77. expect(mock0).toHaveBeenLastCalledWith(1, 2);
  78. })
  79. });