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+ } )
0 commit comments