ui_utils_spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var webUiUtils = require('../../web/ui_utils.js');
  17. var binarySearchFirstItem = webUiUtils.binarySearchFirstItem;
  18. var EventBus = webUiUtils.EventBus;
  19. describe('ui_utils', function () {
  20. describe('binary search', function () {
  21. function isTrue(boolean) {
  22. return boolean;
  23. }
  24. function isGreater3(number) {
  25. return number > 3;
  26. }
  27. it('empty array', function () {
  28. expect(binarySearchFirstItem([], isTrue)).toEqual(0);
  29. });
  30. it('single boolean entry', function () {
  31. expect(binarySearchFirstItem([false], isTrue)).toEqual(1);
  32. expect(binarySearchFirstItem([true], isTrue)).toEqual(0);
  33. });
  34. it('three boolean entries', function () {
  35. expect(binarySearchFirstItem([
  36. true,
  37. true,
  38. true
  39. ], isTrue)).toEqual(0);
  40. expect(binarySearchFirstItem([
  41. false,
  42. true,
  43. true
  44. ], isTrue)).toEqual(1);
  45. expect(binarySearchFirstItem([
  46. false,
  47. false,
  48. true
  49. ], isTrue)).toEqual(2);
  50. expect(binarySearchFirstItem([
  51. false,
  52. false,
  53. false
  54. ], isTrue)).toEqual(3);
  55. });
  56. it('three numeric entries', function () {
  57. expect(binarySearchFirstItem([
  58. 0,
  59. 1,
  60. 2
  61. ], isGreater3)).toEqual(3);
  62. expect(binarySearchFirstItem([
  63. 2,
  64. 3,
  65. 4
  66. ], isGreater3)).toEqual(2);
  67. expect(binarySearchFirstItem([
  68. 4,
  69. 5,
  70. 6
  71. ], isGreater3)).toEqual(0);
  72. });
  73. });
  74. describe('EventBus', function () {
  75. it('dispatch event', function () {
  76. var eventBus = new EventBus();
  77. var count = 0;
  78. eventBus.on('test', function () {
  79. count++;
  80. });
  81. eventBus.dispatch('test');
  82. expect(count).toEqual(1);
  83. });
  84. it('dispatch different event', function () {
  85. var eventBus = new EventBus();
  86. var count = 0;
  87. eventBus.on('test', function () {
  88. count++;
  89. });
  90. eventBus.dispatch('nottest');
  91. expect(count).toEqual(0);
  92. });
  93. it('dispatch event multiple times', function () {
  94. var eventBus = new EventBus();
  95. var count = 0;
  96. eventBus.dispatch('test');
  97. eventBus.on('test', function () {
  98. count++;
  99. });
  100. eventBus.dispatch('test');
  101. eventBus.dispatch('test');
  102. expect(count).toEqual(2);
  103. });
  104. it('dispatch event to multiple handlers', function () {
  105. var eventBus = new EventBus();
  106. var count = 0;
  107. eventBus.on('test', function () {
  108. count++;
  109. });
  110. eventBus.on('test', function () {
  111. count++;
  112. });
  113. eventBus.dispatch('test');
  114. expect(count).toEqual(2);
  115. });
  116. it('dispatch to detached', function () {
  117. var eventBus = new EventBus();
  118. var count = 0;
  119. var listener = function () {
  120. count++;
  121. };
  122. eventBus.on('test', listener);
  123. eventBus.dispatch('test');
  124. eventBus.off('test', listener);
  125. eventBus.dispatch('test');
  126. expect(count).toEqual(1);
  127. });
  128. it('dispatch to wrong detached', function () {
  129. var eventBus = new EventBus();
  130. var count = 0;
  131. eventBus.on('test', function () {
  132. count++;
  133. });
  134. eventBus.dispatch('test');
  135. eventBus.off('test', function () {
  136. count++;
  137. });
  138. eventBus.dispatch('test');
  139. expect(count).toEqual(2);
  140. });
  141. it('dispatch to detached during handling', function () {
  142. var eventBus = new EventBus();
  143. var count = 0;
  144. var listener1 = function () {
  145. eventBus.off('test', listener2);
  146. count++;
  147. };
  148. var listener2 = function () {
  149. eventBus.off('test', listener1);
  150. count++;
  151. };
  152. eventBus.on('test', listener1);
  153. eventBus.on('test', listener2);
  154. eventBus.dispatch('test');
  155. eventBus.dispatch('test');
  156. expect(count).toEqual(2);
  157. });
  158. });
  159. });