1+ import { createConversation } 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('createConversation util', () => {
8+ beforeEach(() => {
9+ jest.resetAllMocks()
10+ })
11+
12+ it('it creates conversation with options passed', async () => {
13+ const createSpy = jest.fn((options) => { return options })
14+ mockedClient['conversations'] = {
15+ conversations: {
16+ create: (options) => createSpy(options)
17+ }
18+ } as any
19+
20+ const result = await createConversation({ friendlyName: "my conversation", addresses: ['1', '2'] })
21+
22+ expect(createSpy).toBeCalledWith({ friendlyName: "my conversation", addresses: ['1', '2'] })
23+ expect(result).toEqual({ friendlyName: "my conversation", addresses: ['1', '2'] })
24+ })
25+
26+ it('calls quit if error is not a 429 retry', async () => {
27+ mockedClient['conversations'] = {
28+ conversations: {
29+ create: (options) => {
30+ throw new Error('Twilio Problem')
31+ }
32+ }
33+ } as any
34+
35+ const consoleSpy = jest.spyOn(console, 'log');
36+
37+ try {
38+ await createConversation({ friendlyName: "my conversation", addresses: ['1', '2'] });
39+ } catch (e) {
40+ expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
41+ }
42+ })
43+
44+ it('throws error to retry on 429 status code', async () => {
45+
46+ interface TwilioError extends Error {
47+ status: number
48+ }
49+
50+ class TwilioError extends Error {
51+ constructor(message) {
52+ super(message);
53+ this.name = "ConcurrencyLimit";
54+ this.status = 429
55+ }
56+ }
57+
58+ mockedClient['conversations'] = {
59+ conversations: {
60+ create: (options) => {
61+ throw new TwilioError('Too many requests')
62+ }
63+ }
64+ } as any
65+
66+ const consoleSpy = jest.spyOn(console, 'log');
67+
68+ try {
69+ await createConversation(
70+ { friendlyName: "my conversation", addresses: ['1', '2']},
71+ { retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
72+ } catch (e) {
73+ expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
74+ }
75+ })
76+ })
0 commit comments