Skip to content

Commit 1922233

Browse files
authored
Merge pull request #137 from contentstack/feat/MKT-11267-proxy-changes
fix:update api implementation
2 parents 802585e + df7cfd4 commit 1922233

File tree

10 files changed

+522
-279
lines changed

10 files changed

+522
-279
lines changed

__test__/init.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import UiLocation from "../src/uiLocation";
33
import { version } from "../package.json";
44

55
jest.mock("../src/uiLocation");
6+
jest.mock("axios");
67

78
describe("ContentstackAppSDK", () => {
89
describe("init", () => {

__test__/uiLocation.test.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
Region,
1414
} from "../src/types";
1515
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';
1718

1819
jest.mock("post-robot");
1920
jest.mock("wolfy87-eventemitter");
@@ -189,22 +190,25 @@ describe("UI Location", () => {
189190

190191
describe("createSDKAdapter", () => {
191192
let mockPostRobot: typeof postRobot;
192-
let opts: ApiRequestParams;
193+
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", () => {

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module.exports = {
66
testEnvironment: "jsdom",
77
transform: {
88
"^.+\\.(ts|tsx)$": "ts-jest",
9+
"^.+\\.(js|jsx)$": "babel-jest",
10+
},
11+
moduleNameMapper: {
12+
"^axios$": "axios/dist/node/axios.cjs"
913
},
1014
collectCoverageFrom: ["./src/**"],
1115
coveragePathIgnorePatterns: ["<rootDir>.*types.ts"],

0 commit comments

Comments
 (0)