-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdate.test.tsx
More file actions
172 lines (152 loc) · 5.27 KB
/
date.test.tsx
File metadata and controls
172 lines (152 loc) · 5.27 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
import { waitFor, screen } 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";
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
global.MutationObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
disconnect: vi.fn(),
}));
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("date field", () => {
let dateField: HTMLParagraphElement;
let visualBuilder: VisualBuilder;
beforeAll(async () => {
dateField = document.createElement("p");
dateField.setAttribute(
"data-cslp",
"all_fields.bltapikey.en-us.date"
);
document.body.appendChild(dateField);
visualBuilder = new VisualBuilder();
await triggerAndWaitForClickAction(
visualBuilderPostMessage,
dateField
);
});
afterAll(() => {
visualBuilder.destroy();
});
test("should have outline", () => {
expect(dateField.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(dateField).toHaveAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY
);
});
});
test("should not contain a contenteditable attribute", async () => {
await waitFor(() => {
expect(dateField).not.toHaveAttribute("contenteditable");
});
});
test("should send a focus field message to parent", async () => {
await waitFor(() => {
expect(visualBuilderPostMessage?.send).toBeCalledWith(
VisualBuilderPostMessageEvents.FOCUS_FIELD,
{
DOMEditStack: getDOMEditStack(dateField),
}
);
});
});
});
});