|
| 1 | +import { connect, AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager' |
| 2 | +import { ConnectionOptions } from 'tls' |
| 3 | + |
| 4 | +import { AmqpBroker } from '../../src/adapter/amqp/amqp-adapter' |
| 5 | +import { AmqpSender } from '../../src/adapter/amqp/amqp-sender' |
| 6 | +import { AmqpReceiver } from '../../src/adapter/amqp/amqp-receiver' |
| 7 | + |
| 8 | +jest.mock('amqp-connection-manager', () => ({ |
| 9 | + _esModule: true, |
| 10 | + connect: jest.fn(() => ({ |
| 11 | + createChannel: jest.fn(), |
| 12 | + on: jest.fn() |
| 13 | + })) |
| 14 | +})) |
| 15 | + |
| 16 | +const mockedConnect = connect as jest.MockedFunction<typeof connect> |
| 17 | +const url = 'amqp://myrabbitserver' |
| 18 | +const options: ConnectionOptions = { timeout: 10 } |
| 19 | + |
| 20 | +describe(`AMQP Broker`, () => { |
| 21 | + let broker: AmqpBroker |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + jest.clearAllMocks() |
| 25 | + broker = new AmqpBroker(url, options) |
| 26 | + }) |
| 27 | + |
| 28 | + it(`connects using connection manager`, () => { |
| 29 | + broker.connect() |
| 30 | + |
| 31 | + const connectionManager = mockedConnect.mock.results[0].value as AmqpConnectionManager |
| 32 | + |
| 33 | + expect(mockedConnect).toHaveBeenCalledTimes(1) |
| 34 | + expect(mockedConnect.mock.calls[0][0]).toStrictEqual([url]) |
| 35 | + expect(mockedConnect.mock.calls[0][1]).toStrictEqual({ connectionOptions: options }) |
| 36 | + expect(broker.connectionManager).toBe(connectionManager) |
| 37 | + |
| 38 | + const mockedChannel = connectionManager.createChannel as jest.MockedFunction< |
| 39 | + typeof connectionManager.createChannel |
| 40 | + > |
| 41 | + |
| 42 | + expect(mockedChannel).toHaveBeenCalledTimes(1) |
| 43 | + expect(mockedChannel.mock.calls[0][0]).toStrictEqual({ json: true }) |
| 44 | + }) |
| 45 | + |
| 46 | + it(`connect can only be called once`, () => { |
| 47 | + broker.connect() |
| 48 | + expect(() => broker.connect()).toThrow('AMQP connection already created') |
| 49 | + }) |
| 50 | + |
| 51 | + it(`must create an instance of AmqpSender`, () => { |
| 52 | + broker.connect() |
| 53 | + |
| 54 | + expect(broker.getSender()).toBeInstanceOf(AmqpSender) |
| 55 | + }) |
| 56 | + |
| 57 | + it(`must create an instance of AmqpReceiver`, () => { |
| 58 | + broker.connect() |
| 59 | + |
| 60 | + expect(broker.getReceiver()).toBeInstanceOf(AmqpReceiver) |
| 61 | + }) |
| 62 | + |
| 63 | + it(`AmqpSender can only be accessed after connect`, () => { |
| 64 | + expect(() => broker.getSender()).toThrow( |
| 65 | + 'AMQP sender must be accessed after connection creation' |
| 66 | + ) |
| 67 | + }) |
| 68 | + |
| 69 | + it(`AmqpReceiver can only be accessed after connect`, () => { |
| 70 | + expect(() => broker.getReceiver()).toThrow( |
| 71 | + 'AMQP receiver must be accessed after connection creation' |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + it(`should call onConnect callback when connectionManager emits it`, () => { |
| 76 | + const connection = broker.connect() |
| 77 | + const connectionManager = mockedConnect.mock.results[0].value as AmqpConnectionManager |
| 78 | + const mockedOn = connectionManager.on as jest.MockedFunction<typeof connectionManager.on> |
| 79 | + |
| 80 | + const onConnect = jest.fn() |
| 81 | + connection.onConnect(onConnect) |
| 82 | + |
| 83 | + const listener = mockedOn.mock.calls[0][1] as (arg: object) => void |
| 84 | + listener({ url }) |
| 85 | + |
| 86 | + expect(mockedOn.mock.calls[0][0]).toBe('connect') |
| 87 | + expect(onConnect.mock.calls[0][0]).toStrictEqual({ host: 'myrabbitserver' }) |
| 88 | + }) |
| 89 | + |
| 90 | + it(`should call onDisconnect callback when connectionManager emits it`, () => { |
| 91 | + const connection = broker.connect() |
| 92 | + const connectionManager = mockedConnect.mock.results[0].value as AmqpConnectionManager |
| 93 | + const mockedOn = connectionManager.on as jest.MockedFunction<typeof connectionManager.on> |
| 94 | + const error = Error('something went wrong') |
| 95 | + |
| 96 | + const onDisconnect = jest.fn() |
| 97 | + connection.onDisconnect(onDisconnect) |
| 98 | + |
| 99 | + const listener = mockedOn.mock.calls[0][1] |
| 100 | + listener({ err: error }) |
| 101 | + |
| 102 | + expect(mockedOn.mock.calls[0][0]).toBe('disconnect') |
| 103 | + expect(onDisconnect.mock.calls[0][0]).toBe(error) |
| 104 | + }) |
| 105 | +}) |
0 commit comments