File tree Expand file tree Collapse file tree
infrastructure/CommandBus
tests/Contexts/Shared/infrastructure/CommandBus Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ export abstract class Command {
2+ readonly commandName : string ;
3+
4+ constructor ( commandName : string ) {
5+ this . commandName = commandName ;
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ import { Command } from './Command' ;
2+
3+ export interface CommandBus {
4+ dispatch ( command : Command ) : void ;
5+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments