PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/test/snr/events/test-eventdispatcher.js

https://bitbucket.org/sapientRhinos/sapientrhinos
JavaScript | 178 lines | 131 code | 20 blank | 27 comment | 0 complexity | 62c23e5e03550ec1788cd4e2ae805e60 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Test case suite for event dispatcher.
  3. *
  4. * @author <a href='mailto:jhok@sapient.com'>Johnathan Hok</a>
  5. *
  6. * Copyright (C) 2012 SapientNitro
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  17. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. var main = (function($, document, window, undefined) {
  20. var dispatcher;
  21. /**
  22. *
  23. */
  24. function init() {
  25. module('EventDispatcher.add()', {
  26. setup: function() {
  27. dispatcher = new SNR.events.EventDispatcher();
  28. }
  29. });
  30. test('event with handler', function() {
  31. var eventName = 'simple';
  32. dispatcher.add(eventName, function() {});
  33. ok(true, 'added event');
  34. });
  35. test('event with handler accepting data', function() {
  36. var eventName = 'simple with data';
  37. dispatcher.add(eventName, function(data) {});
  38. ok(true, 'added event');
  39. });
  40. test('events with the same name', function() {
  41. var eventName = 'same events';
  42. dispatcher.add(eventName, function() {});
  43. dispatcher.add(eventName, function() {});
  44. ok(true, 'added events');
  45. });
  46. module('EventDispatcher.dispatch()', {
  47. setup: function() {
  48. dispatcher = new SNR.events.EventDispatcher();
  49. }
  50. });
  51. test('event with handler', function() {
  52. var eventName = 'simple';
  53. dispatcher.add(eventName, function() {
  54. ok(true, 'received event');
  55. });
  56. dispatcher.dispatch(eventName);
  57. });
  58. test('event with handler accepting data', function() {
  59. var eventName = 'simple with data';
  60. var data = {
  61. 'bool': true,
  62. 'int': 1,
  63. 'string': 'abc',
  64. 'array': [1, 2, 3],
  65. 'object': {}
  66. };
  67. dispatcher.add(eventName, function(data) {
  68. deepEqual(data, data, 'received event');
  69. });
  70. dispatcher.dispatch(eventName, data);
  71. });
  72. test('events with multiple listener instances', function() {
  73. expect(3);
  74. var eventName = 'same events';
  75. dispatcher.add(eventName, function() {
  76. ok(true, 'received in 1st event');
  77. });
  78. dispatcher.add(eventName, function() {
  79. ok(true, 'received in 2nd event');
  80. });
  81. dispatcher.add(eventName, function() {
  82. ok(true, 'received in 3rd event');
  83. });
  84. dispatcher.dispatch(eventName);
  85. });
  86. test('event that does not exist', function() {
  87. expect(1);
  88. var eventName = 'does not exist';
  89. dispatcher.dispatch(eventName);
  90. ok(true, 'does not call any events');
  91. });
  92. module('EventDispatcher.remove()', {
  93. setup: function() {
  94. dispatcher = new SNR.events.EventDispatcher();
  95. }
  96. });
  97. test('event with named function handler', function() {
  98. expect(0);
  99. var eventName = 'simple';
  100. var handler = function() {
  101. ok(false, 'is not removed');
  102. };
  103. dispatcher.add(eventName, handler);
  104. dispatcher.remove(eventName, handler);
  105. dispatcher.dispatch(eventName);
  106. });
  107. test('event with named function handler accepting data', function() {
  108. expect(0);
  109. var eventName = 'simple';
  110. var data = {
  111. 'bool': true,
  112. 'int': 1,
  113. 'string': 'abc',
  114. 'array': [1, 2, 3],
  115. 'object': {}
  116. };
  117. var handler = function(data) {
  118. ok(false, 'is not removed');
  119. };
  120. dispatcher.add(eventName, handler);
  121. dispatcher.remove(eventName, handler);
  122. dispatcher.dispatch(eventName);
  123. });
  124. test('events with multiple listener instances', function() {
  125. expect(0);
  126. var eventName = 'simple';
  127. var handler = function(data) {
  128. ok(false, 'is not removed');
  129. };
  130. dispatcher.add(eventName, handler);
  131. dispatcher.add(eventName, handler);
  132. dispatcher.add(eventName, handler);
  133. dispatcher.remove(eventName, handler);
  134. dispatcher.dispatch(eventName);
  135. });
  136. test('event with anonymous function handler', function() {
  137. expect(0);
  138. var eventName = 'simple';
  139. dispatcher.add(eventName, function() {
  140. ok(false, 'is not removed');
  141. });
  142. dispatcher.remove(eventName);
  143. dispatcher.dispatch(eventName);
  144. });
  145. }
  146. /**
  147. * Expose API publicly
  148. */
  149. return {
  150. init: init
  151. };
  152. }(jQuery, document, window, undefined));
  153. /**
  154. *
  155. */
  156. jQuery(document).ready(function() {
  157. main.init();
  158. });