1+ import { AxiosRequestConfig , AxiosResponse } from "axios" ;
2+
13import postRobot from "post-robot" ;
24
35import UiLocation from "../src/uiLocation" ;
@@ -12,9 +14,8 @@ import {
1214 LocationType ,
1315 Region ,
1416} from "../src/types" ;
15- import { RequestOption } from '../src/types/common.types' ;
16- import { RequestConfig } from '../src/types/api.type' ;
17- import { AxiosRequestConfig , AxiosResponse } from 'axios' ;
17+ import { RequestOption } from "../src/types/common.types" ;
18+ import { RequestConfig } from "../src/types/api.type" ;
1819
1920jest . mock ( "post-robot" ) ;
2021jest . mock ( "wolfy87-eventemitter" ) ;
@@ -60,7 +61,11 @@ const initData: IAppConfigInitData = {
6061 installation_uid : "installation_uid" ,
6162 extension_uid : "extension_uid" ,
6263 region : "NA" ,
63- endpoints : { CMA : "https://api.contentstack.io" , APP : "https://app.contentstack.app" , DEVELOPER_HUB :"" } ,
64+ endpoints : {
65+ CMA : "https://api.contentstack.io" ,
66+ APP : "https://app.contentstack.app" ,
67+ DEVELOPER_HUB : "" ,
68+ } ,
6469 stack : mockStackData ,
6570 user : { } as any ,
6671 currentBranch : "currentBranch" ,
@@ -135,43 +140,48 @@ describe("UI Location", () => {
135140 } ) ;
136141 } ) ;
137142
138- describe ( ' dispatchPostRobotRequest' , ( ) => {
143+ describe ( " dispatchPostRobotRequest" , ( ) => {
139144 let mockPostRobot : typeof postRobot ;
140145 let opts : RequestOption ;
141146 let uiLocationInstance : UiLocation ;
142147 let onError : jest . Mock ;
143148
144149 beforeEach ( ( ) => {
145150 mockPostRobot = postRobot ;
146- opts = { method : ' GET' } ;
151+ opts = { method : " GET" } ;
147152 uiLocationInstance = new UiLocation ( initData ) ;
148153 onError = jest . fn ( ) ;
149154 uiLocationInstance . api = jest . fn ( ) . mockResolvedValue ( {
150- method : ' GET' ,
151- url : "https://test.com/test?limit=10&skip=0"
155+ method : " GET" ,
156+ url : "https://test.com/test?limit=10&skip=0" ,
152157 } ) ;
153158 } ) ;
154159
155- it ( ' should call sendToParent with the correct arguments and resolve with data' , async ( ) => {
160+ it ( " should call sendToParent with the correct arguments and resolve with data" , async ( ) => {
156161 const mockData = { success : true } ;
157162 // Call the method that uses uiLocationInstance.api
158- const result = await uiLocationInstance . api ( "https://test.com/test?limit=10&skip=0" , {
159- method : 'GET'
160- } ) ;
163+ const result = await uiLocationInstance . api (
164+ "https://test.com/test?limit=10&skip=0" ,
165+ {
166+ method : "GET" ,
167+ }
168+ ) ;
161169
162170 // Assertions
163- expect ( uiLocationInstance . api ) . toHaveBeenCalledWith ( 'https://test.com/test?limit=10&skip=0' , {
164- method : 'GET'
165- } ) ;
171+ expect ( uiLocationInstance . api ) . toHaveBeenCalledWith (
172+ "https://test.com/test?limit=10&skip=0" ,
173+ {
174+ method : "GET" ,
175+ }
176+ ) ;
166177 expect ( result ) . toEqual ( {
167- method : ' GET' ,
168- url : ' https://test.com/test?limit=10&skip=0' ,
178+ method : " GET" ,
179+ url : " https://test.com/test?limit=10&skip=0" ,
169180 } ) ;
170-
171181 } ) ;
172182
173- it ( ' should call onError if sendToParent rejects' , async ( ) => {
174- const mockError = new Error ( ' Test error' ) ;
183+ it ( " should call onError if sendToParent rejects" , async ( ) => {
184+ const mockError = new Error ( " Test error" ) ;
175185
176186 // Mock the api method to reject with an error
177187 uiLocationInstance . api = jest . fn ( ) . mockRejectedValue ( mockError ) ;
@@ -182,84 +192,102 @@ describe("UI Location", () => {
182192 } ) ;
183193
184194 // Call the method that uses uiLocationInstance.api and expect it to throw an error
185- await expect ( uiLocationInstance . api ( "https://test.com/test?limit=10&skip=0" , {
186- method : 'GET'
187- } ) ) . rejects . toThrow ( 'Test error' ) ;
195+ await expect (
196+ uiLocationInstance . api (
197+ "https://test.com/test?limit=10&skip=0" ,
198+ {
199+ method : "GET" ,
200+ }
201+ )
202+ ) . rejects . toThrow ( "Test error" ) ;
188203 } ) ;
189204 } ) ;
190205
191-
192206 describe ( "createSDKAdapter" , ( ) => {
193207 let mockPostRobot : typeof postRobot ;
194208 let opts : RequestConfig ;
195209 let uiLocationInstance : UiLocation ;
196210 let onError : jest . Mock ;
197-
211+
198212 beforeEach ( ( ) => {
199213 mockPostRobot = postRobot ;
200- opts = { method : 'GET' , baseURL : "https://test.com" , url : "/test?limit10&skip=0" } ;
214+ opts = {
215+ method : "GET" ,
216+ baseURL : "https://test.com" ,
217+ url : "/test?limit10&skip=0" ,
218+ } ;
201219 uiLocationInstance = new UiLocation ( initData ) ;
202220 onError = jest . fn ( ) ;
203- uiLocationInstance . createAdapter = jest . fn ( ) . mockImplementation ( ( ) => async ( config : AxiosRequestConfig ) => {
204- return {
205- method : 'GET' ,
206- url : '/test?limit=10&skip=0' ,
207- baseURL : 'https://test.com' ,
208- data : { }
209- } as unknown as AxiosResponse ;
210- } ) ;
221+ uiLocationInstance . createAdapter = jest
222+ . fn ( )
223+ . mockImplementation (
224+ ( ) => async ( config : AxiosRequestConfig ) => {
225+ return {
226+ method : "GET" ,
227+ url : "/test?limit=10&skip=0" ,
228+ baseURL : "https://test.com" ,
229+ data : { } ,
230+ } as unknown as AxiosResponse ;
231+ }
232+ ) ;
211233 } ) ;
212-
234+
213235 afterEach ( ( ) => {
214236 postRobotOnMock . mockClear ( ) ;
215237 postRobotSendToParentMock . mockClear ( ) ;
216-
238+
217239 jest . clearAllMocks ( ) ;
218240 window [ "postRobot" ] = undefined ;
219241 window [ "iframeRef" ] = undefined ;
220242 } ) ;
221-
222- it ( ' should call createAdapter with the correct arguments and resolve with data' , async ( ) => {
243+
244+ it ( " should call createAdapter with the correct arguments and resolve with data" , async ( ) => {
223245 const mockData = { success : true } ;
224246 // Call the method that uses uiLocationInstance.createAdapter
225247 const result = await uiLocationInstance . createAdapter ( ) ( {
226- method : ' GET' ,
227- url : ' /test?limit=10&skip=0' ,
228- baseURL : ' https://test.com' ,
229- data : { }
248+ method : " GET" ,
249+ url : " /test?limit=10&skip=0" ,
250+ baseURL : " https://test.com" ,
251+ data : { } ,
230252 } ) ;
231-
253+
232254 expect ( result ) . toEqual ( {
233- method : ' GET' ,
234- url : ' /test?limit=10&skip=0' ,
235- baseURL : ' https://test.com' ,
236- data : { }
255+ method : " GET" ,
256+ url : " /test?limit=10&skip=0" ,
257+ baseURL : " https://test.com" ,
258+ data : { } ,
237259 } ) ;
238260 } ) ;
239-
240- it ( ' should call onError if createAdapter rejects' , async ( ) => {
241- const mockError = new Error ( ' Test error' ) ;
242-
261+
262+ it ( " should call onError if createAdapter rejects" , async ( ) => {
263+ const mockError = new Error ( " Test error" ) ;
264+
243265 // Mock the createAdapter method to reject with an error
244- uiLocationInstance . createAdapter = jest . fn ( ) . mockImplementation ( ( ) => async ( config : AxiosRequestConfig ) => {
245- throw mockError ;
246- } ) ;
247-
266+ uiLocationInstance . createAdapter = jest
267+ . fn ( )
268+ . mockImplementation (
269+ ( ) => async ( config : AxiosRequestConfig ) => {
270+ throw mockError ;
271+ }
272+ ) ;
273+
248274 // Mock the onError implementation
249275 onError . mockImplementation ( ( error ) => {
250276 throw error ;
251277 } ) ;
252-
278+
253279 // Call the method that uses uiLocationInstance.createAdapter and expect it to throw an error
254- await expect ( uiLocationInstance . createAdapter ( ) ( {
255- method : 'GET' ,
256- url : '/test?limit=10&skip=0' ,
257- baseURL : 'https://test.com' ,
258- data : { }
259- } ) ) . rejects . toThrow ( 'Test error' ) ;
280+ await expect (
281+ uiLocationInstance . createAdapter ( ) ( {
282+ method : "GET" ,
283+ url : "/test?limit=10&skip=0" ,
284+ baseURL : "https://test.com" ,
285+ data : { } ,
286+ } )
287+ ) . rejects . toThrow ( "Test error" ) ;
260288 } ) ;
261289 } ) ;
262-
290+
263291 describe ( "getConfig" , ( ) => {
264292 it ( "should return config if no installation uid present" , async ( ) => {
265293 const uiLocation = new UiLocation ( initDataJsonRte as any ) ;
0 commit comments