Skip to content

Commit df7cfd4

Browse files
committed
fix:updated adapter signature
1 parent 58b84eb commit df7cfd4

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

__test__/uiLocation.test.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "../src/types";
1515
import { RequestOption } from '../src/types/common.types';
1616
import { RequestConfig } from '../src/types/api.type';
17+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
1718

1819
jest.mock("post-robot");
1920
jest.mock("wolfy87-eventemitter");
@@ -192,19 +193,22 @@ describe("UI Location", () => {
192193
let opts: RequestConfig;
193194
let uiLocationInstance: UiLocation;
194195
let onError: jest.Mock;
196+
195197
beforeEach(() => {
196198
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" };
198200
uiLocationInstance = new UiLocation(initData);
199201
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;
205209
});
206210
});
207-
211+
208212
afterEach(() => {
209213
postRobotOnMock.mockClear();
210214
postRobotSendToParentMock.mockClear();
@@ -213,52 +217,46 @@ describe("UI Location", () => {
213217
window["postRobot"] = undefined;
214218
window["iframeRef"] = undefined;
215219
});
216-
220+
217221
it('should call createAdapter with the correct arguments and resolve with data', async () => {
218222
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()({
229225
method: 'GET',
230226
url: '/test?limit=10&skip=0',
231227
baseURL: 'https://test.com',
232228
data: {}
233229
});
230+
234231
expect(result).toEqual({
235232
method: 'GET',
236233
url: '/test?limit=10&skip=0',
237234
baseURL: 'https://test.com',
238235
data: {}
239236
});
240-
})
241-
237+
});
238+
242239
it('should call onError if createAdapter rejects', async () => {
243240
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+
248247
// Mock the onError implementation
249248
onError.mockImplementation((error) => {
250249
throw error;
251250
});
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()({
255254
method: 'GET',
256255
url: '/test?limit=10&skip=0',
257256
baseURL: 'https://test.com',
258257
data: {}
259258
})).rejects.toThrow('Test error');
260-
})
261-
259+
});
262260
});
263261

264262
describe("getConfig", () => {

src/uiLocation.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import {
2929
Region,
3030
IOrgFullPageLocation,
3131
} from "./types";
32-
import { GenericObjectType, RequestOption } from "./types/common.types";
32+
import { GenericObjectType } from "./types/common.types";
3333
import { User } from "./types/user.types";
3434
import { formatAppRegion, onData, onError } from "./utils/utils";
3535
import Window from "./window";
3636
import { dispatchApiRequest, dispatchAdapter } from './utils/adapter';
37-
import {RequestConfig, ProxyResponse } from './types/api.type';
37+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
3838

3939
const emitter = new EventEmitter();
4040

@@ -480,9 +480,12 @@ class UiLocation {
480480

481481
/**
482482
* Method used to create an adapter for management sdk.
483-
*/
484-
485-
createAdapter = (config: RequestConfig) => dispatchAdapter(this.postRobot)(config) as Promise<ProxyResponse>;
483+
*/
484+
createAdapter = (): (config: AxiosRequestConfig) => Promise<AxiosResponse> => {
485+
return (config: AxiosRequestConfig): Promise<AxiosResponse> => {
486+
return dispatchAdapter(postRobot)(config) as Promise<AxiosResponse>;
487+
};
488+
};
486489

487490
/**
488491
* Method used to initialize the App SDK.

0 commit comments

Comments
 (0)