1+ import { listParticipantConversations } from "../../src/utils" ;
2+ import client from '../../src/twilioClient' ;
3+ import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation'
4+
5+ jest . mock ( '../../src/twilioClient' )
6+ let mockedClient = jest . mocked ( client , true )
7+
8+ describe ( 'ListParticipantConversations util' , ( ) => {
9+ beforeEach ( ( ) => {
10+ jest . resetAllMocks ( )
11+ } )
12+
13+ it ( 'it lists conversations based on participant number' , async ( ) => {
14+ const createSpy = jest . fn ( ( options ) => { return options } )
15+ mockedClient [ 'conversations' ] = {
16+ participantConversations : {
17+ list : ( options ) => createSpy ( options )
18+ }
19+ } as any
20+
21+ const result = await listParticipantConversations ( "+1234" ) ;
22+ expect ( createSpy ) . toBeCalledWith ( { "address" :"+1234" } ) ;
23+ expect ( result ) . not . toBeNull ( ) ;
24+ // ToDo: add proper mocked response with ParticipantConversationInstance
25+ } )
26+
27+
28+ it ( 'calls quit if error is not a 429 retry' , async ( ) => {
29+ mockedClient [ 'conversations' ] = {
30+ participantConversations : {
31+ list : ( options ) => {
32+ throw new Error ( 'Twilio Problem' )
33+ }
34+ }
35+ } as any
36+
37+ const consoleSpy = jest . spyOn ( console , 'log' ) ;
38+
39+ try {
40+ await listParticipantConversations ( "+1234" ) ;
41+ } catch ( e ) {
42+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Quit without retry' ) ;
43+ }
44+ } )
45+
46+
47+ it ( 'throws error to retry on 429 status code' , async ( ) => {
48+
49+ interface TwilioError extends Error {
50+ status : number
51+ }
52+
53+ class TwilioError extends Error {
54+ constructor ( message ) {
55+ super ( message ) ;
56+ this . name = "ConcurrencyLimit" ;
57+ this . status = 429
58+ }
59+ }
60+
61+ mockedClient [ 'conversations' ] = {
62+ participantConversations : {
63+ list : ( options ) => {
64+ throw new TwilioError ( 'Too many requests' )
65+ }
66+ }
67+ } as any
68+
69+ const consoleSpy = jest . spyOn ( console , 'log' ) ;
70+
71+ try {
72+ await listParticipantConversations (
73+ "+1234" ,
74+ { retries : 0 , factor : 1 , maxTimeout : 0 , minTimeout : 0 } ) ;
75+ } catch ( e ) {
76+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Re-trying on 429 error' ) ;
77+ }
78+ } )
79+
80+ } )
0 commit comments