event_utils_spec.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. var _event_utils = require("../../web/event_utils.js");
  24. var _is_node = require("../../shared/is_node.js");
  25. describe("event_utils", function () {
  26. describe("EventBus", function () {
  27. it("dispatch event", function () {
  28. const eventBus = new _event_utils.EventBus();
  29. let count = 0;
  30. eventBus.on("test", function (evt) {
  31. expect(evt).toEqual(undefined);
  32. count++;
  33. });
  34. eventBus.dispatch("test");
  35. expect(count).toEqual(1);
  36. });
  37. it("dispatch event with arguments", function () {
  38. const eventBus = new _event_utils.EventBus();
  39. let count = 0;
  40. eventBus.on("test", function (evt) {
  41. expect(evt).toEqual({
  42. abc: 123
  43. });
  44. count++;
  45. });
  46. eventBus.dispatch("test", {
  47. abc: 123
  48. });
  49. expect(count).toEqual(1);
  50. });
  51. it("dispatch different event", function () {
  52. const eventBus = new _event_utils.EventBus();
  53. let count = 0;
  54. eventBus.on("test", function () {
  55. count++;
  56. });
  57. eventBus.dispatch("nottest");
  58. expect(count).toEqual(0);
  59. });
  60. it("dispatch event multiple times", function () {
  61. const eventBus = new _event_utils.EventBus();
  62. let count = 0;
  63. eventBus.dispatch("test");
  64. eventBus.on("test", function () {
  65. count++;
  66. });
  67. eventBus.dispatch("test");
  68. eventBus.dispatch("test");
  69. expect(count).toEqual(2);
  70. });
  71. it("dispatch event to multiple handlers", function () {
  72. const eventBus = new _event_utils.EventBus();
  73. let count = 0;
  74. eventBus.on("test", function () {
  75. count++;
  76. });
  77. eventBus.on("test", function () {
  78. count++;
  79. });
  80. eventBus.dispatch("test");
  81. expect(count).toEqual(2);
  82. });
  83. it("dispatch to detached", function () {
  84. const eventBus = new _event_utils.EventBus();
  85. let count = 0;
  86. const listener = function () {
  87. count++;
  88. };
  89. eventBus.on("test", listener);
  90. eventBus.dispatch("test");
  91. eventBus.off("test", listener);
  92. eventBus.dispatch("test");
  93. expect(count).toEqual(1);
  94. });
  95. it("dispatch to wrong detached", function () {
  96. const eventBus = new _event_utils.EventBus();
  97. let count = 0;
  98. eventBus.on("test", function () {
  99. count++;
  100. });
  101. eventBus.dispatch("test");
  102. eventBus.off("test", function () {
  103. count++;
  104. });
  105. eventBus.dispatch("test");
  106. expect(count).toEqual(2);
  107. });
  108. it("dispatch to detached during handling", function () {
  109. const eventBus = new _event_utils.EventBus();
  110. let count = 0;
  111. const listener1 = function () {
  112. eventBus.off("test", listener2);
  113. count++;
  114. };
  115. const listener2 = function () {
  116. eventBus.off("test", listener1);
  117. count++;
  118. };
  119. eventBus.on("test", listener1);
  120. eventBus.on("test", listener2);
  121. eventBus.dispatch("test");
  122. eventBus.dispatch("test");
  123. expect(count).toEqual(2);
  124. });
  125. it("dispatch event to handlers with/without 'once' option", function () {
  126. const eventBus = new _event_utils.EventBus();
  127. let multipleCount = 0,
  128. onceCount = 0;
  129. eventBus.on("test", function () {
  130. multipleCount++;
  131. });
  132. eventBus.on("test", function () {
  133. onceCount++;
  134. }, {
  135. once: true
  136. });
  137. eventBus.dispatch("test");
  138. eventBus.dispatch("test");
  139. eventBus.dispatch("test");
  140. expect(multipleCount).toEqual(3);
  141. expect(onceCount).toEqual(1);
  142. });
  143. it("should not re-dispatch to DOM", async function () {
  144. if (_is_node.isNodeJS) {
  145. pending("Document is not supported in Node.js.");
  146. }
  147. const eventBus = new _event_utils.EventBus();
  148. let count = 0;
  149. eventBus.on("test", function (evt) {
  150. expect(evt).toEqual(undefined);
  151. count++;
  152. });
  153. function domEventListener() {
  154. expect(false).toEqual(true);
  155. }
  156. document.addEventListener("test", domEventListener);
  157. eventBus.dispatch("test");
  158. await Promise.resolve();
  159. expect(count).toEqual(1);
  160. document.removeEventListener("test", domEventListener);
  161. });
  162. });
  163. describe("waitOnEventOrTimeout", function () {
  164. let eventBus;
  165. beforeAll(function () {
  166. eventBus = new _event_utils.EventBus();
  167. });
  168. afterAll(function () {
  169. eventBus = null;
  170. });
  171. it("should reject invalid parameters", async function () {
  172. const invalidTarget = (0, _event_utils.waitOnEventOrTimeout)({
  173. target: "window",
  174. name: "DOMContentLoaded"
  175. }).then(function () {
  176. expect(false).toEqual(true);
  177. }, function (reason) {
  178. expect(reason instanceof Error).toEqual(true);
  179. });
  180. const invalidName = (0, _event_utils.waitOnEventOrTimeout)({
  181. target: eventBus,
  182. name: ""
  183. }).then(function () {
  184. expect(false).toEqual(true);
  185. }, function (reason) {
  186. expect(reason instanceof Error).toEqual(true);
  187. });
  188. const invalidDelay = (0, _event_utils.waitOnEventOrTimeout)({
  189. target: eventBus,
  190. name: "pagerendered",
  191. delay: -1000
  192. }).then(function () {
  193. expect(false).toEqual(true);
  194. }, function (reason) {
  195. expect(reason instanceof Error).toEqual(true);
  196. });
  197. await Promise.all([invalidTarget, invalidName, invalidDelay]);
  198. });
  199. it("should resolve on event, using the DOM", async function () {
  200. if (_is_node.isNodeJS) {
  201. pending("Document is not supported in Node.js.");
  202. }
  203. const button = document.createElement("button");
  204. const buttonClicked = (0, _event_utils.waitOnEventOrTimeout)({
  205. target: button,
  206. name: "click",
  207. delay: 10000
  208. });
  209. button.click();
  210. const type = await buttonClicked;
  211. expect(type).toEqual(_event_utils.WaitOnType.EVENT);
  212. });
  213. it("should resolve on timeout, using the DOM", async function () {
  214. if (_is_node.isNodeJS) {
  215. pending("Document is not supported in Node.js.");
  216. }
  217. const button = document.createElement("button");
  218. const buttonClicked = (0, _event_utils.waitOnEventOrTimeout)({
  219. target: button,
  220. name: "click",
  221. delay: 10
  222. });
  223. const type = await buttonClicked;
  224. expect(type).toEqual(_event_utils.WaitOnType.TIMEOUT);
  225. });
  226. it("should resolve on event, using the EventBus", async function () {
  227. const pageRendered = (0, _event_utils.waitOnEventOrTimeout)({
  228. target: eventBus,
  229. name: "pagerendered",
  230. delay: 10000
  231. });
  232. eventBus.dispatch("pagerendered");
  233. const type = await pageRendered;
  234. expect(type).toEqual(_event_utils.WaitOnType.EVENT);
  235. });
  236. it("should resolve on timeout, using the EventBus", async function () {
  237. const pageRendered = (0, _event_utils.waitOnEventOrTimeout)({
  238. target: eventBus,
  239. name: "pagerendered",
  240. delay: 10
  241. });
  242. const type = await pageRendered;
  243. expect(type).toEqual(_event_utils.WaitOnType.TIMEOUT);
  244. });
  245. });
  246. });