1- import axios , { AxiosRequestConfig } from 'axios' ;
2- import { containers } from 'containers/containers' ;
3- import { AxiosAdapter } from 'core/adapters' ;
4- import { HttpClientType } from 'core/http' ;
1+ import axios , { AxiosPromise , AxiosRequestConfig } from 'axios' ;
2+ import { AbortPromise } from 'core/http' ;
53import { mockData , mockUrl } from './axios.adapter.mock' ;
4+ import { AxiosAbortAdapter } from '../axios-abort.adapter' ;
5+ import { AxiosMemoAdapter } from '../axios-memo.adapter' ;
6+ import { AxiosAdapter } from '../axios.adapter' ;
67
78jest . mock ( 'axios' , ( ) => {
89 const mockAxios = {
10+ defaults : {
11+ adapter : jest . fn ( config => {
12+ if ( ! config ) {
13+ throw new Error ( 'mockAdapterError' ) ;
14+ }
15+
16+ return Promise . resolve ( ) ;
17+ } ) ,
18+ } ,
919 delete : jest . fn ( ( ) => Promise . resolve ( ) ) ,
1020 get : jest . fn ( ( ) => Promise . resolve ( ) ) ,
1121 head : jest . fn ( ( ) => Promise . resolve ( ) ) ,
22+ interceptors : {
23+ request : {
24+ use : jest . fn ( ( ) => Promise . resolve ( ) ) ,
25+ } ,
26+ response : {
27+ use : jest . fn ( ( ) => Promise . resolve ( ) ) ,
28+ } ,
29+ } ,
1230 options : jest . fn ( ( ) => Promise . resolve ( ) ) ,
1331 patch : jest . fn ( ( ) => Promise . resolve ( ) ) ,
1432 post : jest . fn ( ( ) => Promise . resolve ( ) ) ,
@@ -21,19 +39,29 @@ jest.mock('axios', () => {
2139 } ;
2240} ) ;
2341
24- describe ( 'AxiosAdapterFake ' , ( ) => {
42+ describe ( 'AxiosAdapters ' , ( ) => {
2543 let axiosAdapter : AxiosAdapter ;
44+ let axiosAbortAdapter : AxiosAbortAdapter ;
45+ let axiosMemoAdapter : AxiosMemoAdapter ;
2646 let axiosConfig : AxiosRequestConfig ;
2747
28- describe ( 'should execute methods with correct arguments' , ( ) => {
48+ beforeEach ( ( ) => {
49+ axiosAbortAdapter = new AxiosAbortAdapter ( ) ;
50+ axiosMemoAdapter = new AxiosMemoAdapter ( axiosAbortAdapter ) ;
51+ axiosAdapter = new AxiosAdapter ( axiosMemoAdapter ) ;
52+ } ) ;
53+
54+ afterEach ( ( ) => {
55+ jest . restoreAllMocks ( ) ;
56+ } ) ;
57+
58+ describe ( 'AxiosAdapter' , ( ) => {
2959 beforeEach ( ( ) => {
30- containers . snapshot ( ) ;
31- axiosAdapter = containers . get ( HttpClientType ) as AxiosAdapter ;
60+ axiosAdapter . initialize ( ) ;
3261 axiosConfig = axiosAdapter . getConfig ( ) ;
3362 } ) ;
3463
3564 afterEach ( ( ) => {
36- containers . restore ( ) ;
3765 jest . restoreAllMocks ( ) ;
3866 } ) ;
3967
@@ -42,46 +70,178 @@ describe('AxiosAdapterFake', () => {
4270 expect ( axios . create ) . toHaveBeenCalledWith ( axiosConfig ) ;
4371 } ) ;
4472
45- it ( 'delete ' , async ( ) => {
73+ it ( 'send DELETE request ' , async ( ) => {
4674 await axiosAdapter . delete ( mockUrl , axiosConfig ) ;
4775
4876 expect ( axios . delete ) . toHaveBeenCalledWith ( mockUrl , axiosConfig ) ;
4977 } ) ;
5078
51- it ( 'get ' , async ( ) => {
79+ it ( 'send GET request ' , async ( ) => {
5280 await axiosAdapter . get ( mockUrl , axiosConfig ) ;
5381
5482 expect ( axios . get ) . toHaveBeenCalledWith ( mockUrl , axiosConfig ) ;
5583 } ) ;
5684
57- it ( 'head ' , async ( ) => {
85+ it ( 'send HEAD request ' , async ( ) => {
5886 await axiosAdapter . head ( mockUrl , axiosConfig ) ;
5987
6088 expect ( axios . head ) . toHaveBeenCalledWith ( mockUrl , axiosConfig ) ;
6189 } ) ;
6290
63- it ( 'options ' , async ( ) => {
91+ it ( 'send OPTIONS request ' , async ( ) => {
6492 await axiosAdapter . options ( mockUrl , axiosConfig ) ;
6593
6694 expect ( axios . options ) . toHaveBeenCalledWith ( mockUrl , axiosConfig ) ;
6795 } ) ;
6896
69- it ( 'patch ' , async ( ) => {
97+ it ( 'send PATCH request ' , async ( ) => {
7098 await axiosAdapter . patch ( mockUrl , mockData , axiosConfig ) ;
7199
72100 expect ( axios . patch ) . toHaveBeenCalledWith ( mockUrl , mockData , axiosConfig ) ;
73101 } ) ;
74102
75- it ( 'post ' , async ( ) => {
103+ it ( 'send POST request ' , async ( ) => {
76104 await axiosAdapter . post ( mockUrl , mockData , axiosConfig ) ;
77105
78106 expect ( axios . post ) . toHaveBeenCalledWith ( mockUrl , mockData , axiosConfig ) ;
79107 } ) ;
80108
81- it ( 'put ' , async ( ) => {
109+ it ( 'send PUT request ' , async ( ) => {
82110 await axiosAdapter . put ( mockUrl , mockData , axiosConfig ) ;
83111
84112 expect ( axios . put ) . toHaveBeenCalledWith ( mockUrl , mockData , axiosConfig ) ;
85113 } ) ;
114+
115+ it ( 'set REQUEST interceptors' , ( ) => {
116+ const callback = ( ) => null ;
117+ axiosAdapter . setRequestInterceptors ( callback , callback ) ;
118+
119+ expect ( axios . interceptors . request . use ) . toHaveBeenCalledWith ( callback , callback ) ;
120+ } ) ;
121+
122+ it ( 'set RESPONSE interceptors' , ( ) => {
123+ const callback = ( ) => null ;
124+ axiosAdapter . setResponseInterceptors ( callback , callback ) ;
125+
126+ expect ( axios . interceptors . response . use ) . toHaveBeenCalledWith ( callback , callback ) ;
127+ } ) ;
128+ } ) ;
129+
130+ describe ( 'AxiosMemoAdapter' , ( ) => {
131+ let spyAxiosAbortExecute ;
132+
133+ beforeEach ( ( ) => {
134+ spyAxiosAbortExecute = jest . spyOn ( axiosAbortAdapter , 'execute' ) . mockImplementation (
135+ ( ) : AxiosPromise < string > =>
136+ Promise . resolve ( {
137+ config : { } ,
138+ data : 'response' ,
139+ headers : { } ,
140+ status : 200 ,
141+ statusText : 'OK' ,
142+ } ) ,
143+ ) ;
144+ } ) ;
145+
146+ afterEach ( ( ) => {
147+ spyAxiosAbortExecute . mockReset ( ) ;
148+ spyAxiosAbortExecute . mockRestore ( ) ;
149+ } ) ;
150+
151+ it ( 'shouldn`t put request into cache' , async ( ) => {
152+ const spyGetKey = jest . spyOn ( AxiosMemoAdapter , 'getKey' ) . mockImplementation ( ( ) => 'key' ) ;
153+ const config : AxiosRequestConfig = {
154+ data : { key : 'value' } ,
155+ method : 'GET' ,
156+ params : { pages : 2 } ,
157+ url : 'example.com' ,
158+ } ;
159+ await axiosMemoAdapter . execute ( config ) ;
160+
161+ expect ( spyAxiosAbortExecute ) . toHaveBeenCalledWith ( config ) ;
162+ expect ( spyGetKey ) . toHaveBeenLastCalledWith ( config ) ;
163+
164+ await axiosMemoAdapter . execute ( config ) ;
165+
166+ expect ( spyAxiosAbortExecute ) . toHaveBeenCalledTimes ( 2 ) ;
167+ expect ( spyGetKey ) . toHaveBeenCalledTimes ( 2 ) ;
168+ spyGetKey . mockReset ( ) ;
169+ spyGetKey . mockRestore ( ) ;
170+ } ) ;
171+
172+ it ( 'should put request into cache' , async ( ) => {
173+ const config : AxiosRequestConfig = {
174+ data : { key : 'value' } ,
175+ headers : { cached : true } ,
176+ method : 'GET' ,
177+ params : { pages : 2 } ,
178+ url : 'example.com' ,
179+ } ;
180+ await axiosMemoAdapter . execute ( config ) ;
181+
182+ expect ( spyAxiosAbortExecute ) . toHaveBeenCalledWith ( config ) ;
183+
184+ await axiosMemoAdapter . execute ( config ) ;
185+
186+ expect ( spyAxiosAbortExecute ) . toHaveBeenCalledTimes ( 1 ) ;
187+
188+ await axiosMemoAdapter . execute ( {
189+ ...config ,
190+ headers : { } ,
191+ } ) ;
192+
193+ expect ( spyAxiosAbortExecute ) . toHaveBeenCalledTimes ( 2 ) ;
194+ } ) ;
195+
196+ it ( 'should catch an error' , async ( ) => {
197+ spyAxiosAbortExecute = jest
198+ . spyOn ( axiosAbortAdapter , 'execute' )
199+ . mockImplementation ( ( ) : AxiosPromise < string > => {
200+ throw new Error ( 'mockExecuteError' ) ;
201+ } ) ;
202+ const config : AxiosRequestConfig = {
203+ data : { key : 'value' } ,
204+ headers : { cached : true } ,
205+ method : 'GET' ,
206+ params : { pages : 2 } ,
207+ url : 'example.com' ,
208+ } ;
209+ try {
210+ await axiosMemoAdapter . execute ( config ) ;
211+ } catch ( error ) {
212+ expect ( error ) . toBeInstanceOf ( Error ) ;
213+ expect ( error . message ) . toBe ( 'mockExecuteError' ) ;
214+ }
215+ } ) ;
216+ } ) ;
217+
218+ describe ( 'AxiosAbortAdapter' , ( ) => {
219+ it ( 'should execute adapter with default axios adapter' , async ( ) => {
220+ const spyAbortController = jest . spyOn ( AbortController . prototype , 'abort' ) ;
221+ const config : AxiosRequestConfig = {
222+ data : { key : 'value' } ,
223+ method : 'GET' ,
224+ params : { pages : 2 } ,
225+ url : 'example.com' ,
226+ } ;
227+ const response : AbortPromise < any > = axiosAbortAdapter . execute ( config ) ;
228+ expect ( response ?. abort ) . toBeInstanceOf ( Function ) ;
229+ response . abort ( ) ;
230+ expect ( spyAbortController ) . toHaveBeenCalled ( ) ;
231+ await response ;
232+ expect ( axios . defaults . adapter ) . toBeCalledWith ( config ) ;
233+ spyAbortController . mockReset ( ) ;
234+ spyAbortController . mockRestore ( ) ;
235+ } ) ;
236+
237+ it ( 'should execute adapter and catch an error' , async ( ) => {
238+ try {
239+ const config : AxiosRequestConfig = null ;
240+ await axiosAbortAdapter . execute ( config ) ;
241+ } catch ( error ) {
242+ expect ( error ) . toBeInstanceOf ( Error ) ;
243+ expect ( error . message ) . toBe ( 'mockAdapterError' ) ;
244+ }
245+ } ) ;
86246 } ) ;
87247} ) ;
0 commit comments