Skip to content

Commit 199a36b

Browse files
committed
Add coverage for listConversationParticipants
1 parent caaf6a1 commit 199a36b

File tree

4 files changed

+88
-158
lines changed

4 files changed

+88
-158
lines changed

src/utils/listConversationParticipants.util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import retry from 'async-retry'
22
import { ParticipantInstance } from 'twilio/lib/rest/conversations/v1/conversation/participant'
33
import client from '../twilioClient'
4+
import { retryConfig } from '../config/retry.config'
45

5-
export const listConversationParticipants = async (conversation: string) : Promise<ParticipantInstance[]> => {
6+
export const listConversationParticipants = async (conversation: string, retryOptions = retryConfig) : Promise<ParticipantInstance[]> => {
67
return retry(async (quit) => {
78
try {
89
const participants = await client.conversations
@@ -20,6 +21,6 @@ export const listConversationParticipants = async (conversation: string) : Promi
2021
console.log('Re-trying on 429 error');
2122
throw new Error(err);
2223
}
23-
})
24+
}, retryOptions)
2425

2526
}

tests/utils/createConversation.test.ts.skip

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { listConversationParticipants } from '../../src/utils'
2+
import client from '../../src/twilioClient'
3+
4+
jest.mock('../../src/twilioClient')
5+
let mockedClient = jest.mocked(client, true)
6+
7+
describe('listConversationParticipants', () => {
8+
it('it lists participants with provided conversation sid', async () => {
9+
const listSpy = jest.fn(() => { return ['fake_participant_1', 'fake_participant_2'] })
10+
11+
const conversationSpy = jest.fn((options) => {
12+
return {
13+
participants: {
14+
list: listSpy
15+
}
16+
}
17+
})
18+
19+
mockedClient['conversations'] = {
20+
conversations: conversationSpy
21+
} as any
22+
23+
const result = await listConversationParticipants("CH123")
24+
25+
expect(conversationSpy).toBeCalledWith("CH123")
26+
expect(listSpy).toBeCalled()
27+
expect(result).toEqual(['fake_participant_1', 'fake_participant_2'])
28+
})
29+
30+
it('should throw error if Twilio client throws', async () => {
31+
const listSpy = jest.fn(() => { throw new Error('Participant List Error') })
32+
33+
const conversationSpy = jest.fn((options) => {
34+
return {
35+
participants: {
36+
list: listSpy
37+
}
38+
}
39+
})
40+
41+
mockedClient['conversations'] = {
42+
conversations: conversationSpy
43+
} as any
44+
45+
await expect(listConversationParticipants("CH123"))
46+
.rejects
47+
.toThrowError('Participant List Erro')
48+
})
49+
50+
it('should retry if Twilio client throws a 429 error', async () => {
51+
interface TwilioError extends Error {
52+
status: number
53+
}
54+
55+
class TwilioError extends Error {
56+
constructor(message) {
57+
super(message);
58+
this.name = "ConcurrencyLimit";
59+
this.status = 429
60+
}
61+
}
62+
63+
const listSpy = jest.fn(() => { throw new TwilioError('Error to Retry') })
64+
65+
const conversationSpy = jest.fn((options) => {
66+
return {
67+
participants: {
68+
list: listSpy
69+
}
70+
}
71+
})
72+
73+
mockedClient['conversations'] = {
74+
conversations: conversationSpy
75+
} as any
76+
77+
const consoleSpy = jest.spyOn(console, 'log');
78+
79+
try {
80+
await listConversationParticipants("CH123", { retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 })
81+
} catch(err) {
82+
expect(consoleSpy).toBeCalledWith('Re-trying on 429 error')
83+
}
84+
})
85+
})

tests/utils/listParticipantConversations.test.ts.skip

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)