@@ -19,10 +19,11 @@ describe('addParticipant util', () => {
1919
2020 it ( 'it adds participant to conversation' , async ( ) => {
2121 let createSpy = jest . fn ( ( options ) => { return mockParticipant } )
22-
23- let participantsSpy = { participants : { create : createSpy } } ;
24-
25- const conversationsSpy = jest . fn ( ( options ) => { return participantsSpy } ) ;
22+ const conversationsSpy = jest . fn ( ( options ) => {
23+ return {
24+ participants : { create : createSpy }
25+ }
26+ } ) ;
2627
2728 mockedClient [ 'conversations' ] = {
2829 conversations : conversationsSpy
@@ -35,5 +36,58 @@ describe('addParticipant util', () => {
3536 expect ( result ) . not . toBeNull ( ) ;
3637 } )
3738
39+ it ( 'calls quit if error is not a 429 retry' , async ( ) => {
40+ let createSpy = jest . fn ( ( options ) => { throw new Error ( 'Twilio Problem' ) } )
41+ const conversationsSpy = jest . fn ( ( options ) => {
42+ return {
43+ participants : { create : createSpy }
44+ }
45+ } ) ;
46+
47+ mockedClient [ 'conversations' ] = {
48+ conversations : conversationsSpy
49+ } as any
50+
51+ const consoleSpy = jest . spyOn ( console , 'log' ) ;
52+
53+ try {
54+ await addParticipant ( "myConversationSid" , mockParticipant ) ;
55+ } catch ( e ) {
56+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Quit without retry' ) ;
57+ }
58+ } )
59+
60+ it ( 'throws error to retry on 429 status code' , async ( ) => {
3861
62+ interface TwilioError extends Error {
63+ status : number
64+ }
65+
66+ class TwilioError extends Error {
67+ constructor ( message ) {
68+ super ( message ) ;
69+ this . name = "ConcurrencyLimit" ;
70+ this . status = 429
71+ }
72+ }
73+
74+ let createSpy = jest . fn ( ( options ) => { throw new TwilioError ( 'Concurrency Limit' ) } )
75+ const conversationsSpy = jest . fn ( ( options ) => {
76+ return {
77+ participants : { create : createSpy }
78+ }
79+ } ) ;
80+
81+ mockedClient [ 'conversations' ] = {
82+ conversations : conversationsSpy
83+ } as any
84+
85+ const consoleSpy = jest . spyOn ( console , 'log' ) ;
86+
87+ try {
88+ await addParticipant ( "myConversationSid" , mockParticipant , { retries : 0 , factor : 1 , maxTimeout : 0 , minTimeout : 0 } ) ;
89+ } catch ( e ) {
90+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Re-trying on 429 error' ) ;
91+ }
92+ } )
3993} )
0 commit comments