-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentry.test.ts
More file actions
235 lines (199 loc) · 8.49 KB
/
entry.test.ts
File metadata and controls
235 lines (199 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import Entry from "../src/entry";
import testData from "./data/testData.json";
import { jest } from "@jest/globals";
describe("Entry", () => {
let connection: { sendToParent: (...props: any[]) => any };
let emitter: any;
let entry: Entry;
let sendToParent: any;
beforeEach(() => {
sendToParent = () => {};
connection = { sendToParent };
emitter = {
on: (_event: any, cbf: (...props: any[]) => void) => {
setTimeout(() => {
cbf({ data: { data: testData.entry, name: "entrySave" } });
cbf({ data: { data: {}, name: "entryPublish" } });
cbf({
data: { data: testData.entry, name: "entryChange" },
});
}, 50);
},
};
jest.spyOn(emitter, "on");
const changedData = JSON.parse(JSON.stringify(testData));
changedData.entry.title = "changed title";
entry = new Entry(
{ ...testData, changedData } as any,
connection as any,
emitter
);
});
it("init", (done) => {
setTimeout(function () {
expect(entry.content_type).toEqual(testData.content_type);
expect(entry.locale).toEqual(testData.entry.locale);
expect(emitter.on).toHaveBeenCalledWith(
"entrySave",
expect.any(Function)
);
expect(emitter.on).toHaveBeenCalledWith(
"entryChange",
expect.any(Function)
);
expect(emitter.on).toHaveBeenCalledTimes(2);
done();
}, 100);
});
it("getData", () => {
expect(testData.entry).toEqual(entry.getData());
});
describe("getDraftData", () => {
it("should return draft data successfully", async () => {
const mockDraftData = {
title: "Draft Title",
description: "Draft Description",
};
const sendToParentSpy = jest
.spyOn(connection, "sendToParent")
.mockResolvedValue({ data: mockDraftData });
const result = await entry.getDraftData();
expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData");
expect(result).toEqual(mockDraftData);
});
it("should return empty object when response data is null", async () => {
const sendToParentSpy = jest
.spyOn(connection, "sendToParent")
.mockResolvedValue({ data: null });
const result = await entry.getDraftData();
expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData");
expect(result).toEqual({});
});
it("should return empty object when response data is undefined", async () => {
const sendToParentSpy = jest
.spyOn(connection, "sendToParent")
.mockResolvedValue({ data: undefined });
const result = await entry.getDraftData();
expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData");
expect(result).toEqual({});
});
it("should throw error when sendToParent fails", async () => {
const sendToParentSpy = jest
.spyOn(connection, "sendToParent")
.mockRejectedValue(new Error("Connection failed"));
await expect(entry.getDraftData()).rejects.toThrow(
"Failed to retrieve draft data."
);
expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData");
});
});
describe("getField", () => {
it("getField undefined", function () {
const uid = "group1.group";
const schema = entry.content_type.schema[5].schema[0];
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("getField modular blocks, get complete block", function () {
const uid = "modular_blocks.0";
const schema = entry.content_type.schema[6].blocks[2];
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("getField modular blocks, get single block", function () {
const uid = "modular_blocks.0.banner";
const schema = entry.content_type.schema[6].blocks[2].schema;
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("getField modular blocks, get block field", function () {
const uid = "modular_blocks.0.banner.banner_image";
const schema = entry.content_type.schema[6].blocks[2].schema[0];
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("getField global field", function () {
const uid = "global_field.single_line";
const schema = entry.content_type.schema[7].schema[0];
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("getField multiple group", function () {
const uid = "group.group.group.0.single_line";
const schema =
entry.content_type.schema[4].schema[0].schema[0].schema[0];
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("getField group", function () {
const uid = "group.group.group";
const schema = entry.content_type.schema[4].schema[0].schema[0];
const field = entry.getField(uid);
expect(field.uid).toEqual(uid);
expect(field.data_type).toEqual(schema.data_type);
expect(field.schema).toEqual(schema);
});
it("should use unsaved schema if user set options.useUnsavedSchema = true", () => {
const uid = "title";
const field = entry.getField(uid, { useUnsavedSchema: true });
const schema = entry.content_type.schema[0];
expect(field.uid).toBe(uid);
expect(field.schema).toEqual(schema);
expect(field.data_type).toEqual(schema.data_type);
});
it("should use custom Field instance if internal flag is set", () => {
const fieldInstance: any = jest.fn();
entry = new Entry(testData as any, connection as any, emitter, {
_internalFlags: {
FieldInstance: fieldInstance,
},
});
entry.getField("title");
expect(fieldInstance).toHaveBeenCalled();
});
});
it("set field data restriction", async () => {
const uid = "group.group.group";
const field = entry.getField(uid);
await expect(field.setData({ d: "dummy" })).rejects.toThrowError(
"Cannot call set data for current field type"
);
});
it("set field data restriction for modular blocks, one complete block", async () => {
const uid = "modular_blocks.0";
const field = entry.getField(uid);
await expect(field.setData({ d: "dummy" })).rejects.toThrowError(
"Cannot call set data for current field type"
);
});
it("getField Invalid Uid", function () {
expect(() => entry.getField("invaliduid")).toThrow(
"Invalid uid, Field not found"
);
});
it("getField within Create page", function () {
const dataWithoutEntry = JSON.parse(JSON.stringify(testData));
dataWithoutEntry.entry = {};
entry = new Entry(dataWithoutEntry, connection as any, emitter);
expect(() => entry.getField("invaliduid")).toThrowError(
"Invalid uid, Field not found"
);
});
it("onSave Callback must be a function", function () {
expect(() => (entry as any).onSave()).toThrow(
"Callback must be a function"
);
});
});