@@ -12,6 +12,8 @@ import {
1212 LocationType ,
1313 Region ,
1414} from "../src/types" ;
15+ import { RequestOption } from '../src/types/common.types' ;
16+ import { ApiRequestParams } from '../src/types/api.type' ;
1517
1618jest . mock ( "post-robot" ) ;
1719jest . mock ( "wolfy87-eventemitter" ) ;
@@ -131,6 +133,134 @@ describe("UI Location", () => {
131133 } ) ;
132134 } ) ;
133135
136+ describe ( 'dispatchPostRobotRequest' , ( ) => {
137+ let mockPostRobot : typeof postRobot ;
138+ let opts : RequestOption ;
139+ let uiLocationInstance : UiLocation ;
140+ let onError : jest . Mock ;
141+
142+ beforeEach ( ( ) => {
143+ mockPostRobot = postRobot ;
144+ opts = { method : 'GET' } ;
145+ uiLocationInstance = new UiLocation ( initData ) ;
146+ onError = jest . fn ( ) ;
147+ uiLocationInstance . api = jest . fn ( ) . mockResolvedValue ( {
148+ method : 'GET' ,
149+ url : "https://test.com/test?limit=10&skip=0"
150+ } ) ;
151+ } ) ;
152+
153+ it ( 'should call sendToParent with the correct arguments and resolve with data' , async ( ) => {
154+ const mockData = { success : true } ;
155+ // Call the method that uses uiLocationInstance.api
156+ const result = await uiLocationInstance . api ( "https://test.com/test?limit=10&skip=0" , {
157+ method : 'GET'
158+ } ) ;
159+
160+ // Assertions
161+ expect ( uiLocationInstance . api ) . toHaveBeenCalledWith ( 'https://test.com/test?limit=10&skip=0' , {
162+ method : 'GET'
163+ } ) ;
164+ expect ( result ) . toEqual ( {
165+ method : 'GET' ,
166+ url : 'https://test.com/test?limit=10&skip=0' ,
167+ } ) ;
168+
169+ } ) ;
170+
171+ it ( 'should call onError if sendToParent rejects' , async ( ) => {
172+ const mockError = new Error ( 'Test error' ) ;
173+
174+ // Mock the api method to reject with an error
175+ uiLocationInstance . api = jest . fn ( ) . mockRejectedValue ( mockError ) ;
176+
177+ // Mock the onError implementation
178+ onError . mockImplementation ( ( error ) => {
179+ throw error ;
180+ } ) ;
181+
182+ // Call the method that uses uiLocationInstance.api and expect it to throw an error
183+ await expect ( uiLocationInstance . api ( "https://test.com/test?limit=10&skip=0" , {
184+ method : 'GET'
185+ } ) ) . rejects . toThrow ( 'Test error' ) ;
186+ } ) ;
187+ } ) ;
188+
189+
190+ describe ( "createSDKAdapter" , ( ) => {
191+ let mockPostRobot : typeof postRobot ;
192+ let opts : ApiRequestParams ;
193+ let uiLocationInstance : UiLocation ;
194+ let onError : jest . Mock ;
195+ beforeEach ( ( ) => {
196+ mockPostRobot = postRobot ;
197+ opts = { method : 'GET' , baseURL :"https://test.com" , url :"/test?limit10&skip=0" } ;
198+ uiLocationInstance = new UiLocation ( initData ) ;
199+ 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 : { }
205+ } ) ;
206+ } ) ;
207+
208+ afterEach ( ( ) => {
209+ postRobotOnMock . mockClear ( ) ;
210+ postRobotSendToParentMock . mockClear ( ) ;
211+
212+ jest . clearAllMocks ( ) ;
213+ window [ "postRobot" ] = undefined ;
214+ window [ "iframeRef" ] = undefined ;
215+ } ) ;
216+
217+ it ( 'should call createAdapter with the correct arguments and resolve with data' , async ( ) => {
218+ 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 ( {
229+ method : 'GET' ,
230+ url : '/test?limit=10&skip=0' ,
231+ baseURL : 'https://test.com' ,
232+ data : { }
233+ } ) ;
234+ expect ( result ) . toEqual ( {
235+ method : 'GET' ,
236+ url : '/test?limit=10&skip=0' ,
237+ baseURL : 'https://test.com' ,
238+ data : { }
239+ } ) ;
240+ } )
241+
242+ it ( 'should call onError if createAdapter rejects' , async ( ) => {
243+ 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+
248+ // Mock the onError implementation
249+ onError . mockImplementation ( ( error ) => {
250+ throw error ;
251+ } ) ;
252+
253+ // Call the method that uses uiLocationInstance.api 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' ) ;
260+ } )
261+
262+ } ) ;
263+
134264 describe ( "getConfig" , ( ) => {
135265 it ( "should return config if no installation uid present" , async ( ) => {
136266 const uiLocation = new UiLocation ( initDataJsonRte as any ) ;
0 commit comments