Browse Source

test(core): Fix EventBus unit tests

Michael Bromley 4 years ago
parent
commit
86f02e728c
1 changed files with 25 additions and 7 deletions
  1. 25 7
      packages/core/src/event-bus/event-bus.spec.ts

+ 25 - 7
packages/core/src/event-bus/event-bus.spec.ts

@@ -1,11 +1,21 @@
+import { QueryRunner } from 'typeorm';
+
+import { TransactionSubscriber } from '../connection/transaction-subscriber';
+
 import { EventBus } from './event-bus';
 import { VendureEvent } from './vendure-event';
 
+class MockTransactionSubscriber {
+    awaitRelease(queryRunner: QueryRunner): Promise<QueryRunner> {
+        return Promise.resolve(queryRunner);
+    }
+}
+
 describe('EventBus', () => {
     let eventBus: EventBus;
 
     beforeEach(() => {
-        eventBus = new EventBus();
+        eventBus = new EventBus(new MockTransactionSubscriber() as any);
     });
 
     it('can publish without subscribers', () => {
@@ -15,18 +25,19 @@ describe('EventBus', () => {
     });
 
     describe('ofType()', () => {
-        it('single handler is called once', () => {
+        it('single handler is called once', async () => {
             const handler = jest.fn();
             const event = new TestEvent('foo');
             eventBus.ofType(TestEvent).subscribe(handler);
 
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler).toHaveBeenCalledTimes(1);
             expect(handler).toHaveBeenCalledWith(event);
         });
 
-        it('single handler is called on multiple events', () => {
+        it('single handler is called on multiple events', async () => {
             const handler = jest.fn();
             const event1 = new TestEvent('foo');
             const event2 = new TestEvent('bar');
@@ -36,6 +47,7 @@ describe('EventBus', () => {
             eventBus.publish(event1);
             eventBus.publish(event2);
             eventBus.publish(event3);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler).toHaveBeenCalledTimes(3);
             expect(handler).toHaveBeenCalledWith(event1);
@@ -43,7 +55,7 @@ describe('EventBus', () => {
             expect(handler).toHaveBeenCalledWith(event3);
         });
 
-        it('multiple handlers are called', () => {
+        it('multiple handlers are called', async () => {
             const handler1 = jest.fn();
             const handler2 = jest.fn();
             const handler3 = jest.fn();
@@ -53,28 +65,31 @@ describe('EventBus', () => {
             eventBus.ofType(TestEvent).subscribe(handler3);
 
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler1).toHaveBeenCalledWith(event);
             expect(handler2).toHaveBeenCalledWith(event);
             expect(handler3).toHaveBeenCalledWith(event);
         });
 
-        it('handler is not called for other events', () => {
+        it('handler is not called for other events', async () => {
             const handler = jest.fn();
             const event = new OtherTestEvent('foo');
             eventBus.ofType(TestEvent).subscribe(handler);
 
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler).not.toHaveBeenCalled();
         });
 
-        it('ofType() returns a subscription', () => {
+        it('ofType() returns a subscription', async () => {
             const handler = jest.fn();
             const event = new TestEvent('foo');
             const subscription = eventBus.ofType(TestEvent).subscribe(handler);
 
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler).toHaveBeenCalledTimes(1);
 
@@ -82,11 +97,12 @@ describe('EventBus', () => {
 
             eventBus.publish(event);
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler).toHaveBeenCalledTimes(1);
         });
 
-        it('unsubscribe() only unsubscribes own handler', () => {
+        it('unsubscribe() only unsubscribes own handler', async () => {
             const handler1 = jest.fn();
             const handler2 = jest.fn();
             const event = new TestEvent('foo');
@@ -94,6 +110,7 @@ describe('EventBus', () => {
             const subscription2 = eventBus.ofType(TestEvent).subscribe(handler2);
 
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler1).toHaveBeenCalledTimes(1);
             expect(handler2).toHaveBeenCalledTimes(1);
@@ -102,6 +119,7 @@ describe('EventBus', () => {
 
             eventBus.publish(event);
             eventBus.publish(event);
+            await new Promise(resolve => setImmediate(resolve));
 
             expect(handler1).toHaveBeenCalledTimes(1);
             expect(handler2).toHaveBeenCalledTimes(3);