Skip to content

Commit f580ae0

Browse files
committed
Finish getConversationByAddressPair test, add deleteConversation nested method example
1 parent 7005961 commit f580ae0

File tree

5 files changed

+114
-100
lines changed

5 files changed

+114
-100
lines changed

src/utils/deleteConversation.util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { retryConfig } from "../config/retry.config";
66
export const deleteConversation = async (conversationSid: string, retryOptions = retryConfig) : Promise<boolean> => {
77
return retry(async(quit) => {
88
try {
9+
console.log(client['conversations']['conversations']['remove'])
910
await client.conversations.conversations(conversationSid).remove()
1011
return true
1112
} catch (err) {
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation';
22
import retry from 'async-retry'
33
import client from '../twilioClient'
4+
import { retryConfig } from '../config/retry.config';
45

5-
export const getConversationByAddressPair = async (address: string, proxyAddress: string) : Promise<ParticipantConversationInstance> => {
6+
export const getConversationByAddressPair = async (address: string, proxyAddress: string, retryOptions = retryConfig) : Promise<ParticipantConversationInstance> => {
67
return retry(async(quit) => {
78
try {
8-
if (address === undefined) {
9-
throw "getOpenConversationsForAddressPair: address is missing";
10-
}
11-
129
const participantConversations = await client.conversations.v1
1310
.participantConversations
1411
.list({ address: address });
15-
12+
1613
const conversation = participantConversations.find(p => {
1714
if (p.conversationState !== 'closed' && p.participantMessagingBinding.proxy_address === proxyAddress) {
1815
console.log(`Found a non-closed conversation ${p.conversationSid} with proxy address ${p.participantMessagingBinding.proxy_address} for address ${address}`);
1916
return p;
2017
}
2118
});
22-
2319
return conversation;
2420
} catch(err) {
2521
if (err.status !== 429) {
@@ -30,5 +26,5 @@ export const getConversationByAddressPair = async (address: string, proxyAddress
3026
console.log('Re-trying on 429 error');
3127
throw new Error(err);
3228
}
33-
})
29+
}, retryOptions)
3430
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import client from '../../src/twilioClient'
3+
jest.mock('../../src/twilioClient')
4+
import { deleteConversation } from "../../src/utils";
5+
6+
7+
describe('deleteConversation util', () => {
8+
beforeEach(() => {
9+
jest.resetAllMocks()
10+
})
11+
12+
it('it deletes conversation', async () => {
13+
let mockedClient = jest.mocked(client, true)
14+
15+
let removeSpy = jest.fn(() => {
16+
return true
17+
})
18+
19+
let conversationsSpy = jest.fn((options) => {
20+
return {
21+
remove: removeSpy
22+
}
23+
})
24+
25+
mockedClient['conversations'] = {
26+
conversations: conversationsSpy
27+
} as any
28+
29+
const result = await deleteConversation("myConversationSid")
30+
expect(result).toBe(true)
31+
32+
expect(conversationsSpy).toBeCalledWith("myConversationSid")
33+
expect(removeSpy).toBeCalled()
34+
})
35+
36+
it.skip('throws an error if Twilio client throws', async () => {
37+
38+
})
39+
40+
it.skip('retrys if error is 429', () => {
41+
42+
})
43+
})

tests/utils/deleteConversation.test.ts.skip

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getConversationByAddressPair } from "../../src/utils";
22
import client from '../../src/twilioClient';
3-
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation';
43

54
jest.mock('../../src/twilioClient')
65
let mockedClient = jest.mocked(client, true)
@@ -10,28 +9,88 @@ describe('GetConversationByAddressPair util', () => {
109
jest.resetAllMocks()
1110
})
1211

13-
it('it gets Conversation by address pair', async () => {
12+
it('it gets Conversation by address pair, excluding closed conversations', async () => {
1413
const mockParticipants = [{
1514
conversationState: "closed",
16-
conversationSid: "myConversationSid",
15+
conversationSid: "myClosedConversationSid",
16+
participantMessagingBinding: {
17+
proxy_address: "+0000"
18+
}
19+
}, {
20+
conversationState: "open",
21+
conversationSid: "myClosedConversationSid",
22+
participantMessagingBinding: {
23+
proxy_address: "+0000"
24+
}
25+
}, {
26+
conversationState: "open",
27+
conversationSid: "myOpenConversationSid",
1728
participantMessagingBinding: {
1829
proxy_address: "+5678"
1930
}
2031
}]
2132

22-
const createSpy = jest.fn((options) => { return mockParticipants })
33+
const listSpy = jest.fn((options) => { return mockParticipants })
2334
mockedClient['conversations'] = {
2435
v1:{
2536
participantConversations: {
26-
list: (options) => createSpy(options)
37+
list: (options) => listSpy(options)
2738
}
2839
}
2940
} as any
3041

3142
const result = await getConversationByAddressPair("+1234","+5678");
32-
expect(createSpy).toBeCalledWith({"address":"+1234"});
33-
expect(result).not.toBeNull();
43+
expect(listSpy).toBeCalledWith({"address":"+1234"});
44+
expect(result).toEqual({conversationSid: "myOpenConversationSid", conversationState: "open", participantMessagingBinding: {proxy_address: "+5678"}});
3445
// ToDo: add proper mocked response with ParticipantConversationInstance
3546
})
47+
48+
it("should throw error if Twilio client throws", async () => {
49+
const createSpy = jest.fn((options) => { throw new Error('Twilio Problem') })
50+
mockedClient['conversations'] = {
51+
v1:{
52+
participantConversations: {
53+
list: (options) => createSpy(options)
54+
}
55+
}
56+
} as any
57+
58+
await expect(getConversationByAddressPair("bad input", "worse input"))
59+
.rejects
60+
.toThrowError('Twilio Proble')
61+
})
62+
63+
it("should retry if Twilio error is 429", async () => {
64+
65+
interface TwilioError extends Error {
66+
status: number
67+
}
68+
69+
class TwilioError extends Error {
70+
constructor(message) {
71+
super(message);
72+
this.name = "ConcurrencyLimit";
73+
this.status = 429
74+
}
75+
}
76+
77+
mockedClient['conversations'] = {
78+
v1:{
79+
participantConversations: {
80+
list: (options) => {
81+
throw new TwilioError('Too many requests')
82+
}
83+
}
84+
}
85+
} as any
86+
87+
const consoleSpy = jest.spyOn(console, 'log');
88+
89+
try {
90+
await getConversationByAddressPair("+1234", "+5678", { retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
91+
} catch (e) {
92+
expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
93+
}
94+
})
3695
})
3796

0 commit comments

Comments
 (0)