Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __test__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UiLocation from "../src/uiLocation";
import { version } from "../package.json";

jest.mock("../src/uiLocation");
jest.mock("axios");

describe("ContentstackAppSDK", () => {
describe("init", () => {
Expand Down
60 changes: 29 additions & 31 deletions __test__/uiLocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
Region,
} from "../src/types";
import { RequestOption } from '../src/types/common.types';
import { ApiRequestParams } from '../src/types/api.type';
import { RequestConfig } from '../src/types/api.type';
import { AxiosRequestConfig, AxiosResponse } from 'axios';

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

describe("createSDKAdapter", () => {
let mockPostRobot: typeof postRobot;
let opts: ApiRequestParams;
let opts: RequestConfig;
let uiLocationInstance: UiLocation;
let onError: jest.Mock;

beforeEach(() => {
mockPostRobot = postRobot;
opts = { method: 'GET', baseURL:"https://test.com", url:"/test?limit10&skip=0" };
opts = { method: 'GET', baseURL: "https://test.com", url: "/test?limit10&skip=0" };
uiLocationInstance = new UiLocation(initData);
onError = jest.fn();
uiLocationInstance.createAdapter = jest.fn().mockResolvedValue({
method: 'GET',
url: '/test?limit=10&skip=0',
baseURL: 'https://test.com',
data: {}
uiLocationInstance.createAdapter = jest.fn().mockImplementation(() => async (config: AxiosRequestConfig) => {
return {
method: 'GET',
url: '/test?limit=10&skip=0',
baseURL: 'https://test.com',
data: {}
} as unknown as AxiosResponse;
});
});

afterEach(() => {
postRobotOnMock.mockClear();
postRobotSendToParentMock.mockClear();
Expand All @@ -213,52 +217,46 @@ describe("UI Location", () => {
window["postRobot"] = undefined;
window["iframeRef"] = undefined;
});

it('should call createAdapter with the correct arguments and resolve with data', async () => {
const mockData = { success: true };
// Call the method that uses uiLocationInstance.api
const result = await uiLocationInstance.createAdapter({
method: 'GET',
url: '/test?limit=10&skip=0',
baseURL: 'https://test.com',
data: {}
});

// Assertions
expect(uiLocationInstance.createAdapter).toHaveBeenCalledWith({
// Call the method that uses uiLocationInstance.createAdapter
const result = await uiLocationInstance.createAdapter()({
method: 'GET',
url: '/test?limit=10&skip=0',
baseURL: 'https://test.com',
data: {}
});

expect(result).toEqual({
method: 'GET',
url: '/test?limit=10&skip=0',
baseURL: 'https://test.com',
data: {}
});
})

});
it('should call onError if createAdapter rejects', async () => {
const mockError = new Error('Test error');

// Mock the api method to reject with an error
uiLocationInstance.createAdapter = jest.fn().mockRejectedValue(mockError);


// Mock the createAdapter method to reject with an error
uiLocationInstance.createAdapter = jest.fn().mockImplementation(() => async (config: AxiosRequestConfig) => {
throw mockError;
});

// Mock the onError implementation
onError.mockImplementation((error) => {
throw error;
});

// Call the method that uses uiLocationInstance.api and expect it to throw an error
await expect(uiLocationInstance.createAdapter({
// Call the method that uses uiLocationInstance.createAdapter and expect it to throw an error
await expect(uiLocationInstance.createAdapter()({
method: 'GET',
url: '/test?limit=10&skip=0',
baseURL: 'https://test.com',
data: {}
})).rejects.toThrow('Test error');
})

});
});

describe("getConfig", () => {
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports = {
testEnvironment: "jsdom",
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest",
},
moduleNameMapper: {
"^axios$": "axios/dist/node/axios.cjs"
},
collectCoverageFrom: ["./src/**"],
coveragePathIgnorePatterns: ["<rootDir>.*types.ts"],
Expand Down
Loading
Loading