Skip to content

Commit ba2beca

Browse files
committed
tests and retryconfig for listParticipantConversations
1 parent 777fab8 commit ba2beca

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import retry from 'async-retry'
22
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation'
33
import client from '../twilioClient'
4+
import { retryConfig } from "../config/retry.config";
45

5-
export const listParticipantConversations = async (phoneNumber: string) : Promise<ParticipantConversationInstance[]> => {
6+
export const listParticipantConversations = async (phoneNumber: string, retryOptions = retryConfig) : Promise<ParticipantConversationInstance[]> => {
67
return retry(async (quit) => {
78
try {
89
const activeConversations = await client.conversations.participantConversations.list({address: phoneNumber})
910
return activeConversations
1011
} catch (err) {
1112
if (err.status !== 429) {
12-
quit(new Error(err));
13+
console.log('Quit without retry')
14+
console.log(err)
15+
quit(new Error('Quit without retry'));
1316
return;
1417
}
1518

1619
console.log('Re-trying on 429 error');
1720
throw new Error(err);
1821
}
19-
})
22+
}, retryOptions)
2023

2124
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { listParticipantConversations } from "../../src/utils";
2+
import client from '../../src/twilioClient';
3+
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation'
4+
5+
jest.mock('../../src/twilioClient')
6+
let mockedClient = jest.mocked(client, true)
7+
8+
describe('ListParticipantConversations util', () => {
9+
beforeEach(() => {
10+
jest.resetAllMocks()
11+
})
12+
13+
it('it lists conversations based on participant number', async () => {
14+
const createSpy = jest.fn((options) => { return options })
15+
mockedClient['conversations'] = {
16+
participantConversations: {
17+
list: (options) => createSpy(options)
18+
}
19+
} as any
20+
21+
const result = await listParticipantConversations("+1234");
22+
expect(createSpy).toBeCalledWith({"address":"+1234"});
23+
expect(result).not.toBeNull();
24+
// ToDo: add proper mocked response with ParticipantConversationInstance
25+
})
26+
27+
28+
it('calls quit if error is not a 429 retry', async () => {
29+
mockedClient['conversations'] = {
30+
participantConversations: {
31+
list: (options) => {
32+
throw new Error('Twilio Problem')
33+
}
34+
}
35+
} as any
36+
37+
const consoleSpy = jest.spyOn(console, 'log');
38+
39+
try {
40+
await listParticipantConversations("+1234");
41+
} catch (e) {
42+
expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
43+
}
44+
})
45+
46+
47+
it('throws error to retry on 429 status code', async () => {
48+
49+
interface TwilioError extends Error {
50+
status: number
51+
}
52+
53+
class TwilioError extends Error {
54+
constructor(message) {
55+
super(message);
56+
this.name = "ConcurrencyLimit";
57+
this.status = 429
58+
}
59+
}
60+
61+
mockedClient['conversations'] = {
62+
participantConversations: {
63+
list: (options) => {
64+
throw new TwilioError('Too many requests')
65+
}
66+
}
67+
} as any
68+
69+
const consoleSpy = jest.spyOn(console, 'log');
70+
71+
try {
72+
await listParticipantConversations(
73+
"+1234",
74+
{ retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
75+
} catch (e) {
76+
expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
77+
}
78+
})
79+
80+
})

0 commit comments

Comments
 (0)