import { InMemoryEventPublisher } from './InMemoryEventPublisher'; import { DashboardAccessedEvent } from '../../core/dashboard/application/ports/DashboardEventPublisher'; import { LeagueCreatedEvent } from '../../core/leagues/application/ports/LeagueEventPublisher'; describe('InMemoryEventPublisher', () => { let publisher: InMemoryEventPublisher; beforeEach(() => { publisher = new InMemoryEventPublisher(); }); describe('Dashboard Events', () => { it('should publish and track dashboard accessed events', async () => { // Given const event: DashboardAccessedEvent = { userId: 'user-1', timestamp: new Date() }; // When await publisher.publishDashboardAccessed(event); // Then expect(publisher.getDashboardAccessedEventCount()).toBe(1); }); it('should throw error when configured to fail', async () => { // Given publisher.setShouldFail(true); const event: DashboardAccessedEvent = { userId: 'user-1', timestamp: new Date() }; // When & Then await expect(publisher.publishDashboardAccessed(event)).rejects.toThrow('Event publisher failed'); }); }); describe('League Events', () => { it('should publish and track league created events', async () => { // Given const event: LeagueCreatedEvent = { leagueId: 'league-1', name: 'Test League', timestamp: new Date() }; // When await publisher.emitLeagueCreated(event); // Then expect(publisher.getLeagueCreatedEventCount()).toBe(1); expect(publisher.getLeagueCreatedEvents()).toContainEqual(event); }); }); describe('Generic Domain Events', () => { it('should publish and track generic domain events', async () => { // Given const event = { type: 'TestEvent', timestamp: new Date() }; // When await publisher.publish(event); // Then expect(publisher.getEvents()).toContainEqual(event); }); }); describe('Maintenance', () => { it('should clear all events', async () => { // Given await publisher.publishDashboardAccessed({ userId: 'u1', timestamp: new Date() }); await publisher.emitLeagueCreated({ leagueId: 'l1', name: 'L1', timestamp: new Date() }); await publisher.publish({ type: 'Generic', timestamp: new Date() }); // When publisher.clear(); // Then expect(publisher.getDashboardAccessedEventCount()).toBe(0); expect(publisher.getLeagueCreatedEventCount()).toBe(0); expect(publisher.getEvents().length).toBe(0); }); }); });