11import axios from "axios" ;
2+ import AxiosMockAdapter from "axios-mock-adapter" ;
23
34import ContactLists from "../../../lib/api/ContactLists" ;
5+ import handleSendingError from "../../../lib/axios-logger" ;
6+ import { ContactList } from "../../../types/api/contactlist" ;
7+
8+ import CONFIG from "../../../config" ;
9+
10+ const { CLIENT_SETTINGS } = CONFIG ;
11+ const { GENERAL_ENDPOINT } = CLIENT_SETTINGS ;
412
513describe ( "lib/api/ContactLists: " , ( ) => {
14+ let mock : AxiosMockAdapter ;
615 const accountId = 100 ;
716 const contactListsAPI = new ContactLists ( axios , accountId ) ;
817
@@ -17,4 +26,53 @@ describe("lib/api/ContactLists: ", () => {
1726 } ) ;
1827 } ) ;
1928 } ) ;
29+
30+ beforeAll ( ( ) => {
31+ axios . interceptors . response . use (
32+ ( response ) => response . data ,
33+ handleSendingError
34+ ) ;
35+ mock = new AxiosMockAdapter ( axios ) ;
36+ } ) ;
37+
38+ afterEach ( ( ) => {
39+ mock . reset ( ) ;
40+ } ) ;
41+
42+ describe ( "getList(): " , ( ) => {
43+ it ( "successfully gets all contact lists." , async ( ) => {
44+ const endpoint = `${ GENERAL_ENDPOINT } /api/accounts/${ accountId } /contacts/lists` ;
45+ const expectedResponseData : ContactList [ ] = [
46+ { id : 1 , name : "Test List 1" } ,
47+ { id : 2 , name : "Test List 2" } ,
48+ ] ;
49+
50+ expect . assertions ( 2 ) ;
51+
52+ mock . onGet ( endpoint ) . reply ( 200 , expectedResponseData ) ;
53+ const result = await contactListsAPI . getList ( ) ;
54+
55+ expect ( mock . history . get [ 0 ] . url ) . toEqual ( endpoint ) ;
56+ expect ( result ) . toEqual ( expectedResponseData ) ;
57+ } ) ;
58+
59+ it ( "passes the search param to the request." , async ( ) => {
60+ const search = "news" ;
61+ const endpoint = `${ GENERAL_ENDPOINT } /api/accounts/${ accountId } /contacts/lists` ;
62+ const expectedResponseData : ContactList [ ] = [
63+ { id : 1 , name : "Newsletter" } ,
64+ ] ;
65+
66+ expect . assertions ( 3 ) ;
67+
68+ mock
69+ . onGet ( endpoint , { params : { search } } )
70+ . reply ( 200 , expectedResponseData ) ;
71+ const result = await contactListsAPI . getList ( { search } ) ;
72+
73+ expect ( mock . history . get [ 0 ] . url ) . toEqual ( endpoint ) ;
74+ expect ( mock . history . get [ 0 ] . params ) . toEqual ( { search } ) ;
75+ expect ( result ) . toEqual ( expectedResponseData ) ;
76+ } ) ;
77+ } ) ;
2078} ) ;
0 commit comments