ElggHooksTest.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. define(function(require) {
  2. var elgg = require('elgg');
  3. describe("elgg.hooks", function() {
  4. beforeEach(function() {
  5. elgg.config.hooks = {};
  6. elgg.provide('elgg.config.hooks.all.all');
  7. });
  8. describe("elgg.trigger_hook()", function() {
  9. it("return value defaults to null", function() {
  10. expect(elgg.trigger_hook("fee", "fum")).toBe(null);
  11. elgg.register_hook_handler('fee', 'fum', elgg.nullFunction);
  12. expect(elgg.trigger_hook("fee", "fum")).toBe(null);
  13. expect(elgg.trigger_hook('x', 'y', {}, null)).toBe(null);
  14. expect(elgg.trigger_hook('x', 'z', {}, false)).toBe(false);
  15. });
  16. it("handlers returning null/undefined don't change returnvalue", function() {
  17. elgg.register_hook_handler('test', 'test', elgg.nullFunction);
  18. expect(elgg.trigger_hook('test', 'test', {}, 1984)).toBe(1984);
  19. elgg.register_hook_handler('test', 'test', function(hook, type, params, value) {
  20. return undefined;
  21. });
  22. expect(elgg.trigger_hook('test', 'test', {}, 42)).toBe(42);
  23. });
  24. it("triggers handlers registered with 'all'", function() {
  25. elgg.register_hook_handler('all', 'bar', elgg.abstractMethod);
  26. expect(function() { elgg.trigger_hook('foo', 'bar'); }).toThrow();
  27. elgg.register_hook_handler('foo', 'all', elgg.abstractMethod);
  28. expect(function() { elgg.trigger_hook('foo', 'baz'); }).toThrow();
  29. elgg.register_hook_handler('all', 'all', elgg.abstractMethod);
  30. expect(function() { elgg.trigger_hook('pinky', 'winky'); }).toThrow();
  31. });
  32. });
  33. describe("elgg.register_hook_handler()", function() {
  34. it("only accepts functions as handlers", function() {
  35. expect(function() { elgg.register_hook_handler('str', 'str', 'oops'); }).toThrow();
  36. });
  37. });
  38. });
  39. });