-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreference.test.tsx
More file actions
276 lines (242 loc) · 9.05 KB
/
reference.test.tsx
File metadata and controls
276 lines (242 loc) · 9.05 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { fireEvent, screen, waitFor } from "@testing-library/preact";
import "@testing-library/jest-dom";
import { getFieldSchemaMap } from "../../../../__test__/data/fieldSchemaMap";
import Config from "../../../../configManager/configManager";
import { VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY } from "../../../utils/constants";
import { FieldSchemaMap } from "../../../utils/fieldSchemaMap";
import { getDOMEditStack } from "../../../utils/getCsDataOfElement";
import visualBuilderPostMessage from "../../../utils/visualBuilderPostMessage";
import { vi } from "vitest";
import { VisualBuilderPostMessageEvents } from "../../../utils/types/postMessage.types";
import { VisualBuilder } from "../../../index";
import {
mockGetBoundingClientRect,
sleep,
triggerAndWaitForClickAction,
} from "../../../../__test__/utils";
vi.mock("../../../components/FieldToolbar", () => {
return {
default: () => {
return <div>Field Toolbar</div>;
},
};
});
vi.mock("../../../components/fieldLabelWrapper", () => {
return {
default: () => {
return (
<div data-testid="mock-field-label-wrapper">Field Label</div>
);
},
};
});
vi.mock("../../../utils/visualBuilderPostMessage", async () => {
const { getAllContentTypes } = await vi.importActual<
typeof import("../../../../__test__/data/contentType")
>("../../../../__test__/data/contentType");
const contentTypes = getAllContentTypes();
return {
__esModule: true,
default: {
send: vi.fn().mockImplementation((eventName: string) => {
if (eventName === "init")
return Promise.resolve({
contentTypes,
});
return Promise.resolve();
}),
on: vi.fn(),
},
};
});
vi.mock("../../../../utils/index.ts", async () => {
const actual = await vi.importActual("../../../../utils");
return {
__esModule: true,
...actual,
isOpenInBuilder: vi.fn().mockReturnValue(true),
};
});
describe("When an element is clicked in visual builder mode", () => {
beforeAll(async () => {
FieldSchemaMap.setFieldSchema(
"all_fields",
getFieldSchemaMap().all_fields
);
vi.spyOn(
document.documentElement,
"clientWidth",
"get"
).mockReturnValue(100);
vi.spyOn(
document.documentElement,
"clientHeight",
"get"
).mockReturnValue(100);
vi.spyOn(document.body, "scrollHeight", "get").mockReturnValue(100);
Config.reset();
Config.set("mode", 2);
});
afterAll(() => {
vi.clearAllMocks();
document.body.innerHTML = "";
Config.reset();
});
describe("reference field", () => {
let referenceField: HTMLParagraphElement;
let visualBuilder: VisualBuilder;
beforeAll(async () => {
referenceField = document.createElement("p");
referenceField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.reference"
);
mockGetBoundingClientRect(referenceField);
document.body.appendChild(referenceField);
visualBuilder = new VisualBuilder();
await triggerAndWaitForClickAction(
visualBuilderPostMessage,
referenceField
);
});
afterAll(() => {
visualBuilder.destroy();
});
test("should have outline", async () => {
const hoverOutline = document.querySelector(
"[data-testid='visual-builder__overlay--outline']"
);
await waitFor(() => {
expect(hoverOutline).toHaveAttribute("style");
});
expect(hoverOutline).toHaveAttribute(
"style",
"top: 10px; height: 5px; width: 10px; left: 10px; outline-color: rgb(113, 92, 221);"
);
});
test("should have an overlay", () => {
const overlay = document.querySelector(".visual-builder__overlay");
expect(overlay!.classList.contains("visible"));
});
test("should have a field path dropdown", () => {
const toolbar = document.querySelector(
"[data-testid='mock-field-label-wrapper']"
);
expect(toolbar).toBeInTheDocument();
});
test("should contain a data-cslp-field-type attribute", async () => {
await waitFor(() => {
expect(referenceField).toHaveAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY
);
});
});
test("should not contain a contenteditable attribute", async () => {
await waitFor(() => {
expect(referenceField).not.toHaveAttribute("contenteditable");
});
});
test.skip("should send a focus field message to parent", async () => {
await waitFor(() => {
expect(visualBuilderPostMessage?.send).toBeCalledWith(
VisualBuilderPostMessageEvents.FOCUS_FIELD,
{
DOMEditStack: getDOMEditStack(referenceField),
}
);
});
});
});
describe("reference field (multiple)", () => {
let container: HTMLDivElement;
let firstReferenceField: HTMLDivElement;
let secondReferenceField: HTMLDivElement;
let visualBuilder: VisualBuilder;
beforeAll(async () => {
container = document.createElement("div");
container.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.reference_multiple_"
);
firstReferenceField = document.createElement("div");
firstReferenceField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.reference_multiple_.0"
);
secondReferenceField = document.createElement("div");
secondReferenceField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.reference_multiple_.1"
);
mockGetBoundingClientRect(container);
container.appendChild(firstReferenceField);
container.appendChild(secondReferenceField);
document.body.appendChild(container);
visualBuilder = new VisualBuilder();
await triggerAndWaitForClickAction(
visualBuilderPostMessage,
container
);
});
afterAll(() => {
visualBuilder.destroy();
});
test("should have outline", async () => {
const hoverOutline = document.querySelector(
"[data-testid='visual-builder__overlay--outline']"
);
await waitFor(() => {
expect(hoverOutline).toHaveAttribute("style");
});
expect(hoverOutline).toHaveAttribute(
"style",
"top: 10px; height: 5px; width: 10px; left: 10px; outline-color: rgb(113, 92, 221);"
);
});
test("should have an overlay", () => {
const overlay = document.querySelector(".visual-builder__overlay");
expect(overlay!.classList.contains("visible"));
});
test("should have a field path dropdown", () => {
const toolbar = document.querySelector(
"[data-testid='mock-field-label-wrapper']"
);
expect(toolbar).toBeInTheDocument();
});
test("should contain a data-cslp-field-type attribute", async () => {
await waitFor(() => {
expect(container).toHaveAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY
);
});
});
test("both container and its children should not contain a contenteditable attribute", async () => {
fireEvent.click(container);
await waitFor(() => {
expect(container).not.toHaveAttribute("contenteditable");
});
fireEvent.click(container.children[0]);
await waitFor(() => {
expect(container.children[0]).not.toHaveAttribute(
"contenteditable"
);
});
fireEvent.click(container.children[1]);
await waitFor(() => {
expect(container.children[1]).not.toHaveAttribute(
"contenteditable"
);
});
});
test.skip("should send a focus field message to parent", async () => {
await waitFor(() => {
expect(visualBuilderPostMessage?.send).toBeCalledWith(
VisualBuilderPostMessageEvents.FOCUS_FIELD,
{
DOMEditStack: getDOMEditStack(container),
}
);
});
});
});
});