Skip to content

Commit f80e8f6

Browse files
committed
Working mocked twilioclient
1 parent a417fcb commit f80e8f6

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

src/utils/createConversation.util.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@ import client from "../twilioClient";
44

55
import retry from 'async-retry';
66

7-
export class CreateConversation {
7+
export const createConversation = async (options: SessionPostBody) : Promise<ConversationInstance> => {
88

9-
async callCreate (options) {
10-
return client.conversations.conversations.create(options);
11-
}
12-
13-
async createConversation (options: SessionPostBody) : Promise<ConversationInstance> {
14-
15-
return retry(async (quit) => {
16-
try {
17-
const conversation = await this.callCreate(options);
18-
return conversation;
19-
} catch (err) {
20-
if (err.status !== 429) {
21-
quit(new Error(err));
22-
return;
23-
}
24-
25-
console.log('Re-trying on 429 error');
26-
throw new Error(err);
9+
return retry(async (quit) => {
10+
try {
11+
const conversation = await client.conversations.conversations.create(options);
12+
return conversation;
13+
} catch (err) {
14+
if (err.status !== 429) {
15+
console.log(err)
16+
quit(new Error('Quit without retry'));
17+
return;
2718
}
28-
})
29-
}
30-
}
19+
20+
console.log('Re-trying on 429 error');
21+
throw new Error(err);
22+
}
23+
})
24+
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
import { ConversationInstance } from 'twilio/lib/rest/conversations/v1/conversation'
2-
import { CreateConversation } from "../../src/utils/createConversation.util"
1+
import { createConversation } from "../../src/utils";
2+
import client from '../../src/twilioClient'
33

4-
describe('conversations service', () => {
5-
const mockValue = {} as ConversationInstance
6-
const testInstance = new CreateConversation
4+
jest.mock('../../src/twilioClient')
5+
let mockedClient = jest.mocked(client, true)
6+
7+
describe('createConversation util', () => {
8+
beforeEach(() => {
9+
jest.resetAllMocks()
10+
})
711

8-
let spy = jest.spyOn(testInstance, 'callCreate').mockResolvedValue(mockValue)
12+
it('it creates conversation with options passed', async () => {
13+
const createSpy = jest.fn((options) => {})
14+
mockedClient['conversations'] = {
15+
conversations: {
16+
create: (options) => createSpy(options)
17+
}
18+
} as any
919

10-
test("It creates a conversation", async () => {
11-
await testInstance.createConversation({friendlyName: 'test', addresses: ['1', '2']})
12-
expect(spy).toBeCalled()
20+
createConversation({ friendlyName: "my conversation", addresses: ['1', '2'] })
21+
22+
expect(createSpy).toBeCalledWith({ friendlyName: "my conversation", addresses: ['1', '2'] })
1323
})
14-
})
1524

25+
it('calls quit if error is not a 429 retry', async () => {
26+
27+
})
28+
})

0 commit comments

Comments
 (0)