Repository navigation
Expand file tree
/
Copy pathindex.unit.test.js
More file actions
64 lines (55 loc) · 2.13 KB
/
Copy pathindex.unit.test.js
File metadata and controls
64 lines (55 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { MessageQueue } = require('.')
const SqsClient = require('./sqs-client')
jest.mock('./sqs-client')
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
SqsClient.mockClear();
});
describe('Publish', () => {
test('Publish is called once', async () => {
const messageQueue = new MessageQueue('sqs')
await messageQueue.publish('hello world', {})
expect(messageQueue.client.publish).toHaveBeenCalledTimes(1)
})
test('Payload is published', async () => {
const topicPrefix = 'core'
const messageQueue = new MessageQueue('sqs', { topicPrefix })
const payload = { x: 5, y: 'strawberries' }
await messageQueue.publish('eat', payload)
expect(messageQueue.client.publish.mock.calls[0][1]).toEqual(payload)
})
test('Topic prefix is configurable', async () => {
const topicPrefix = 'core'
const messageQueue = new MessageQueue('sqs', { topicPrefix })
const eventName = 'eat'
const payload = { x: 5, y: 'strawberries' }
await messageQueue.publish(eventName, payload)
expect(messageQueue.client.publish.mock.calls[0][0]).toBe(`${topicPrefix}-${eventName}`)
})
})
describe('Subscribe', () => {
test('Handler is called once', () => {
const messageQueue = new MessageQueue('sqs')
const messageHandler = jest.fn((message) => Promise.resolve(true))
messageQueue.client.subscribe = (queueName, handler) => {
handler({ x: 'Hello', y: 47 })
}
messageQueue.subscribe('hello', messageHandler)
expect(messageHandler).toHaveBeenCalledTimes(1)
})
})
describe('Construction', () => {
test('rabbitmq client requires connection url', () => {
delete process.env.RABBITMQ_URL
delete process.env.AMQP_URL
expect(() => new MessageQueue('rabbitmq')).toThrow(/RABBITMQ_URL/)
})
test('rabbitmq client constructs when url present', () => {
process.env.RABBITMQ_URL = 'amqp://x:y@localhost:5672'
const mq = new MessageQueue('rabbitmq')
expect(mq.client.constructor.name).toBe('RabbitMQMessageQueueClient')
})
test('unknown client type throws', () => {
expect(() => new MessageQueue('redis')).toThrow(/not supported/)
})
})