Skip to content

Commit b0b88cd

Browse files
committed
separate files
1 parent d657b13 commit b0b88cd

3 files changed

Lines changed: 856 additions & 181 deletions

File tree

Lines changed: 388 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,388 @@
1+
import { render, act, findByTestId } from "@testing-library/preact";
2+
import FieldLabelWrapperComponent from "../fieldLabelWrapper";
3+
import { CslpData } from "../../../cslp/types/cslp.types";
4+
import { VisualBuilderCslpEventDetails } from "../../types/visualBuilder.types";
5+
import { singleLineFieldSchema } from "../../../__test__/data/fields";
6+
import { isFieldDisabled } from "../../utils/isFieldDisabled";
7+
import { FieldSchemaMap } from "../../utils/fieldSchemaMap";
8+
import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage";
9+
import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types";
10+
import React from "preact/compat";
11+
12+
// All mocks - same as main test file
13+
vi.mock("../Tooltip", () => ({
14+
ToolbarTooltip: ({ children, data, disabled }: any) => (
15+
<div
16+
data-testid="toolbar-tooltip"
17+
data-disabled={disabled}
18+
data-content-type-name={data.contentTypeName}
19+
data-reference-field-name={data.referenceFieldName}
20+
>
21+
{children}
22+
</div>
23+
),
24+
}));
25+
26+
const testFieldSchemaCache: Record<string, Record<string, any>> = {};
27+
28+
vi.mock("../../utils/fieldSchemaMap", async (importOriginal) => {
29+
const actual =
30+
await importOriginal<typeof import("../../utils/fieldSchemaMap")>();
31+
return {
32+
FieldSchemaMap: {
33+
...actual.FieldSchemaMap,
34+
getFieldSchema: vi
35+
.fn()
36+
.mockImplementation(
37+
(contentTypeUid: string, fieldPath: string) => {
38+
if (testFieldSchemaCache[contentTypeUid]?.[fieldPath]) {
39+
const cachedValue =
40+
testFieldSchemaCache[contentTypeUid][fieldPath];
41+
return Promise.resolve(cachedValue);
42+
}
43+
const defaultSchema = {
44+
display_name: "Field 0",
45+
data_type: "text",
46+
field_metadata: {
47+
description: "",
48+
default_value: "",
49+
version: 3,
50+
},
51+
uid: "test_field",
52+
};
53+
if (!testFieldSchemaCache[contentTypeUid]) {
54+
testFieldSchemaCache[contentTypeUid] = {};
55+
}
56+
testFieldSchemaCache[contentTypeUid][fieldPath] =
57+
defaultSchema;
58+
return Promise.resolve(defaultSchema);
59+
}
60+
),
61+
setFieldSchema: vi
62+
.fn()
63+
.mockImplementation(
64+
(
65+
contentTypeUid: string,
66+
schemaMap: Record<string, any>
67+
) => {
68+
if (!testFieldSchemaCache[contentTypeUid]) {
69+
testFieldSchemaCache[contentTypeUid] = {};
70+
}
71+
Object.assign(
72+
testFieldSchemaCache[contentTypeUid],
73+
schemaMap
74+
);
75+
}
76+
),
77+
hasFieldSchema: vi
78+
.fn()
79+
.mockImplementation(
80+
(contentTypeUid: string, fieldPath: string) => {
81+
return !!testFieldSchemaCache[contentTypeUid]?.[
82+
fieldPath
83+
];
84+
}
85+
),
86+
clear: vi.fn().mockImplementation(() => {
87+
Object.keys(testFieldSchemaCache).forEach(
88+
(key) => delete testFieldSchemaCache[key]
89+
);
90+
}),
91+
},
92+
};
93+
});
94+
95+
vi.mock("../../utils/visualBuilderPostMessage", () => ({
96+
default: {
97+
send: vi.fn().mockImplementation((eventName: string, fields: any) => {
98+
if (
99+
eventName ===
100+
VisualBuilderPostMessageEvents.GET_FIELD_DISPLAY_NAMES
101+
) {
102+
const result: Record<string, string> = {};
103+
if (Array.isArray(fields)) {
104+
fields.forEach((field: any) => {
105+
const cslpValue = field?.cslpValue || field?.cslp || "";
106+
if (!cslpValue) return;
107+
if (cslpValue === "mockFieldCslp") {
108+
result[cslpValue] = "Field 0";
109+
} else if (
110+
cslpValue ===
111+
"contentTypeUid.entryUid.locale.parentPath1"
112+
) {
113+
result[cslpValue] = "Field 1";
114+
} else if (
115+
cslpValue ===
116+
"contentTypeUid.entryUid.locale.parentPath2"
117+
) {
118+
result[cslpValue] = "Field 2";
119+
} else if (
120+
cslpValue ===
121+
"contentTypeUid.entryUid.locale.parentPath3"
122+
) {
123+
result[cslpValue] = "Field 3";
124+
} else {
125+
result[cslpValue] = cslpValue;
126+
}
127+
});
128+
}
129+
return Promise.resolve(result);
130+
} else if (
131+
eventName ===
132+
VisualBuilderPostMessageEvents.GET_CONTENT_TYPE_NAME ||
133+
eventName === "get-content-type-name"
134+
) {
135+
return Promise.resolve({
136+
contentTypeName: "Page CT",
137+
});
138+
} else if (
139+
eventName === VisualBuilderPostMessageEvents.REFERENCE_MAP ||
140+
eventName === "get-reference-map"
141+
) {
142+
return Promise.resolve({});
143+
}
144+
return Promise.resolve({});
145+
}),
146+
},
147+
}));
148+
149+
vi.mock("../../utils/isFieldDisabled", async (importOriginal) => {
150+
const actual =
151+
await importOriginal<typeof import("../../utils/isFieldDisabled")>();
152+
return {
153+
...actual,
154+
isFieldDisabled: vi
155+
.fn()
156+
.mockReturnValue({ isDisabled: false, reason: "" }),
157+
};
158+
});
159+
160+
vi.mock("../../../cslp", () => ({
161+
extractDetailsFromCslp: vi.fn().mockImplementation((path) => {
162+
return {
163+
content_type_uid: "mockContentTypeUid",
164+
fieldPath: path,
165+
cslpValue: path,
166+
};
167+
}),
168+
}));
169+
170+
vi.mock("../../utils/fetchEntryPermissionsAndStageDetails", () => ({
171+
fetchEntryPermissionsAndStageDetails: vi.fn().mockImplementation(() => {
172+
return Promise.resolve({
173+
acl: {
174+
update: {
175+
create: true,
176+
read: true,
177+
update: true,
178+
delete: true,
179+
publish: true,
180+
},
181+
},
182+
workflowStage: {
183+
stage: undefined,
184+
permissions: {
185+
entry: {
186+
update: true,
187+
},
188+
},
189+
},
190+
resolvedVariantPermissions: {
191+
update: true,
192+
},
193+
});
194+
}),
195+
}));
196+
197+
vi.mock("../generators/generateCustomCursor", () => ({
198+
getFieldIcon: vi.fn().mockReturnValue("<svg>mock-icon</svg>"),
199+
FieldTypeIconsMap: {
200+
reference: "<svg>reference-icon</svg>",
201+
},
202+
}));
203+
204+
const mockStyles = {
205+
"visual-builder__focused-toolbar--variant":
206+
"visual-builder__focused-toolbar--variant",
207+
"visual-builder__tooltip--persistent":
208+
"visual-builder__tooltip--persistent",
209+
"visual-builder__custom-tooltip": "visual-builder__custom-tooltip",
210+
"visual-builder__focused-toolbar__field-label-wrapper":
211+
"visual-builder__focused-toolbar__field-label-wrapper",
212+
"visual-builder__focused-toolbar--field-disabled":
213+
"visual-builder__focused-toolbar--field-disabled",
214+
"visual-builder__focused-toolbar__text":
215+
"visual-builder__focused-toolbar__text",
216+
"field-label-dropdown-open": "field-label-dropdown-open",
217+
"visual-builder__button": "visual-builder__button",
218+
"visual-builder__button-loader": "visual-builder__button-loader",
219+
"visual-builder__reference-icon-container":
220+
"visual-builder__reference-icon-container",
221+
"visual-builder__content-type-icon": "visual-builder__content-type-icon",
222+
};
223+
224+
vi.mock("../visualBuilder.style", () => ({
225+
visualBuilderStyles: vi.fn(() => mockStyles),
226+
}));
227+
228+
vi.mock("../VariantIndicator", () => ({
229+
VariantIndicator: () => <div data-testid="variant-indicator">Variant</div>,
230+
}));
231+
232+
vi.mock("../../utils/errorHandling", () => ({
233+
hasPostMessageError: vi.fn().mockReturnValue(false),
234+
}));
235+
236+
const mockFieldMetadata: CslpData = {
237+
entry_uid: "mockEntryUid",
238+
content_type_uid: "mockContentTypeUid",
239+
cslpValue: "mockFieldCslp",
240+
locale: "",
241+
variant: undefined,
242+
fieldPath: "mockFieldPath",
243+
fieldPathWithIndex: "",
244+
multipleFieldMetadata: {
245+
index: 0,
246+
parentDetails: {
247+
parentPath: "",
248+
parentCslpValue: "",
249+
},
250+
},
251+
instance: {
252+
fieldPathWithIndex: "",
253+
},
254+
};
255+
256+
describe("FieldLabelWrapperComponent - isFieldDisabled Tests", () => {
257+
beforeEach(() => {
258+
vi.clearAllMocks();
259+
vi.mocked(isFieldDisabled).mockReturnValue({
260+
isDisabled: false,
261+
reason: "",
262+
});
263+
vi.mocked(visualBuilderPostMessage!.send).mockImplementation(
264+
(eventName: string, fields: any) => {
265+
if (
266+
eventName ===
267+
VisualBuilderPostMessageEvents.GET_FIELD_DISPLAY_NAMES
268+
) {
269+
const result: Record<string, string> = {};
270+
if (Array.isArray(fields)) {
271+
fields.forEach((field: any) => {
272+
const cslpValue =
273+
field?.cslpValue || field?.cslp || "";
274+
if (!cslpValue) return;
275+
if (cslpValue === "mockFieldCslp") {
276+
result[cslpValue] = "Field 0";
277+
} else if (
278+
cslpValue ===
279+
"contentTypeUid.entryUid.locale.parentPath1"
280+
) {
281+
result[cslpValue] = "Field 1";
282+
} else if (
283+
cslpValue ===
284+
"contentTypeUid.entryUid.locale.parentPath2"
285+
) {
286+
result[cslpValue] = "Field 2";
287+
} else if (
288+
cslpValue ===
289+
"contentTypeUid.entryUid.locale.parentPath3"
290+
) {
291+
result[cslpValue] = "Field 3";
292+
} else {
293+
result[cslpValue] = cslpValue;
294+
}
295+
});
296+
}
297+
return Promise.resolve(result);
298+
} else if (
299+
eventName ===
300+
VisualBuilderPostMessageEvents.GET_CONTENT_TYPE_NAME ||
301+
eventName === "get-content-type-name"
302+
) {
303+
return Promise.resolve({
304+
contentTypeName: "Page CT",
305+
});
306+
} else if (
307+
eventName ===
308+
VisualBuilderPostMessageEvents.REFERENCE_MAP ||
309+
eventName === "get-reference-map"
310+
) {
311+
return Promise.resolve({});
312+
}
313+
return Promise.resolve({});
314+
}
315+
);
316+
FieldSchemaMap.setFieldSchema(mockFieldMetadata.content_type_uid, {
317+
[mockFieldMetadata.fieldPath]: singleLineFieldSchema,
318+
});
319+
});
320+
321+
afterEach(() => {
322+
FieldSchemaMap.clear();
323+
document.body.innerHTML = "";
324+
});
325+
326+
afterAll(() => {
327+
vi.clearAllMocks();
328+
});
329+
330+
const mockEventDetails: VisualBuilderCslpEventDetails = {
331+
editableElement: document.createElement("div"),
332+
cslpData: "",
333+
fieldMetadata: mockFieldMetadata,
334+
};
335+
336+
const mockGetParentEditable = () => document.createElement("div");
337+
338+
test("calls isFieldDisabled with correct arguments", async () => {
339+
const { container } = render(
340+
<FieldLabelWrapperComponent
341+
fieldMetadata={mockFieldMetadata}
342+
eventDetails={mockEventDetails}
343+
parentPaths={[]}
344+
getParentEditableElement={mockGetParentEditable}
345+
/>
346+
);
347+
348+
await act(async () => {
349+
await new Promise<void>((resolve) =>
350+
queueMicrotask(() => resolve())
351+
);
352+
});
353+
354+
await findByTestId(
355+
container as HTMLElement,
356+
"visual-builder__focused-toolbar__field-label-wrapper",
357+
{},
358+
{ timeout: 1000 }
359+
);
360+
expect(isFieldDisabled).toHaveBeenCalled();
361+
362+
expect(isFieldDisabled).toHaveBeenCalledWith(
363+
singleLineFieldSchema,
364+
mockEventDetails,
365+
{
366+
update: true,
367+
},
368+
{
369+
update: {
370+
create: true,
371+
read: true,
372+
update: true,
373+
delete: true,
374+
publish: true,
375+
},
376+
},
377+
{
378+
stage: undefined,
379+
permissions: {
380+
entry: {
381+
update: true,
382+
},
383+
},
384+
}
385+
);
386+
});
387+
});
388+

0 commit comments

Comments
 (0)