Skip to content

Commit

Permalink
fix(core): proper handling for events with no handlers registered
Browse files Browse the repository at this point in the history
  • Loading branch information
Matii96 committed Apr 5, 2023
1 parent 5273e9b commit 41eff02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Expand Up @@ -50,25 +50,23 @@ describe('EventsExecutor', () => {
);
});

it('should handle event', (done) => {
executor.execute(subject, event).subscribe({
complete() {
expect(handler.handle).toHaveBeenCalledTimes(1);
done();
},
});
it('should handle event with its handler', async () => {
await lastValueFrom(executor.execute(subject, event));
expect(handler.handle).toHaveBeenCalledTimes(1);
});

it('should emit event handling steps', (done) => {
executor.execute(subject, event).subscribe({
complete() {
expect(eventStates).toEqual([
new HandlingStartedEventProcessingState(handler),
new HandlingCompletedEventProcessingState(handler),
]);
done();
},
});
it('should handle event with no handlers registered', async () => {
class NoHandlersEvent {}
const event = new NoHandlersEvent();
expect(await lastValueFrom(executor.execute(subject, event))).toBe(event);
});

it('should emit event handling steps', async () => {
await lastValueFrom(executor.execute(subject, event));
expect(eventStates).toEqual([
new HandlingStartedEventProcessingState(handler),
new HandlingCompletedEventProcessingState(handler),
]);
});
});

Expand Down
@@ -1,4 +1,18 @@
import { catchError, defer, from, last, map, mergeMap, of, retry, Subject, tap, throwError, timeout } from 'rxjs';
import {
catchError,
defaultIfEmpty,
defer,
from,
last,
map,
mergeMap,
of,
retry,
Subject,
tap,
throwError,
timeout,
} from 'rxjs';
import { Event } from '../../../messages/event/event';
import { EventsProvidersSchema } from '../../../providers-manager/messages/events/interfaces/events-providers-schema.interface';
import { IEventHandler, MessageTypes } from '../../../messages';
Expand Down Expand Up @@ -31,6 +45,7 @@ export class MediatorEventsExecutor implements EventsExecutor {
isPromise(handler) ? from(handler as Promise<IEventHandler<Event>>) : of(handler as IEventHandler<Event>)
),
mergeMap((handler) => this.handleEvent({ config, messageProcessing, event, handler })),
defaultIfEmpty(undefined), // There can be no handlers registered for this event
last(),
map(() => event)
);
Expand Down

0 comments on commit 41eff02

Please sign in to comment.