Hook.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const SyncHook = require("../SyncHook");
  7. describe("Hook", () => {
  8. it("should allow to insert hooks before others and in stages", () => {
  9. const hook = new SyncHook();
  10. const calls = [];
  11. hook.tap("A", () => calls.push("A"));
  12. hook.tap({
  13. name: "B",
  14. before: "A"
  15. }, () => calls.push("B"));
  16. calls.length = 0;
  17. hook.call();
  18. expect(calls).toEqual(["B", "A"]);
  19. hook.tap({
  20. name: "C",
  21. before: ["A", "B"]
  22. }, () => calls.push("C"));
  23. calls.length = 0;
  24. hook.call();
  25. expect(calls).toEqual(["C", "B", "A"]);
  26. hook.tap({
  27. name: "D",
  28. before: "B"
  29. }, () => calls.push("D"));
  30. calls.length = 0;
  31. hook.call();
  32. expect(calls).toEqual(["C", "D", "B", "A"]);
  33. hook.tap({
  34. name: "E",
  35. stage: -5
  36. }, () => calls.push("E"));
  37. hook.tap({
  38. name: "F",
  39. stage: -3
  40. }, () => calls.push("F"));
  41. calls.length = 0;
  42. hook.call();
  43. expect(calls).toEqual(["E", "F", "C", "D", "B", "A"]);
  44. });
  45. });