Tapable.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Tapable = require("../Tapable");
  7. const SyncHook = require("../SyncHook");
  8. const HookMap = require("../HookMap");
  9. describe("Tapable", () => {
  10. it("should use same name or camelCase hook by default", () => {
  11. const t = new Tapable();
  12. t.hooks = {
  13. myHook: new SyncHook()
  14. };
  15. let called = 0;
  16. t.plugin("my-hook", () => called++);
  17. t.hooks.myHook.call();
  18. t.plugin("myHook", () => called += 10);
  19. t.hooks.myHook.call();
  20. expect(called).toEqual(12);
  21. });
  22. it("should throw on unknown hook", () => {
  23. const t = new Tapable();
  24. t.hooks = {
  25. myHook: new SyncHook()
  26. };
  27. expect(() => {
  28. t.plugin("some-hook", () => {});
  29. }).toThrow(/some-hook/);
  30. t.hooks.myHook.call();
  31. });
  32. it("should use custom mapping", () => {
  33. const t = new Tapable();
  34. t.hooks = {
  35. myHook: new SyncHook(),
  36. hookMap: new HookMap(name => new SyncHook())
  37. };
  38. let called = 0;
  39. t._pluginCompat.tap("hookMap custom mapping", options => {
  40. const match = /^hookMap (.+)$/.exec(options.name);
  41. if(match) {
  42. t.hooks.hookMap.tap(match[1], options.fn.name || "unnamed compat plugin", options.fn);
  43. return true;
  44. }
  45. })
  46. t.plugin("my-hook", () => called++);
  47. t.plugin("hookMap test", () => called += 10);
  48. t.hooks.myHook.call();
  49. expect(called).toEqual(1);
  50. t.hooks.hookMap.for("test").call();
  51. expect(called).toEqual(11);
  52. t.hooks.hookMap.for("other").call();
  53. expect(called).toEqual(11);
  54. });
  55. })