1+ import { AxiosRequestConfig , AxiosResponse } from "axios" ;
2+
13import postRobot from "post-robot" ;
24
35import UiLocation from "../src/uiLocation" ;
@@ -12,6 +14,8 @@ import {
1214 LocationType ,
1315 Region ,
1416} from "../src/types" ;
17+ import { RequestOption } from "../src/types/common.types" ;
18+ import { RequestConfig } from "../src/types/api.type" ;
1519
1620jest . mock ( "post-robot" ) ;
1721jest . mock ( "wolfy87-eventemitter" ) ;
@@ -57,6 +61,11 @@ const initData: IAppConfigInitData = {
5761 installation_uid : "installation_uid" ,
5862 extension_uid : "extension_uid" ,
5963 region : "NA" ,
64+ endpoints : {
65+ CMA : "https://api.contentstack.io" ,
66+ APP : "https://app.contentstack.app" ,
67+ DEVELOPER_HUB : "" ,
68+ } ,
6069 stack : mockStackData ,
6170 user : { } as any ,
6271 currentBranch : "currentBranch" ,
@@ -131,6 +140,154 @@ describe("UI Location", () => {
131140 } ) ;
132141 } ) ;
133142
143+ describe ( "dispatchPostRobotRequest" , ( ) => {
144+ let mockPostRobot : typeof postRobot ;
145+ let opts : RequestOption ;
146+ let uiLocationInstance : UiLocation ;
147+ let onError : jest . Mock ;
148+
149+ beforeEach ( ( ) => {
150+ mockPostRobot = postRobot ;
151+ opts = { method : "GET" } ;
152+ uiLocationInstance = new UiLocation ( initData ) ;
153+ onError = jest . fn ( ) ;
154+ uiLocationInstance . api = jest . fn ( ) . mockResolvedValue ( {
155+ method : "GET" ,
156+ url : "https://test.com/test?limit=10&skip=0" ,
157+ } ) ;
158+ } ) ;
159+
160+ it ( "should call sendToParent with the correct arguments and resolve with data" , async ( ) => {
161+ const mockData = { success : true } ;
162+ // Call the method that uses uiLocationInstance.api
163+ const result = await uiLocationInstance . api (
164+ "https://test.com/test?limit=10&skip=0" ,
165+ {
166+ method : "GET" ,
167+ }
168+ ) ;
169+
170+ // Assertions
171+ expect ( uiLocationInstance . api ) . toHaveBeenCalledWith (
172+ "https://test.com/test?limit=10&skip=0" ,
173+ {
174+ method : "GET" ,
175+ }
176+ ) ;
177+ expect ( result ) . toEqual ( {
178+ method : "GET" ,
179+ url : "https://test.com/test?limit=10&skip=0" ,
180+ } ) ;
181+ } ) ;
182+
183+ it ( "should call onError if sendToParent rejects" , async ( ) => {
184+ const mockError = new Error ( "Test error" ) ;
185+
186+ // Mock the api method to reject with an error
187+ uiLocationInstance . api = jest . fn ( ) . mockRejectedValue ( mockError ) ;
188+
189+ // Mock the onError implementation
190+ onError . mockImplementation ( ( error ) => {
191+ throw error ;
192+ } ) ;
193+
194+ // Call the method that uses uiLocationInstance.api and expect it to throw an 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" ) ;
203+ } ) ;
204+ } ) ;
205+
206+ describe ( "createSDKAdapter" , ( ) => {
207+ let mockPostRobot : typeof postRobot ;
208+ let opts : RequestConfig ;
209+ let uiLocationInstance : UiLocation ;
210+ let onError : jest . Mock ;
211+
212+ beforeEach ( ( ) => {
213+ mockPostRobot = postRobot ;
214+ opts = {
215+ method : "GET" ,
216+ baseURL : "https://test.com" ,
217+ url : "/test?limit10&skip=0" ,
218+ } ;
219+ uiLocationInstance = new UiLocation ( initData ) ;
220+ onError = jest . fn ( ) ;
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+ ) ;
233+ } ) ;
234+
235+ afterEach ( ( ) => {
236+ postRobotOnMock . mockClear ( ) ;
237+ postRobotSendToParentMock . mockClear ( ) ;
238+
239+ jest . clearAllMocks ( ) ;
240+ window [ "postRobot" ] = undefined ;
241+ window [ "iframeRef" ] = undefined ;
242+ } ) ;
243+
244+ it ( "should call createAdapter with the correct arguments and resolve with data" , async ( ) => {
245+ const mockData = { success : true } ;
246+ // Call the method that uses uiLocationInstance.createAdapter
247+ const result = await uiLocationInstance . createAdapter ( ) ( {
248+ method : "GET" ,
249+ url : "/test?limit=10&skip=0" ,
250+ baseURL : "https://test.com" ,
251+ data : { } ,
252+ } ) ;
253+
254+ expect ( result ) . toEqual ( {
255+ method : "GET" ,
256+ url : "/test?limit=10&skip=0" ,
257+ baseURL : "https://test.com" ,
258+ data : { } ,
259+ } ) ;
260+ } ) ;
261+
262+ it ( "should call onError if createAdapter rejects" , async ( ) => {
263+ const mockError = new Error ( "Test error" ) ;
264+
265+ // Mock the createAdapter method to reject with an error
266+ uiLocationInstance . createAdapter = jest
267+ . fn ( )
268+ . mockImplementation (
269+ ( ) => async ( config : AxiosRequestConfig ) => {
270+ throw mockError ;
271+ }
272+ ) ;
273+
274+ // Mock the onError implementation
275+ onError . mockImplementation ( ( error ) => {
276+ throw error ;
277+ } ) ;
278+
279+ // Call the method that uses uiLocationInstance.createAdapter and expect it to throw an 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" ) ;
288+ } ) ;
289+ } ) ;
290+
134291 describe ( "getConfig" , ( ) => {
135292 it ( "should return config if no installation uid present" , async ( ) => {
136293 const uiLocation = new UiLocation ( initDataJsonRte as any ) ;
0 commit comments