-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml-rte.test.tsx
More file actions
259 lines (227 loc) · 8.42 KB
/
html-rte.test.tsx
File metadata and controls
259 lines (227 loc) · 8.42 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
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 { 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", () => {
let mouseClickEvent: Event;
beforeAll(() => {
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);
mouseClickEvent = new Event("click", {
bubbles: true,
cancelable: true,
});
});
afterAll(() => {
vi.clearAllMocks();
document.body.innerHTML = "";
Config.reset();
});
describe("HTML RTE field", () => {
let htmlRteField: HTMLParagraphElement;
let visualBuilder: VisualBuilder;
beforeAll(async () => {
htmlRteField = document.createElement("p");
htmlRteField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.rich_text_editor"
);
document.body.appendChild(htmlRteField);
visualBuilder = new VisualBuilder();
await triggerAndWaitForClickAction(
visualBuilderPostMessage,
htmlRteField
);
});
afterAll(() => {
visualBuilder.destroy();
});
test("should have outline", () => {
expect(htmlRteField.classList.contains("cslp-edit-mode"));
});
test("should have an overlay", () => {
const overlay = document.querySelector(".visual-builder__overlay");
expect(overlay!.classList.contains("visible"));
});
test("should have a field path dropdown", async () => {
await waitFor(async () => {
const toolbar = await screen.findByTestId(
"mock-field-label-wrapper"
);
expect(toolbar).toBeInTheDocument();
});
});
test("should contain a data-cslp-field-type attribute", async () => {
await waitFor(() => {
expect(htmlRteField).toHaveAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY
);
});
});
test("should not contain a contenteditable attribute", async () => {
await waitFor(() => {
expect(htmlRteField).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(htmlRteField),
}
);
});
});
});
describe("HTML RTE field (multiple)", () => {
let container: HTMLDivElement;
let firstHtmlRteField: HTMLParagraphElement;
let secondHtmlRteField: HTMLParagraphElement;
let visualBuilder: VisualBuilder;
beforeAll(async () => {
container = document.createElement("div");
container.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.rich_text_editor_multiple_"
);
firstHtmlRteField = document.createElement("p");
firstHtmlRteField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.rich_text_editor_multiple_.0"
);
secondHtmlRteField = document.createElement("p");
secondHtmlRteField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.rich_text_editor_multiple_.1"
);
container.appendChild(firstHtmlRteField);
container.appendChild(secondHtmlRteField);
document.body.appendChild(container);
visualBuilder = new VisualBuilder();
await triggerAndWaitForClickAction(
visualBuilderPostMessage,
container
);
});
afterAll(() => {
visualBuilder.destroy();
});
test("should have outline", () => {
expect(container.classList.contains("cslp-edit-mode"));
});
test("should have an overlay", () => {
const overlay = document.querySelector(".visual-builder__overlay");
expect(overlay!.classList.contains("visible"));
});
test("should have a field path dropdown", async () => {
await waitFor(async () => {
const toolbar = await screen.findByTestId(
"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),
}
);
});
});
});
});