event-bus.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import { firstValueFrom, Subject } from 'rxjs';
  2. import { QueryRunner } from 'typeorm';
  3. import { beforeEach, describe, expect, it, vi } from 'vitest';
  4. import { EventBus } from './event-bus';
  5. import { VendureEvent } from './vendure-event';
  6. class MockTransactionSubscriber {
  7. awaitRelease(queryRunner: QueryRunner): Promise<QueryRunner> {
  8. return Promise.resolve(queryRunner);
  9. }
  10. }
  11. describe('EventBus', () => {
  12. let eventBus: EventBus;
  13. beforeEach(() => {
  14. eventBus = new EventBus(new MockTransactionSubscriber() as any);
  15. });
  16. it('can publish without subscribers', () => {
  17. const event = new TestEvent('foo');
  18. expect(async () => await eventBus.publish(event)).not.toThrow();
  19. });
  20. describe('ofType()', () => {
  21. it('single handler is called once', async () => {
  22. const handler = vi.fn();
  23. const event = new TestEvent('foo');
  24. eventBus.ofType(TestEvent).subscribe(handler);
  25. await eventBus.publish(event);
  26. await new Promise(resolve => setImmediate(resolve));
  27. expect(handler).toHaveBeenCalledTimes(1);
  28. expect(handler).toHaveBeenCalledWith(event);
  29. });
  30. it('single handler is called on multiple events', async () => {
  31. const handler = vi.fn();
  32. const event1 = new TestEvent('foo');
  33. const event2 = new TestEvent('bar');
  34. const event3 = new TestEvent('baz');
  35. eventBus.ofType(TestEvent).subscribe(handler);
  36. await eventBus.publish(event1);
  37. await eventBus.publish(event2);
  38. await eventBus.publish(event3);
  39. await new Promise(resolve => setImmediate(resolve));
  40. expect(handler).toHaveBeenCalledTimes(3);
  41. expect(handler).toHaveBeenCalledWith(event1);
  42. expect(handler).toHaveBeenCalledWith(event2);
  43. expect(handler).toHaveBeenCalledWith(event3);
  44. });
  45. it('multiple handler are called', async () => {
  46. const handler1 = vi.fn();
  47. const handler2 = vi.fn();
  48. const handler3 = vi.fn();
  49. const event = new TestEvent('foo');
  50. eventBus.ofType(TestEvent).subscribe(handler1);
  51. eventBus.ofType(TestEvent).subscribe(handler2);
  52. eventBus.ofType(TestEvent).subscribe(handler3);
  53. await eventBus.publish(event);
  54. await new Promise(resolve => setImmediate(resolve));
  55. expect(handler1).toHaveBeenCalledWith(event);
  56. expect(handler2).toHaveBeenCalledWith(event);
  57. expect(handler3).toHaveBeenCalledWith(event);
  58. });
  59. it('handler is not called for other events', async () => {
  60. const handler = vi.fn();
  61. const event = new OtherTestEvent('foo');
  62. eventBus.ofType(TestEvent).subscribe(handler);
  63. await eventBus.publish(event);
  64. await new Promise(resolve => setImmediate(resolve));
  65. expect(handler).not.toHaveBeenCalled();
  66. });
  67. it('ofType() returns a subscription', async () => {
  68. const handler = vi.fn();
  69. const event = new TestEvent('foo');
  70. const subscription = eventBus.ofType(TestEvent).subscribe(handler);
  71. await eventBus.publish(event);
  72. await new Promise(resolve => setImmediate(resolve));
  73. expect(handler).toHaveBeenCalledTimes(1);
  74. subscription.unsubscribe();
  75. await eventBus.publish(event);
  76. await eventBus.publish(event);
  77. await new Promise(resolve => setImmediate(resolve));
  78. expect(handler).toHaveBeenCalledTimes(1);
  79. });
  80. it('unsubscribe() only unsubscribes own handler', async () => {
  81. const handler1 = vi.fn();
  82. const handler2 = vi.fn();
  83. const event = new TestEvent('foo');
  84. const subscription1 = eventBus.ofType(TestEvent).subscribe(handler1);
  85. const subscription2 = eventBus.ofType(TestEvent).subscribe(handler2);
  86. await eventBus.publish(event);
  87. await new Promise(resolve => setImmediate(resolve));
  88. expect(handler1).toHaveBeenCalledTimes(1);
  89. expect(handler2).toHaveBeenCalledTimes(1);
  90. subscription1.unsubscribe();
  91. await eventBus.publish(event);
  92. await eventBus.publish(event);
  93. await new Promise(resolve => setImmediate(resolve));
  94. expect(handler1).toHaveBeenCalledTimes(1);
  95. expect(handler2).toHaveBeenCalledTimes(3);
  96. });
  97. });
  98. describe('filter()', () => {
  99. it('single handler is called once', async () => {
  100. const handler = vi.fn();
  101. const event = new TestEvent('foo');
  102. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler);
  103. await eventBus.publish(event);
  104. await new Promise(resolve => setImmediate(resolve));
  105. expect(handler).toHaveBeenCalledTimes(1);
  106. expect(handler).toHaveBeenCalledWith(event);
  107. });
  108. it('single handler is called on multiple events', async () => {
  109. const handler = vi.fn();
  110. const event1 = new TestEvent('foo');
  111. const event2 = new TestEvent('bar');
  112. const event3 = new TestEvent('baz');
  113. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler);
  114. await eventBus.publish(event1);
  115. await eventBus.publish(event2);
  116. await eventBus.publish(event3);
  117. await new Promise(resolve => setImmediate(resolve));
  118. expect(handler).toHaveBeenCalledTimes(3);
  119. expect(handler).toHaveBeenCalledWith(event1);
  120. expect(handler).toHaveBeenCalledWith(event2);
  121. expect(handler).toHaveBeenCalledWith(event3);
  122. });
  123. it('multiple handler are called', async () => {
  124. const handler1 = vi.fn();
  125. const handler2 = vi.fn();
  126. const handler3 = vi.fn();
  127. const event = new TestEvent('foo');
  128. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler1);
  129. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler2);
  130. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler3);
  131. await eventBus.publish(event);
  132. await new Promise(resolve => setImmediate(resolve));
  133. expect(handler1).toHaveBeenCalledWith(event);
  134. expect(handler2).toHaveBeenCalledWith(event);
  135. expect(handler3).toHaveBeenCalledWith(event);
  136. });
  137. it('handler is not called for other events', async () => {
  138. const handler = vi.fn();
  139. const event = new OtherTestEvent('foo');
  140. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler);
  141. await eventBus.publish(event);
  142. await new Promise(resolve => setImmediate(resolve));
  143. expect(handler).not.toHaveBeenCalled();
  144. });
  145. it('handler is called for instance of child classes', async () => {
  146. const handler = vi.fn();
  147. const event = new ChildTestEvent('bar', 'foo');
  148. eventBus.filter(vendureEvent => vendureEvent instanceof TestEvent).subscribe(handler);
  149. await eventBus.publish(event);
  150. await new Promise(resolve => setImmediate(resolve));
  151. expect(handler).toHaveBeenCalled();
  152. });
  153. it('filter() returns a subscription', async () => {
  154. const handler = vi.fn();
  155. const event = new TestEvent('foo');
  156. const subscription = eventBus
  157. .filter(vendureEvent => vendureEvent instanceof TestEvent)
  158. .subscribe(handler);
  159. await eventBus.publish(event);
  160. await new Promise(resolve => setImmediate(resolve));
  161. expect(handler).toHaveBeenCalledTimes(1);
  162. subscription.unsubscribe();
  163. await eventBus.publish(event);
  164. await eventBus.publish(event);
  165. await new Promise(resolve => setImmediate(resolve));
  166. expect(handler).toHaveBeenCalledTimes(1);
  167. });
  168. it('unsubscribe() only unsubscribes own handler', async () => {
  169. const handler1 = vi.fn();
  170. const handler2 = vi.fn();
  171. const event = new TestEvent('foo');
  172. const subscription1 = eventBus
  173. .filter(vendureEvent => vendureEvent instanceof TestEvent)
  174. .subscribe(handler1);
  175. const subscription2 = eventBus
  176. .filter(vendureEvent => vendureEvent instanceof TestEvent)
  177. .subscribe(handler2);
  178. await eventBus.publish(event);
  179. await new Promise(resolve => setImmediate(resolve));
  180. expect(handler1).toHaveBeenCalledTimes(1);
  181. expect(handler2).toHaveBeenCalledTimes(1);
  182. subscription1.unsubscribe();
  183. await eventBus.publish(event);
  184. await eventBus.publish(event);
  185. await new Promise(resolve => setImmediate(resolve));
  186. expect(handler1).toHaveBeenCalledTimes(1);
  187. expect(handler2).toHaveBeenCalledTimes(3);
  188. });
  189. });
  190. describe('blocking event handlers', () => {
  191. it('calls the handler function', async () => {
  192. const event = new TestEvent('foo');
  193. const spy = vi.fn((e: VendureEvent) => undefined);
  194. eventBus.registerBlockingEventHandler({
  195. handler: e => spy(e),
  196. id: 'test-handler',
  197. event: TestEvent,
  198. });
  199. await eventBus.publish(event);
  200. expect(spy).toHaveBeenCalledTimes(1);
  201. expect(spy).toHaveBeenCalledWith(event);
  202. });
  203. it('throws when attempting to register with a duplicate id', () => {
  204. eventBus.registerBlockingEventHandler({
  205. handler: e => undefined,
  206. id: 'test-handler',
  207. event: TestEvent,
  208. });
  209. expect(() => {
  210. eventBus.registerBlockingEventHandler({
  211. handler: e => undefined,
  212. id: 'test-handler',
  213. event: TestEvent,
  214. });
  215. }).toThrowError(
  216. 'A handler with the id "test-handler" is already registered for the event TestEvent',
  217. );
  218. });
  219. it('calls multiple handler functions', async () => {
  220. const event = new TestEvent('foo');
  221. const spy1 = vi.fn((e: VendureEvent) => undefined);
  222. const spy2 = vi.fn((e: VendureEvent) => undefined);
  223. eventBus.registerBlockingEventHandler({
  224. handler: e => spy1(e),
  225. id: 'test-handler1',
  226. event: TestEvent,
  227. });
  228. eventBus.registerBlockingEventHandler({
  229. handler: e => spy2(e),
  230. id: 'test-handler2',
  231. event: TestEvent,
  232. });
  233. await eventBus.publish(event);
  234. expect(spy1).toHaveBeenCalledTimes(1);
  235. expect(spy1).toHaveBeenCalledWith(event);
  236. expect(spy2).toHaveBeenCalledTimes(1);
  237. expect(spy2).toHaveBeenCalledWith(event);
  238. });
  239. it('handles multiple events', async () => {
  240. const event1 = new TestEvent('foo');
  241. const event2 = new OtherTestEvent('bar');
  242. const spy = vi.fn((e: VendureEvent) => undefined);
  243. eventBus.registerBlockingEventHandler({
  244. handler: e => spy(e),
  245. id: 'test-handler',
  246. event: [TestEvent, OtherTestEvent],
  247. });
  248. await eventBus.publish(event1);
  249. expect(spy).toHaveBeenCalledTimes(1);
  250. expect(spy).toHaveBeenCalledWith(event1);
  251. await eventBus.publish(event2);
  252. expect(spy).toHaveBeenCalledTimes(2);
  253. expect(spy).toHaveBeenCalledWith(event2);
  254. });
  255. it('publish method throws in a handler throws', async () => {
  256. const event = new TestEvent('foo');
  257. eventBus.registerBlockingEventHandler({
  258. handler: () => {
  259. throw new Error('test error');
  260. },
  261. id: 'test-handler',
  262. event: TestEvent,
  263. });
  264. await expect(eventBus.publish(event)).rejects.toThrow('test error');
  265. });
  266. it('order of execution with "before" property', async () => {
  267. const event = new TestEvent('foo');
  268. const spy = vi.fn((input: string) => undefined);
  269. eventBus.registerBlockingEventHandler({
  270. handler: e => spy('test-handler1'),
  271. id: 'test-handler1',
  272. event: TestEvent,
  273. });
  274. eventBus.registerBlockingEventHandler({
  275. handler: e => spy('test-handler2'),
  276. id: 'test-handler2',
  277. event: TestEvent,
  278. before: 'test-handler1',
  279. });
  280. await eventBus.publish(event);
  281. expect(spy).toHaveBeenCalledTimes(2);
  282. expect(spy).toHaveBeenNthCalledWith(1, 'test-handler2');
  283. expect(spy).toHaveBeenNthCalledWith(2, 'test-handler1');
  284. });
  285. it('order of execution with "after" property', async () => {
  286. const event = new TestEvent('foo');
  287. const spy = vi.fn((input: string) => undefined);
  288. eventBus.registerBlockingEventHandler({
  289. handler: e => spy('test-handler1'),
  290. id: 'test-handler1',
  291. event: TestEvent,
  292. after: 'test-handler2',
  293. });
  294. eventBus.registerBlockingEventHandler({
  295. handler: e => spy('test-handler2'),
  296. id: 'test-handler2',
  297. event: TestEvent,
  298. });
  299. await eventBus.publish(event);
  300. expect(spy).toHaveBeenCalledTimes(2);
  301. expect(spy).toHaveBeenNthCalledWith(1, 'test-handler2');
  302. expect(spy).toHaveBeenNthCalledWith(2, 'test-handler1');
  303. });
  304. it('throws if there is a cycle in before ordering', () => {
  305. const spy = vi.fn((input: string) => undefined);
  306. eventBus.registerBlockingEventHandler({
  307. handler: e => spy('test-handler1'),
  308. id: 'test-handler1',
  309. event: TestEvent,
  310. before: 'test-handler2',
  311. });
  312. expect(() =>
  313. eventBus.registerBlockingEventHandler({
  314. handler: e => spy('test-handler2'),
  315. id: 'test-handler2',
  316. event: TestEvent,
  317. before: 'test-handler1',
  318. }),
  319. ).toThrowError(
  320. 'Circular dependency detected between event handlers test-handler1 and test-handler2',
  321. );
  322. });
  323. it('throws if there is a cycle in after ordering', () => {
  324. const spy = vi.fn((input: string) => undefined);
  325. eventBus.registerBlockingEventHandler({
  326. handler: e => spy('test-handler1'),
  327. id: 'test-handler1',
  328. event: TestEvent,
  329. after: 'test-handler2',
  330. });
  331. expect(() =>
  332. eventBus.registerBlockingEventHandler({
  333. handler: e => spy('test-handler2'),
  334. id: 'test-handler2',
  335. event: TestEvent,
  336. after: 'test-handler1',
  337. }),
  338. ).toThrowError(
  339. 'Circular dependency detected between event handlers test-handler1 and test-handler2',
  340. );
  341. });
  342. it('blocks execution of the publish method', async () => {
  343. const event = new TestEvent('foo');
  344. const subject = new Subject<void>();
  345. eventBus.registerBlockingEventHandler({
  346. handler: e => firstValueFrom(subject.asObservable()),
  347. id: 'test-handler',
  348. event: TestEvent,
  349. });
  350. const publishPromise = eventBus.publish(event);
  351. expect(publishPromise).toBeInstanceOf(Promise);
  352. let resolved = false;
  353. void publishPromise.then(() => (resolved = true));
  354. expect(resolved).toBe(false);
  355. await new Promise(resolve => setTimeout(resolve, 50));
  356. expect(resolved).toBe(false);
  357. // Handler only resolves after the subject emits
  358. subject.next();
  359. // Allow the event loop to tick
  360. await new Promise(resolve => setTimeout(resolve, 0));
  361. // Now the promise should be resolved
  362. expect(resolved).toBe(true);
  363. });
  364. });
  365. });
  366. class TestEvent extends VendureEvent {
  367. constructor(public payload: string) {
  368. super();
  369. }
  370. }
  371. class ChildTestEvent extends TestEvent {
  372. constructor(
  373. public childPayload: string,
  374. payload: string,
  375. ) {
  376. super(payload);
  377. }
  378. }
  379. class OtherTestEvent extends VendureEvent {
  380. constructor(public payload: string) {
  381. super();
  382. }
  383. }