|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { AsyncHandlersAppModule } from '../src/sync-event-handlers/async-handlers-app.module'; |
| 3 | +import { EventBus } from '../../src/event-bus'; |
| 4 | +import { HeroKilledDragon2SlowHandler } from '../src/sync-event-handlers/events/handlers/hero-killed-dragon2-slow.handler'; |
| 5 | +import { HeroKilledDragonSlowHandler } from '../src/sync-event-handlers/events/handlers/hero-killed-dragon-slow.handler'; |
| 6 | +import { HeroFoundItemSlowHandler } from '../src/sync-event-handlers/events/handlers/hero-found-item-slow.handler'; |
| 7 | +import { SyncHandlersAppModule } from '../src/sync-event-handlers/sync-handlers-app.module'; |
| 8 | +import { HeroFoundItemEvent } from '../src/heroes/events/impl/hero-found-item.event'; |
| 9 | +import { HeroKilledDragonEvent } from '../src/heroes/events/impl/hero-killed-dragon.event'; |
| 10 | + |
| 11 | +describe('Sync event handlers', () => { |
| 12 | + let moduleRef: TestingModule; |
| 13 | + let eventBus: EventBus; |
| 14 | + let heroFoundItemHandler: HeroFoundItemSlowHandler; |
| 15 | + let heroFoundItemEnded: jest.SpyInstance; |
| 16 | + let heroKilledDragonHandler: HeroKilledDragonSlowHandler; |
| 17 | + let heroKilledDragonEnded: jest.SpyInstance; |
| 18 | + let heroKilledDragon2Handler: HeroKilledDragon2SlowHandler; |
| 19 | + let heroKilledDragon2Ended: jest.SpyInstance; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + heroFoundItemEnded.mockClear(); |
| 23 | + heroKilledDragonEnded.mockClear(); |
| 24 | + heroKilledDragon2Ended.mockClear(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('when event handlers are fully asynchronous', () => { |
| 28 | + beforeAll(async () => { |
| 29 | + moduleRef = await Test.createTestingModule({ |
| 30 | + imports: [AsyncHandlersAppModule], |
| 31 | + }).compile(); |
| 32 | + |
| 33 | + await moduleRef.init(); |
| 34 | + eventBus = moduleRef.get(EventBus); |
| 35 | + heroFoundItemHandler = moduleRef.get(HeroFoundItemSlowHandler); |
| 36 | + heroKilledDragonHandler = moduleRef.get(HeroKilledDragonSlowHandler); |
| 37 | + heroKilledDragon2Handler = moduleRef.get(HeroKilledDragon2SlowHandler); |
| 38 | + heroFoundItemEnded = jest.spyOn(heroFoundItemHandler, 'end'); |
| 39 | + heroKilledDragonEnded = jest.spyOn(heroKilledDragonHandler, 'end'); |
| 40 | + heroKilledDragon2Ended = jest.spyOn(heroKilledDragon2Handler, 'end'); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('when "HeroFoundItemEvent" event is published', () => { |
| 44 | + it('should wait for the event handlers to complete', async () => { |
| 45 | + const event = new HeroFoundItemEvent('hero1', 'item1'); |
| 46 | + |
| 47 | + await eventBus.publish(event); |
| 48 | + |
| 49 | + expect(heroFoundItemEnded).not.toHaveBeenCalled(); |
| 50 | + expect(heroKilledDragonEnded).not.toHaveBeenCalled(); |
| 51 | + expect(heroKilledDragon2Ended).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('when "HeroKilledDragonEvent" event is published', () => { |
| 56 | + it('should wait for the event handlers to complete', async () => { |
| 57 | + const event = new HeroKilledDragonEvent('hero1', 'dragon1'); |
| 58 | + |
| 59 | + await eventBus.publish(event); |
| 60 | + |
| 61 | + expect(heroFoundItemEnded).not.toHaveBeenCalled(); |
| 62 | + expect(heroKilledDragonEnded).not.toHaveBeenCalled(); |
| 63 | + expect(heroKilledDragon2Ended).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('when event handlers are synchronous', () => { |
| 69 | + beforeAll(async () => { |
| 70 | + moduleRef = await Test.createTestingModule({ |
| 71 | + imports: [SyncHandlersAppModule], |
| 72 | + }).compile(); |
| 73 | + |
| 74 | + await moduleRef.init(); |
| 75 | + eventBus = moduleRef.get(EventBus); |
| 76 | + heroFoundItemHandler = moduleRef.get(HeroFoundItemSlowHandler); |
| 77 | + heroKilledDragonHandler = moduleRef.get(HeroKilledDragonSlowHandler); |
| 78 | + heroKilledDragon2Handler = moduleRef.get(HeroKilledDragon2SlowHandler); |
| 79 | + heroFoundItemEnded = jest.spyOn(heroFoundItemHandler, 'end'); |
| 80 | + heroKilledDragonEnded = jest.spyOn(heroKilledDragonHandler, 'end'); |
| 81 | + heroKilledDragon2Ended = jest.spyOn(heroKilledDragon2Handler, 'end'); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('when "HeroFoundItemEvent" event is published', () => { |
| 85 | + it('should wait for the event handlers to complete', async () => { |
| 86 | + const event = new HeroFoundItemEvent('hero1', 'item1'); |
| 87 | + |
| 88 | + await eventBus.publish(event); |
| 89 | + |
| 90 | + expect(heroFoundItemEnded).toHaveBeenCalledTimes(1); |
| 91 | + expect(heroKilledDragonEnded).not.toHaveBeenCalled(); |
| 92 | + expect(heroKilledDragon2Ended).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('when "HeroKilledDragonEvent" event is published', () => { |
| 97 | + it('should wait for the event handlers to complete', async () => { |
| 98 | + const event = new HeroKilledDragonEvent('hero1', 'dragon1'); |
| 99 | + |
| 100 | + await eventBus.publish(event); |
| 101 | + |
| 102 | + expect(heroFoundItemEnded).not.toHaveBeenCalled(); |
| 103 | + expect(heroKilledDragonEnded).toHaveBeenCalledTimes(1); |
| 104 | + expect(heroKilledDragon2Ended).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('when an unknown event is published', () => { |
| 109 | + it('should return directly', async () => { |
| 110 | + await eventBus.publish({ id: 'unknown event' }); |
| 111 | + |
| 112 | + expect(heroFoundItemEnded).not.toHaveBeenCalled(); |
| 113 | + expect(heroKilledDragonEnded).not.toHaveBeenCalled(); |
| 114 | + expect(heroKilledDragon2Ended).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + afterAll(async () => { |
| 120 | + await moduleRef.close(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments