-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemptyBlock.test.tsx
More file actions
73 lines (63 loc) · 2.36 KB
/
emptyBlock.test.tsx
File metadata and controls
73 lines (63 loc) · 2.36 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
import React from "preact/compat";
import { render, fireEvent, waitFor } from "@testing-library/preact";
import { EmptyBlock } from "../emptyBlock";
import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage";
import { observeParentAndFocusNewInstance } from "../../utils/multipleElementAddButton";
import { CslpData } from "../../../cslp/types/cslp.types";
import { ISchemaFieldMap } from "../../utils/types/index.types";
import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types";
vi.mock("../../utils/visualBuilderPostMessage", () => ({
default: {
send: vi.fn(),
},
}));
vi.mock("../../utils/multipleElementAddButton", () => ({
observeParentAndFocusNewInstance: vi.fn(),
}));
describe("EmptyBlock", () => {
const mockDetails = {
fieldMetadata: {
cslpValue: "parent.cslp.value",
} as CslpData,
fieldSchema: {
display_name: "Test Block",
} as ISchemaFieldMap,
};
afterEach(() => {
vi.clearAllMocks();
});
test("should render correctly", () => {
const { getByText, getByTestId } = render(
<EmptyBlock details={mockDetails} />
);
expect(
getByText(
(_, element) =>
element?.textContent ===
"This page doesn’t have any Test Block added. Click the button below to add one."
)
).toBeTruthy();
expect(
getByTestId("visual-builder__empty-block-add-button")
).toBeTruthy();
expect(getByText("Add Test Block")).toBeTruthy();
});
test("should call sendAddInstanceEvent on button click", async () => {
const { getByTestId } = render(<EmptyBlock details={mockDetails} />);
const addButton = getByTestId("visual-builder__empty-block-add-button");
fireEvent.click(addButton);
await waitFor(() => {
expect((visualBuilderPostMessage as any).send).toHaveBeenCalledWith(
VisualBuilderPostMessageEvents.ADD_INSTANCE,
{
fieldMetadata: mockDetails.fieldMetadata,
index: 0,
}
);
});
expect(observeParentAndFocusNewInstance).toHaveBeenCalledWith({
parentCslp: mockDetails.fieldMetadata.cslpValue,
index: 0,
});
});
});