Skip to content

Commit d1762aa

Browse files
Merge pull request #52 from contentstack/field-location
feat: entry field location support
2 parents 3f6b000 + 85c0bc2 commit d1762aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+19201
-5135
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"node": true
77
},
88
"parser": "@typescript-eslint/parser",
9-
"plugins": ["@typescript-eslint"],
9+
"plugins": ["@typescript-eslint", "only-warn"],
1010
"extends": [
1111
"eslint:recommended",
1212
"plugin:@typescript-eslint/eslint-recommended",

__test__/entry.test.ts

Lines changed: 96 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ describe("Entry", () => {
2525
};
2626

2727
jest.spyOn(emitter, "on");
28+
29+
const changedData = JSON.parse(JSON.stringify(testData));
30+
changedData.entry.title = "changed title";
31+
2832
// @ts-ignore
29-
entry = new Entry({ data: testData }, connection, emitter);
33+
entry = new Entry({ data: testData, changedData }, connection, emitter);
3034
});
3135

3236
it("init", (done) => {
@@ -50,69 +54,99 @@ describe("Entry", () => {
5054
expect(testData.entry).toEqual(entry.getData());
5155
});
5256

53-
it("getField undefined", function () {
54-
const uid = "group1.group";
55-
const schema = entry.content_type.schema[5].schema[0];
56-
const field = entry.getField(uid);
57-
58-
expect(field.uid).toEqual(uid);
59-
expect(field.data_type).toEqual(schema.data_type);
60-
expect(field.schema).toEqual(schema);
61-
});
62-
63-
it("getField modular blocks, get complete block", function () {
64-
const uid = "modular_blocks.0";
65-
const schema = entry.content_type.schema[6].blocks[2];
66-
const field = entry.getField(uid);
67-
expect(field.uid).toEqual(uid);
68-
expect(field.data_type).toEqual(schema.data_type);
69-
expect(field.schema).toEqual(schema);
70-
});
71-
72-
it("getField modular blocks, get single block", function () {
73-
const uid = "modular_blocks.0.banner";
74-
const schema = entry.content_type.schema[6].blocks[2].schema;
75-
const field = entry.getField(uid);
76-
expect(field.uid).toEqual(uid);
77-
expect(field.data_type).toEqual(schema.data_type);
78-
expect(field.schema).toEqual(schema);
79-
});
80-
81-
it("getField modular blocks, get block field", function () {
82-
const uid = "modular_blocks.0.banner.banner_image";
83-
const schema = entry.content_type.schema[6].blocks[2].schema[0];
84-
const field = entry.getField(uid);
85-
expect(field.uid).toEqual(uid);
86-
expect(field.data_type).toEqual(schema.data_type);
87-
expect(field.schema).toEqual(schema);
88-
});
89-
90-
it("getField global field", function () {
91-
const uid = "global_field.single_line";
92-
const schema = entry.content_type.schema[7].schema[0];
93-
const field = entry.getField(uid);
94-
expect(field.uid).toEqual(uid);
95-
expect(field.data_type).toEqual(schema.data_type);
96-
expect(field.schema).toEqual(schema);
97-
});
57+
describe("getField", () => {
58+
it("getField undefined", function () {
59+
const uid = "group1.group";
60+
const schema = entry.content_type.schema[5].schema[0];
61+
const field = entry.getField(uid);
62+
63+
expect(field.uid).toEqual(uid);
64+
expect(field.data_type).toEqual(schema.data_type);
65+
expect(field.schema).toEqual(schema);
66+
});
67+
68+
it("getField modular blocks, get complete block", function () {
69+
const uid = "modular_blocks.0";
70+
const schema = entry.content_type.schema[6].blocks[2];
71+
const field = entry.getField(uid);
72+
expect(field.uid).toEqual(uid);
73+
expect(field.data_type).toEqual(schema.data_type);
74+
expect(field.schema).toEqual(schema);
75+
});
76+
77+
it("getField modular blocks, get single block", function () {
78+
const uid = "modular_blocks.0.banner";
79+
const schema = entry.content_type.schema[6].blocks[2].schema;
80+
const field = entry.getField(uid);
81+
expect(field.uid).toEqual(uid);
82+
expect(field.data_type).toEqual(schema.data_type);
83+
expect(field.schema).toEqual(schema);
84+
});
85+
86+
it("getField modular blocks, get block field", function () {
87+
const uid = "modular_blocks.0.banner.banner_image";
88+
const schema = entry.content_type.schema[6].blocks[2].schema[0];
89+
const field = entry.getField(uid);
90+
expect(field.uid).toEqual(uid);
91+
expect(field.data_type).toEqual(schema.data_type);
92+
expect(field.schema).toEqual(schema);
93+
});
94+
95+
it("getField global field", function () {
96+
const uid = "global_field.single_line";
97+
const schema = entry.content_type.schema[7].schema[0];
98+
const field = entry.getField(uid);
99+
expect(field.uid).toEqual(uid);
100+
expect(field.data_type).toEqual(schema.data_type);
101+
expect(field.schema).toEqual(schema);
102+
});
103+
104+
it("getField multiple group", function () {
105+
const uid = "group.group.group.0.single_line";
106+
const schema =
107+
entry.content_type.schema[4].schema[0].schema[0].schema[0];
108+
const field = entry.getField(uid);
109+
expect(field.uid).toEqual(uid);
110+
expect(field.data_type).toEqual(schema.data_type);
111+
expect(field.schema).toEqual(schema);
112+
});
113+
114+
it("getField group", function () {
115+
const uid = "group.group.group";
116+
const schema = entry.content_type.schema[4].schema[0].schema[0];
117+
const field = entry.getField(uid);
118+
expect(field.uid).toEqual(uid);
119+
expect(field.data_type).toEqual(schema.data_type);
120+
expect(field.schema).toEqual(schema);
121+
});
122+
123+
it("should use unsaved schema if user set options.useUnsavedSchema = true", () => {
124+
const uid = "title";
125+
const field = entry.getField(uid, { useUnsavedSchema: true });
126+
const schema = entry.content_type.schema[0];
127+
expect(field.uid).toBe(uid);
128+
expect(field.schema).toEqual(schema);
129+
expect(field.data_type).toEqual(schema.data_type);
130+
131+
});
132+
it("should use custom Field instance if internal flag is set", () => {
133+
const fieldInstance = jest.fn();
134+
entry = new Entry(
135+
// @ts-ignore
136+
{ data: testData },
137+
connection,
138+
emitter,
139+
{
140+
_internalFlags: {
141+
FieldInstance: fieldInstance,
142+
},
143+
}
144+
);
98145

99-
it("getField multiple group", function () {
100-
const uid = "group.group.group.0.single_line";
101-
const schema =
102-
entry.content_type.schema[4].schema[0].schema[0].schema[0];
103-
const field = entry.getField(uid);
104-
expect(field.uid).toEqual(uid);
105-
expect(field.data_type).toEqual(schema.data_type);
106-
expect(field.schema).toEqual(schema);
107-
});
146+
entry.getField("title");
108147

109-
it("getField group", function () {
110-
const uid = "group.group.group";
111-
const schema = entry.content_type.schema[4].schema[0].schema[0];
112-
const field = entry.getField(uid);
113-
expect(field.uid).toEqual(uid);
114-
expect(field.data_type).toEqual(schema.data_type);
115-
expect(field.schema).toEqual(schema);
148+
expect(fieldInstance).toHaveBeenCalled();
149+
});
116150
});
117151

118152
it("set field data restriction", async () => {
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import EntryFieldLocationEntry from "../../src/entryFieldLocation/entry";
2+
import testData from "../data/testData.json";
3+
import { IEntryFieldLocationInitData } from "../../src/types";
4+
import { errorMessage } from "../../src/utils/errorMessages";
5+
6+
describe("EntryFieldLocationEntry", () => {
7+
let entryInstance: EntryFieldLocationEntry;
8+
let sendToParent: any;
9+
let connection: { sendToParent: (...props: any[]) => any };
10+
11+
let emitter: any;
12+
function getEntryInitialData(): IEntryFieldLocationInitData {
13+
return {
14+
data: {
15+
type: "ENTRY_FIELD_LOCATION",
16+
config: {},
17+
content_type: {},
18+
entry: {
19+
tags: ["tag1"],
20+
},
21+
locale: "en-us",
22+
uid: "field_uid",
23+
schema: {},
24+
value: {},
25+
self: false,
26+
changedData: {
27+
tags: ["tag1", "tag2"],
28+
},
29+
app_id: "app_id",
30+
currentBranch: "master",
31+
extension_uid: "extension_uid",
32+
installation_uid: "installation_uid",
33+
stack: {
34+
api_key: "api_key",
35+
created_at: "created_at",
36+
is_asset_download_public: true,
37+
name: "name",
38+
master_locale: "en-us",
39+
org_uid: "org_uid",
40+
owner_uid: "owner_uid",
41+
settings: {},
42+
uid: "uid",
43+
updated_at: "updated_at",
44+
user_uids: ["user_uids"],
45+
branches: [],
46+
collaborators: [],
47+
discrete_variables: {
48+
_version: 1,
49+
cms: true,
50+
secret_key: "secret_key",
51+
},
52+
},
53+
user: {},
54+
},
55+
};
56+
}
57+
const entryIntialData: IEntryFieldLocationInitData = getEntryInitialData();
58+
59+
beforeEach(() => {
60+
sendToParent = () => {};
61+
connection = { sendToParent };
62+
63+
emitter = {
64+
on: (_event: any, cbf: (...props: any[]) => void) => {
65+
setTimeout(() => {
66+
cbf({ data: { data: testData.entry, name: "entrySave" } });
67+
cbf({ data: { data: {}, name: "entryPublish" } });
68+
cbf({
69+
data: { data: testData.entry, name: "entryChange" },
70+
});
71+
}, 50);
72+
},
73+
};
74+
75+
jest.spyOn(emitter, "on");
76+
entryInstance = new EntryFieldLocationEntry(
77+
entryIntialData,
78+
connection,
79+
emitter
80+
);
81+
});
82+
83+
describe("getTags", () => {
84+
it("should return saved tags", () => {
85+
expect(entryInstance.getTags()).toEqual(["tag1"]);
86+
});
87+
88+
it("should return draft tags if useUnsavedSchema is true", () => {
89+
expect(entryInstance.getTags({ useUnsavedSchema: true })).toEqual([
90+
"tag1",
91+
"tag2",
92+
]);
93+
});
94+
95+
it("should return currently set tags if useUnsavedSchema is true and tags have been set", async () => {
96+
await entryInstance.setTags(["tag3", "tag4"]);
97+
98+
expect(entryInstance.getTags({ useUnsavedSchema: true })).toEqual([
99+
"tag3",
100+
"tag4",
101+
]);
102+
});
103+
104+
it("should fallback to saved tags if useUnsavedSchema is true and tags have not been set yet", async () => {
105+
delete entryInstance._changedData;
106+
107+
expect(entryInstance.getTags({ useUnsavedSchema: true })).toEqual([
108+
"tag1",
109+
]);
110+
});
111+
112+
it("should return old tags and tags have been set", async () => {
113+
await entryInstance.setTags(["tag3", "tag4"]);
114+
expect(entryInstance.getTags()).toEqual(["tag1"]);
115+
});
116+
});
117+
118+
describe("setTags", () => {
119+
it("should set tags on the entry", async () => {
120+
delete entryInstance._changedData;
121+
expect(await entryInstance.setTags(["tag3", "tag4"])).toEqual([
122+
"tag3",
123+
"tag4",
124+
]);
125+
expect(entryInstance.getTags({ useUnsavedSchema: true })).toEqual([
126+
"tag3",
127+
"tag4",
128+
]);
129+
});
130+
131+
it("should throw an error when tags are not defined", async () => {
132+
// @ts-ignore
133+
await expect(entryInstance.setTags()).rejects.toThrow(
134+
errorMessage.entryField.entry.tagsShouldNotBeBlank
135+
);
136+
});
137+
138+
it("should throw an error if tags is not an array of strings", async () => {
139+
// @ts-ignore
140+
await expect(entryInstance.setTags(["tag3", 4])).rejects.toThrow(
141+
errorMessage.entryField.entry.tagsShouldBeArrayOfStrings
142+
);
143+
144+
// @ts-ignore
145+
await expect(entryInstance.setTags("tag3")).rejects.toThrow(
146+
errorMessage.entryField.entry.tagsShouldBeArrayOfStrings
147+
);
148+
});
149+
150+
it("should update the tags on the entry", async () => {
151+
expect(await entryInstance.setTags(["tag3", "tag4"])).toEqual([
152+
"tag3",
153+
"tag4",
154+
]);
155+
expect(entryInstance.getTags({ useUnsavedSchema: true })).toEqual([
156+
"tag3",
157+
"tag4",
158+
]);
159+
});
160+
});
161+
});

0 commit comments

Comments
 (0)