event.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /// <reference path="../typings/index.d.ts"/>
  2. import Event from '../lib/Event';
  3. import * as assert from 'assert';
  4. suite('Event handling', function() {
  5. let event: Event<string>;
  6. setup(function() {
  7. event = new Event();
  8. });
  9. test('no handlers', function() {
  10. assert(!event.hasHandlers, 'event should report any registered handlers');
  11. assert.doesNotThrow(function() {
  12. event.dispatch('');
  13. }, 'dispatch should not throw');
  14. });
  15. suite('one handler', function() {
  16. test('no context', function() {
  17. let called = false;
  18. event.addHandler(function(payload: string) {
  19. assert.equal(payload, 'foo');
  20. called = true;
  21. });
  22. assert(event.hasHandlers, 'event should report registered handlers');
  23. event.dispatch('foo');
  24. assert(called, 'the handler should have been called');
  25. });
  26. test('with context', function() {
  27. let foo = 0;
  28. function handler(payload: string, context: number) {
  29. assert.equal(payload, 'foo');
  30. foo = context;
  31. };
  32. event.addHandler(handler, 5).dispatch('foo');
  33. assert.equal(foo, 5, 'context was not transferred correctly');
  34. assert(event.removeHandler(handler).hasHandlers, 'handler should only be remvoved if contet matches');
  35. assert(!event.removeHandler(handler, 5).hasHandlers, 'handler should be remvoved if contet matches');
  36. });
  37. test('one handler, no context, multiple attachments', function() {
  38. let i = 0;
  39. function handler(): void {
  40. i++;
  41. }
  42. event.addHandler(handler).addHandler(handler).dispatch('foo');
  43. assert.equal(i, 1, 'handler should have been called once');
  44. assert(event.isHandlerAttached(handler), 'handler should be reported as attached');
  45. event.removeHandler(handler);
  46. assert(!event.isHandlerAttached(handler), 'handler should be reported as not attached after removal');
  47. });
  48. test('one handler, different contexts, multiple attachments', function() {
  49. let i = 0;
  50. function handler(): void {
  51. i++;
  52. }
  53. event.addHandler(handler, 1).addHandler(handler, 2).dispatch('foo');
  54. assert.equal(i, 2, 'handler should have been called twice');
  55. assert(!event.isHandlerAttached(handler), 'handler should be reported as not attached w/o context');
  56. assert(event.isHandlerAttached(handler, 1), 'handler should be reported as attached with proper context');
  57. });
  58. });
  59. suite('two handlers', function() {
  60. let called1: boolean, called2: boolean,
  61. contexts: Array<number>;
  62. function handler1(payload: string) {
  63. assert.equal(payload, 'foo');
  64. called1 = true;
  65. };
  66. function handler2(payload: string) {
  67. assert.equal(payload, 'foo');
  68. called2 = true;
  69. };
  70. function ctxHandler(payload: string, context: any) {
  71. contexts[context] = context;
  72. assert.equal(payload, 'foo');
  73. }
  74. setup(function() {
  75. called1 = called2 = false;
  76. contexts = [0, 0, 0];
  77. event
  78. .addHandler(handler1)
  79. .addHandler(handler2);
  80. });
  81. test('both attached', function() {
  82. event.dispatch('foo');
  83. assert(called1, 'handler 1 should have been called');
  84. assert(called2, 'handler 2 should have been called');
  85. });
  86. test('handler 1 detached', function() {
  87. event.removeHandler(handler1);
  88. event.dispatch('foo');
  89. assert(!called1, 'handler 1 should not have been called');
  90. assert(called2, 'handler 2 should have been called');
  91. });
  92. test('handler 2 detached', function() {
  93. event.removeHandler(handler2);
  94. event.dispatch('foo');
  95. assert(called1, 'handler 1 should have been called');
  96. assert(!called2, 'handler 2 should not have been called');
  97. });
  98. test('context, both attached', function() {
  99. event
  100. .addHandler(ctxHandler, 1)
  101. .addHandler(ctxHandler, 2)
  102. .dispatch('foo');
  103. assert.equal(contexts[1], 1, 'context 1 was not transferred correctly');
  104. assert.equal(contexts[2], 2, 'context 2 was not transferred correctly');
  105. });
  106. test('context, handler 1 detached', function() {
  107. event
  108. .addHandler(ctxHandler, 1)
  109. .addHandler(ctxHandler, 2)
  110. .removeHandler(ctxHandler, 1)
  111. .dispatch('foo');
  112. assert.equal(contexts[1], 0, 'handler was not detached');
  113. assert.equal(contexts[2], 2, 'context 2 was not transferred correctly');
  114. });
  115. test('context, handler 2 detached', function() {
  116. event
  117. .addHandler(ctxHandler, 1)
  118. .addHandler(ctxHandler, 2)
  119. .removeHandler(ctxHandler, 2)
  120. .dispatch('foo');
  121. assert.equal(contexts[1], 1, 'context 1 was not transferred correctly');
  122. assert.equal(contexts[2], 0, 'handler was not detached');
  123. });
  124. });
  125. });