|
| 1 | +import { Query } from '../../../../../src/Contexts/Shared/domain/Query'; |
| 2 | +import { QueryHandlersInformation } from '../../../../../src/Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation'; |
| 3 | +import { QueryNotRegisteredError } from '../../../../../src/Contexts/Shared/domain/QueryNotRegisteredError'; |
| 4 | +import { QueryHandler } from '../../../../../src/Contexts/Shared/domain/QueryHandler'; |
| 5 | +import { Response } from '../../../../../src/Contexts/Shared/domain/Response'; |
| 6 | + |
| 7 | +class UnhandledQuery extends Query { |
| 8 | + static QUERY_NAME = 'unhandled.query'; |
| 9 | +} |
| 10 | + |
| 11 | +class HandledQuery extends Query { |
| 12 | + static QUERY_NAME = 'handled.query'; |
| 13 | +} |
| 14 | + |
| 15 | +class MyQueryHandler implements QueryHandler<Query, Response> { |
| 16 | + subscribedTo(): HandledQuery { |
| 17 | + return HandledQuery; |
| 18 | + } |
| 19 | + |
| 20 | + async handle(query: HandledQuery): Promise<Response> {return {};} |
| 21 | +} |
| 22 | + |
| 23 | +describe('InMemoryQueryBus', () => { |
| 24 | + it('throws an error if dispatches a query without handler', async () => { |
| 25 | + const unhandledQuery = new UnhandledQuery(); |
| 26 | + const queryHandlersInformation = new QueryHandlersInformation([]); |
| 27 | + const queryBus = new InMemoryQueryBus(queryHandlersInformation); |
| 28 | + |
| 29 | + let exception = null; |
| 30 | + |
| 31 | + try { |
| 32 | + await queryBus.ask(unhandledQuery); |
| 33 | + } catch (error) { |
| 34 | + exception = error; |
| 35 | + } |
| 36 | + |
| 37 | + expect(exception).toBeInstanceOf(QueryNotRegisteredError); |
| 38 | + expect(exception.message).toBe(`The query <UnhandledCommand> hasn't a query handler associated`); |
| 39 | + }); |
| 40 | + |
| 41 | + it('accepts a query with handler', async () => { |
| 42 | + const handledQuery = new HandledQuery(); |
| 43 | + const myQueryHandler = new MyQueryHandler(); |
| 44 | + const queryHandlersInformation = new QueryHandlersInformation([myQueryHandler]); |
| 45 | + const queryBus = new InMemoryQueryBus(queryHandlersInformation); |
| 46 | + |
| 47 | + await queryBus.ask(handledQuery); |
| 48 | + }); |
| 49 | +}); |
0 commit comments