@@ -13,7 +13,8 @@ import {
13
13
Region ,
14
14
} from "../src/types" ;
15
15
import { RequestOption } from '../src/types/common.types' ;
16
- import { ApiRequestParams } from '../src/types/api.type' ;
16
+ import { RequestConfig } from '../src/types/api.type' ;
17
+ import { AxiosRequestConfig , AxiosResponse } from 'axios' ;
17
18
18
19
jest . mock ( "post-robot" ) ;
19
20
jest . mock ( "wolfy87-eventemitter" ) ;
@@ -189,22 +190,25 @@ describe("UI Location", () => {
189
190
190
191
describe ( "createSDKAdapter" , ( ) => {
191
192
let mockPostRobot : typeof postRobot ;
192
- let opts : ApiRequestParams ;
193
+ let opts : RequestConfig ;
193
194
let uiLocationInstance : UiLocation ;
194
195
let onError : jest . Mock ;
196
+
195
197
beforeEach ( ( ) => {
196
198
mockPostRobot = postRobot ;
197
- opts = { method : 'GET' , baseURL :"https://test.com" , url :"/test?limit10&skip=0" } ;
199
+ opts = { method : 'GET' , baseURL : "https://test.com" , url : "/test?limit10&skip=0" } ;
198
200
uiLocationInstance = new UiLocation ( initData ) ;
199
201
onError = jest . fn ( ) ;
200
- uiLocationInstance . createAdapter = jest . fn ( ) . mockResolvedValue ( {
201
- method : 'GET' ,
202
- url : '/test?limit=10&skip=0' ,
203
- baseURL : 'https://test.com' ,
204
- data : { }
202
+ uiLocationInstance . createAdapter = jest . fn ( ) . mockImplementation ( ( ) => async ( config : AxiosRequestConfig ) => {
203
+ return {
204
+ method : 'GET' ,
205
+ url : '/test?limit=10&skip=0' ,
206
+ baseURL : 'https://test.com' ,
207
+ data : { }
208
+ } as unknown as AxiosResponse ;
205
209
} ) ;
206
210
} ) ;
207
-
211
+
208
212
afterEach ( ( ) => {
209
213
postRobotOnMock . mockClear ( ) ;
210
214
postRobotSendToParentMock . mockClear ( ) ;
@@ -213,52 +217,46 @@ describe("UI Location", () => {
213
217
window [ "postRobot" ] = undefined ;
214
218
window [ "iframeRef" ] = undefined ;
215
219
} ) ;
216
-
220
+
217
221
it ( 'should call createAdapter with the correct arguments and resolve with data' , async ( ) => {
218
222
const mockData = { success : true } ;
219
- // Call the method that uses uiLocationInstance.api
220
- const result = await uiLocationInstance . createAdapter ( {
221
- method : 'GET' ,
222
- url : '/test?limit=10&skip=0' ,
223
- baseURL : 'https://test.com' ,
224
- data : { }
225
- } ) ;
226
-
227
- // Assertions
228
- expect ( uiLocationInstance . createAdapter ) . toHaveBeenCalledWith ( {
223
+ // Call the method that uses uiLocationInstance.createAdapter
224
+ const result = await uiLocationInstance . createAdapter ( ) ( {
229
225
method : 'GET' ,
230
226
url : '/test?limit=10&skip=0' ,
231
227
baseURL : 'https://test.com' ,
232
228
data : { }
233
229
} ) ;
230
+
234
231
expect ( result ) . toEqual ( {
235
232
method : 'GET' ,
236
233
url : '/test?limit=10&skip=0' ,
237
234
baseURL : 'https://test.com' ,
238
235
data : { }
239
236
} ) ;
240
- } )
241
-
237
+ } ) ;
238
+
242
239
it ( 'should call onError if createAdapter rejects' , async ( ) => {
243
240
const mockError = new Error ( 'Test error' ) ;
244
-
245
- // Mock the api method to reject with an error
246
- uiLocationInstance . createAdapter = jest . fn ( ) . mockRejectedValue ( mockError ) ;
247
-
241
+
242
+ // Mock the createAdapter method to reject with an error
243
+ uiLocationInstance . createAdapter = jest . fn ( ) . mockImplementation ( ( ) => async ( config : AxiosRequestConfig ) => {
244
+ throw mockError ;
245
+ } ) ;
246
+
248
247
// Mock the onError implementation
249
248
onError . mockImplementation ( ( error ) => {
250
249
throw error ;
251
250
} ) ;
252
-
253
- // Call the method that uses uiLocationInstance.api and expect it to throw an error
254
- await expect ( uiLocationInstance . createAdapter ( {
251
+
252
+ // Call the method that uses uiLocationInstance.createAdapter and expect it to throw an error
253
+ await expect ( uiLocationInstance . createAdapter ( ) ( {
255
254
method : 'GET' ,
256
255
url : '/test?limit=10&skip=0' ,
257
256
baseURL : 'https://test.com' ,
258
257
data : { }
259
258
} ) ) . rejects . toThrow ( 'Test error' ) ;
260
- } )
261
-
259
+ } ) ;
262
260
} ) ;
263
261
264
262
describe ( "getConfig" , ( ) => {
0 commit comments