Skip to content

Commit 034debe

Browse files
authored
Merge pull request #143 from contentstack/staging
Staging --> Main
2 parents a2807cf + 79640f8 commit 034debe

24 files changed

+760
-16567
lines changed

.eslintrc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
"plugin:@typescript-eslint/eslint-recommended",
1313
"plugin:@typescript-eslint/recommended"
1414
],
15-
"env": {
16-
"browser": true,
17-
"node": true
18-
},
19-
2015
"rules": {
2116
"@typescript-eslint/no-explicit-any": 0,
2217
"@typescript-eslint/prefer-const": 0,

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
node-version: [12.x]
19+
node-version: [18.x]
2020
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2121

2222
steps:

.github/workflows/npm-publish-github-packages.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- uses: actions/checkout@v3
1515
- uses: actions/setup-node@v3
1616
with:
17-
node-version: 14
17+
node-version: 18
1818
- run: npm ci
1919
- run: npm test
2020

@@ -28,7 +28,7 @@ jobs:
2828
- uses: actions/checkout@v3
2929
- uses: actions/setup-node@v3
3030
with:
31-
node-version: 14
31+
node-version: 18
3232
registry-url: https://npm.pkg.github.com/
3333
- run: npm ci
3434
- run: npm publish

.github/workflows/npm-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- uses: actions/checkout@v2
1515
- uses: actions/setup-node@v2
1616
with:
17-
node-version: 16
17+
node-version: 18
1818
- run: npm ci
1919
- run: npm test
2020

@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/checkout@v2
2626
- uses: actions/setup-node@v2
2727
with:
28-
node-version: 16
28+
node-version: 18
2929
registry-url: https://registry.npmjs.org/
3030
- run: npm ci
3131
- run: npm publish

.github/workflows/unit-test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
node-version: [14.x]
19+
node-version: [18.x]
2020

2121
steps:
2222
- name: Checkout repository
2323
uses: actions/checkout@v2
2424
with:
25-
node-version: '14.x'
25+
node-version: '18.x'
2626

2727
- name: Use Node.js
2828
uses: actions/setup-node@v2
2929
with:
30-
node-version: '14.x'
30+
node-version: '18.x'
3131

3232
- name: Install deps
3333
run: npm ci

__test__/assetSidebarWidget.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe("AssetSidebarWidget", () => {
2525
user: {} as any,
2626
currentBranch: "mock_branch",
2727
region: "region",
28+
endpoints: { CMA: "", APP: "",DEVELOPER_HUB:"" },
2829
};
2930

3031
let connection: { sendToParent: (...props: any[]) => any };

__test__/fieldModifierLocation/entry.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe("FieldModifierLocationEntry", () => {
4747
extension_uid: "extension_uid",
4848
installation_uid: "installation_uid",
4949
region: "NA",
50+
endpoints: { CMA: "", APP: "",DEVELOPER_HUB:"" },
5051
stack: {
5152
api_key: "api_key",
5253
created_at: "created_at",

__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", () => {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { IGlobalFullPageLocationInitData, LocationType } from "../src/types";
2+
import { OrganizationDetails } from "../src/types/organization.types";
3+
4+
const mockData: IGlobalFullPageLocationInitData = {
5+
type: LocationType.GLOBAL_FULL_PAGE_LOCATION,
6+
app_id: "app_id",
7+
installation_uid: "installation_uid",
8+
extension_uid: "extension_uid",
9+
region: "NA",
10+
endpoints: { CMA: "", APP: "", DEVELOPER_HUB: "" },
11+
stack: {} as any,
12+
user: {} as any,
13+
currentBranch: "currentBranch",
14+
organization: {} as OrganizationDetails,
15+
};
16+
const organizationFullPage = {
17+
currentOrganization: mockData.organization,
18+
};
19+
20+
afterEach(() => {
21+
jest.clearAllMocks();
22+
});
23+
24+
test("should return organization details", () => {
25+
expect(organizationFullPage.currentOrganization).toBe(
26+
mockData.organization
27+
);
28+
});
29+
30+
test("should handle missing organization details", () => {
31+
const invalidData: IGlobalFullPageLocationInitData = {
32+
...mockData,
33+
organization: null as any, // check missing organization details
34+
};
35+
const invalidOrganizationFullPage = {
36+
currentOrganization: invalidData.organization,
37+
};
38+
expect(invalidOrganizationFullPage.currentOrganization).toBeNull();
39+
});

__test__/uiLocation.test.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AxiosRequestConfig, AxiosResponse } from "axios";
2+
13
import postRobot from "post-robot";
24

35
import 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

1620
jest.mock("post-robot");
1721
jest.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

Comments
 (0)