Skip to content

Commit a2874ee

Browse files
committed
Add in memory version of command bus not handling commands
1 parent feb2c24 commit a2874ee

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export abstract class Command {
2+
readonly commandName: string;
3+
4+
constructor(commandName: string) {
5+
this.commandName = commandName;
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Command } from './Command';
2+
3+
export interface CommandBus {
4+
dispatch(command: Command): void;
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { CommandBus } from './../../domain/CommandBus';
2+
import { Command } from '../../domain/Command';
3+
import { NoHandlerForMessageError } from './NoHandlerForMessageError';
4+
5+
export class InMemoryCommandBus implements CommandBus {
6+
dispatch(command: Command): void {
7+
throw new NoHandlerForMessageError(command);
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Command } from '../../domain/Command';
2+
3+
export class NoHandlerForMessageError extends Error {
4+
constructor(command: Command) {
5+
super(`There is not handler for command of type ${command.commandName}`);
6+
}
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { InMemoryCommandBus } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus';
2+
import { Command } from '../../../../../src/Contexts/Shared/domain/Command';
3+
import { NoHandlerForMessageError } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/NoHandlerForMessageError';
4+
5+
class UnhandledCommand extends Command {
6+
constructor() {
7+
super('UnhandledCommandName');
8+
}
9+
}
10+
11+
describe('InMemoryCommandBus', () => {
12+
it('throws an error if dispatches a command without handler', (done) => {
13+
const unhandledCommand = new UnhandledCommand();
14+
const commandBus = new InMemoryCommandBus();
15+
16+
try {
17+
commandBus.dispatch(unhandledCommand);
18+
} catch (error) {
19+
expect(error).toBeInstanceOf(NoHandlerForMessageError);
20+
expect(error.message).toBe('There is not handler for command of type UnhandledCommandName');
21+
done();
22+
}
23+
});
24+
});

0 commit comments

Comments
 (0)