Skip to content

Commit efa55a5

Browse files
author
Kaushik Shetty
authored
Merge pull request #68 from contentstack/develop
v1.5.0
2 parents 2fe95f4 + e7b2e9d commit efa55a5

File tree

18 files changed

+8348
-129
lines changed

18 files changed

+8348
-129
lines changed

__test__/extension.test.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import postRobot from "post-robot";
2+
13
import Extension from "../src/extension";
2-
import { IAppConfigInitData } from "../src/types";
4+
import { IAppConfigInitData, Region } from "../src/types";
35

46
jest.mock("post-robot", () => ({
57
sendToParent: jest.fn(),
@@ -12,6 +14,7 @@ const initData: IAppConfigInitData = {
1214
app_id: "app_id",
1315
installation_uid: "installation_uid",
1416
extension_uid: "extension_uid",
17+
region: "NA",
1518
stack: {
1619
created_at: "created_at",
1720
updated_at: "updated_at",
@@ -30,17 +33,11 @@ const initData: IAppConfigInitData = {
3033
},
3134
};
3235

33-
describe("Main extension", () => {
34-
36+
describe("Extension", () => {
3537
afterEach(() => {
3638
window["postRobot"] = undefined;
3739
window["iframeRef"] = undefined;
38-
})
39-
40-
it("should have modal property", () => {
41-
const extension = new Extension(initData);
42-
expect(extension.modal).toBeDefined();
43-
})
40+
});
4441

4542
describe("Properties in the window object", () => {
4643
it("should have postRobot property", () => {
@@ -54,4 +51,36 @@ describe("Main extension", () => {
5451
);
5552
});
5653
});
54+
55+
it("should have modal property", () => {
56+
const extension = new Extension(initData);
57+
expect(extension.modal).toBeDefined();
58+
});
59+
60+
it("pulse should invoke post robot method with type analytics", () => {
61+
const extensionObj = new Extension(initData);
62+
const eventName = "Sample Event";
63+
const metadata = { foo: "bar" };
64+
extensionObj.pulse(eventName, metadata);
65+
expect((postRobot as any).sendToParent).toHaveBeenCalledWith(
66+
"analytics",
67+
{
68+
eventName,
69+
metadata,
70+
}
71+
);
72+
});
73+
74+
describe("getCurrentRegion", () => {
75+
it("should have getCurrentRegion method", () => {
76+
const extensionObj = new Extension(initData);
77+
expect(extensionObj.getCurrentLocation).toBeDefined();
78+
});
79+
80+
it("should return a valid region", () => {
81+
const extensionObj = new Extension(initData);
82+
const region = extensionObj.getCurrentRegion();
83+
expect(region).toBe(Region.NA);
84+
});
85+
});
5786
});

__test__/extenstion.test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

__test__/fieldModifierLocation/entry.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe("FieldModifierLocationEntry", () => {
3030
currentBranch: "master",
3131
extension_uid: "extension_uid",
3232
installation_uid: "installation_uid",
33+
region: 'NA',
3334
stack: {
3435
api_key: "api_key",
3536
created_at: "created_at",

__test__/stackUtils.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import * as Utils from "../src/stack/utils";
2+
3+
describe("Utils", () => {
4+
it("merge", () => {
5+
const source = {
6+
name: "Molecule Man",
7+
age: 29,
8+
secretIdentity: "Dan Jukes",
9+
powers: ["Radiation resistance", "Turning tiny", "Radiation blast"],
10+
new: {
11+
id: "0001",
12+
type: "donut",
13+
name: "Cake",
14+
image: {
15+
url: "images/0001.jpg",
16+
width: 200,
17+
height: 200,
18+
},
19+
thumbnail: {
20+
url: "images/thumbnails/0001.jpg",
21+
width: 32,
22+
height: 32,
23+
},
24+
},
25+
};
26+
27+
const target = {
28+
name: "Madame Uppercut",
29+
age: 39,
30+
secretIdentity: "Jane Wilson",
31+
powers: [
32+
"Million tonne punch",
33+
"Damage resistance",
34+
"Superhuman reflexes",
35+
],
36+
new: {
37+
id: "0001",
38+
type: "donut",
39+
name: "Cake",
40+
image: {
41+
url: "images/0001.jpg",
42+
width: 200,
43+
height: 200,
44+
},
45+
thumbnail: {
46+
url: "images/thumbnails/0001.jpg",
47+
width: 32,
48+
height: 32,
49+
},
50+
},
51+
};
52+
53+
const merged = Utils.mergeDeep(target, source);
54+
const expected = {
55+
name: "Molecule Man",
56+
age: 29,
57+
secretIdentity: "Dan Jukes",
58+
powers: [
59+
"Million tonne punch",
60+
"Damage resistance",
61+
"Superhuman reflexes",
62+
"Radiation resistance",
63+
"Turning tiny",
64+
"Radiation blast",
65+
],
66+
new: {
67+
id: "0001",
68+
type: "donut",
69+
name: "Cake",
70+
image: { url: "images/0001.jpg", width: 200, height: 200 },
71+
thumbnail: {
72+
url: "images/thumbnails/0001.jpg",
73+
width: 32,
74+
height: 32,
75+
},
76+
},
77+
};
78+
expect(merged).toEqual(expected);
79+
// @ts-ignore
80+
expect(Utils.merge({})).toEqual({});
81+
expect(Utils.merge({ x: "x" }, { z: "z" })).toEqual({ x: "x", z: "z" });
82+
});
83+
});

__test__/utils.test.ts

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,24 @@
1-
import * as Utils from "../src/stack/utils";
1+
import { Region } from "../src/types";
2+
import { formatAppRegion } from "../src/utils/utils";
23

3-
describe("Utils", () => {
4-
it("merge", () => {
5-
const source = {
6-
name: "Molecule Man",
7-
age: 29,
8-
secretIdentity: "Dan Jukes",
9-
powers: ["Radiation resistance", "Turning tiny", "Radiation blast"],
10-
new: {
11-
id: "0001",
12-
type: "donut",
13-
name: "Cake",
14-
image: {
15-
url: "images/0001.jpg",
16-
width: 200,
17-
height: 200,
18-
},
19-
thumbnail: {
20-
url: "images/thumbnails/0001.jpg",
21-
width: 32,
22-
height: 32,
23-
},
24-
},
25-
};
4+
describe("formatAppRegion", () => {
5+
it('should return NA for "NA"', () => {
6+
expect(formatAppRegion("NA")).toBe(Region.NA);
7+
});
8+
9+
it('should return EU for "EU"', () => {
10+
expect(formatAppRegion("EU")).toBe(Region.EU);
11+
});
2612

27-
const target = {
28-
name: "Madame Uppercut",
29-
age: 39,
30-
secretIdentity: "Jane Wilson",
31-
powers: [
32-
"Million tonne punch",
33-
"Damage resistance",
34-
"Superhuman reflexes",
35-
],
36-
new: {
37-
id: "0001",
38-
type: "donut",
39-
name: "Cake",
40-
image: {
41-
url: "images/0001.jpg",
42-
width: 200,
43-
height: 200,
44-
},
45-
thumbnail: {
46-
url: "images/thumbnails/0001.jpg",
47-
width: 32,
48-
height: 32,
49-
},
50-
},
51-
};
13+
it('should return AZURE_NA for "AZURE_NA"', () => {
14+
expect(formatAppRegion("AZURE_NA")).toBe(Region.AZURE_NA);
15+
});
16+
17+
it('should return AZURE_EU for "AZURE_EU"', () => {
18+
expect(formatAppRegion("AZURE_EU")).toBe(Region.AZURE_EU);
19+
});
5220

53-
const merged = Utils.mergeDeep(target, source);
54-
const expected = {
55-
name: "Molecule Man",
56-
age: 29,
57-
secretIdentity: "Dan Jukes",
58-
powers: [
59-
"Million tonne punch",
60-
"Damage resistance",
61-
"Superhuman reflexes",
62-
"Radiation resistance",
63-
"Turning tiny",
64-
"Radiation blast",
65-
],
66-
new: {
67-
id: "0001",
68-
type: "donut",
69-
name: "Cake",
70-
image: { url: "images/0001.jpg", width: 200, height: 200 },
71-
thumbnail: {
72-
url: "images/thumbnails/0001.jpg",
73-
width: 32,
74-
height: 32,
75-
},
76-
},
77-
};
78-
expect(merged).toEqual(expected);
79-
// @ts-ignore
80-
expect(Utils.merge({})).toEqual({});
81-
expect(Utils.merge({ x: "x" }, { z: "z" })).toEqual({ x: "x", z: "z" });
21+
it("should return unknown for any invalid region", () => {
22+
expect(formatAppRegion("invalid")).toBe(Region.UNKNOWN);
8223
});
8324
});

dist/index.js

Lines changed: 8150 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/extension.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Metadata from "./metadata";
44
import Modal from "./modal";
55
import Stack from "./stack";
66
import Store from "./store";
7-
import { IAppConfigInitData, IAppConfigWidget, IAssetSidebarInitData, ICustomField, IDashboardInitData, IDashboardWidget, IEntryFieldLocation, IEntryFieldLocationInitData, IFieldInitData, IFieldModifierLocation, IFieldModifierLocationInitData, IFullPageLocation, IFullPageLocationInitData, ILocation, IPageWidget, IRTEInitData, ISidebarInitData, ISidebarWidget, IUser } from "./types";
7+
import { IAppConfigInitData, IAppConfigWidget, IAssetSidebarInitData, ICustomField, IDashboardInitData, IDashboardWidget, IEntryFieldLocation, IEntryFieldLocationInitData, IFieldInitData, IFieldModifierLocation, IFieldModifierLocationInitData, IFullPageLocation, IFullPageLocationInitData, ILocation, IPageWidget, IRTEInitData, ISidebarInitData, ISidebarWidget, IUser, Region } from "./types";
88
/** Class representing an extension from Contentstack App Framework SDK. */
99
declare class Extension {
1010
/**
@@ -21,6 +21,7 @@ declare class Extension {
2121
metadata: Metadata;
2222
locationUID: string;
2323
modal: Modal;
24+
readonly region: Region;
2425
location: {
2526
DashboardWidget: IDashboardWidget | null;
2627
SidebarWidget: ISidebarWidget | null;
@@ -41,6 +42,7 @@ declare class Extension {
4142
[key: string]: any;
4243
}>;
4344
getCurrentLocation: () => ILocation;
45+
getCurrentRegion: () => Region;
4446
static initialize(version: string): any;
4547
setReady(): any;
4648
}

dist/src/extension.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/types.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ declare interface ICommonInitData {
5858
stack: StackDetail;
5959
user: IUser;
6060
currentBranch: string;
61+
region: string;
6162
}
6263
export declare interface IDashboardInitData {
6364
data: ICommonInitData & {
@@ -209,5 +210,12 @@ export declare interface IManagementTokenDetails {
209210
uid: string;
210211
name: string;
211212
}
213+
export declare enum Region {
214+
UNKNOWN = "UNKNOWN",
215+
NA = "NA",
216+
EU = "EU",
217+
AZURE_NA = "AZURE_NA",
218+
AZURE_EU = "AZURE_EU"
219+
}
212220
export {};
213221
//# sourceMappingURL=types.d.ts.map

0 commit comments

Comments
 (0)