|
| 1 | +import { AmqpSender } from '../../src/adapter/amqp/amqp-sender' |
| 2 | + |
| 3 | +import { ConfirmChannel } from 'amqplib' |
| 4 | +import { ChannelWrapper } from 'amqp-connection-manager' |
| 5 | +import { |
| 6 | + QueueSpecitication, |
| 7 | + TopicSpecification, |
| 8 | + BindingSpecification |
| 9 | +} from '../../src/domain/model/broker.model' |
| 10 | + |
| 11 | +const queueName = 'myQueue' |
| 12 | +const topicName = 'mytopic' |
| 13 | +const routingKey = 'myRoutingKey' |
| 14 | + |
1 | 15 | describe(`AMQP sender`, () => { |
2 | | - it(`declares a queue`, () => {}) |
| 16 | + let addSetupMock: jest.MockedFunction< |
| 17 | + (func: (channel: ConfirmChannel) => Promise<void>) => Promise<void> |
| 18 | + > |
| 19 | + let publishMock: jest.MockedFunction< |
| 20 | + (exchange: string, routingKey: string, content: Buffer) => Promise<void> |
| 21 | + > |
| 22 | + let channelWrapper: ChannelWrapper |
| 23 | + let sender: AmqpSender |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks() |
| 27 | + addSetupMock = jest.fn(func => Promise.resolve()) |
| 28 | + publishMock = jest.fn((exchange, routingKey, content) => Promise.resolve()) |
| 29 | + channelWrapper = ({ addSetup: addSetupMock, publish: publishMock } as any) as ChannelWrapper |
| 30 | + sender = new AmqpSender(channelWrapper) |
| 31 | + }) |
| 32 | + |
| 33 | + it(`declares a queue`, async () => { |
| 34 | + const queue: QueueSpecitication = { |
| 35 | + name: queueName, |
| 36 | + exclusive: true |
| 37 | + } |
| 38 | + |
| 39 | + const assertQueueMock = jest.fn((name, options) => undefined) |
| 40 | + const channel = ({ assertQueue: assertQueueMock } as any) as ConfirmChannel |
| 41 | + |
| 42 | + await sender.declareQueue(queue) |
| 43 | + await addSetupMock.mock.calls[0][0](channel) |
| 44 | + |
| 45 | + expect(assertQueueMock.mock.calls[0][0]).toBe(queueName) |
| 46 | + expect(assertQueueMock.mock.calls[0][1]).toStrictEqual({ |
| 47 | + exclusive: true |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it(`declares a topic`, async () => { |
| 52 | + const topic: TopicSpecification = { |
| 53 | + name: topicName |
| 54 | + } |
| 55 | + |
| 56 | + const assertExchangeMock = jest.fn((name, type) => undefined) |
| 57 | + const channel = ({ assertExchange: assertExchangeMock } as any) as ConfirmChannel |
| 58 | + |
| 59 | + await sender.declareTopic(topic) |
| 60 | + await addSetupMock.mock.calls[0][0](channel) |
| 61 | + |
| 62 | + expect(assertExchangeMock.mock.calls[0][0]).toBe(topicName) |
| 63 | + expect(assertExchangeMock.mock.calls[0][1]).toEqual('topic') |
| 64 | + }) |
| 65 | + |
| 66 | + it(`do a binding`, async () => { |
| 67 | + const binding: BindingSpecification = { |
| 68 | + queue: queueName, |
| 69 | + topic: topicName, |
| 70 | + routingKey: routingKey |
| 71 | + } |
| 72 | + |
| 73 | + const bindQueueMock = jest.fn((queue, topic, routingKey) => undefined) |
| 74 | + const channel = ({ bindQueue: bindQueueMock } as any) as ConfirmChannel |
| 75 | + |
| 76 | + await sender.bind(binding) |
| 77 | + await addSetupMock.mock.calls[0][0](channel) |
| 78 | + |
| 79 | + expect(bindQueueMock.mock.calls[0][0]).toBe(queueName) |
| 80 | + expect(bindQueueMock.mock.calls[0][1]).toEqual(topicName) |
| 81 | + expect(bindQueueMock.mock.calls[0][2]).toEqual(routingKey) |
| 82 | + }) |
3 | 83 |
|
4 | | - it(`declares a topic`, () => {}) |
| 84 | + it(`publish messages`, async () => { |
| 85 | + await sender.publish(topicName, routingKey, { foo: 'bar' }) |
5 | 86 |
|
6 | | - it(`do a binding`, () => {}) |
| 87 | + expect(publishMock.mock.calls[0][0]).toBe(topicName) |
| 88 | + expect(publishMock.mock.calls[0][1]).toBe(routingKey) |
| 89 | + expect(publishMock.mock.calls[0][2]).toStrictEqual({ foo: 'bar' }) |
| 90 | + }) |
7 | 91 | }) |
0 commit comments