|
| 1 | +import React from "preact/compat"; |
| 2 | +import { render, fireEvent, waitFor } from "@testing-library/preact"; |
| 3 | +import { EmptyBlock } from "../emptyBlock"; |
| 4 | +import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage"; |
| 5 | +import { observeParentAndFocusNewInstance } from "../../utils/multipleElementAddButton"; |
| 6 | +import { CslpData } from "../../../cslp/types/cslp.types"; |
| 7 | +import { ISchemaFieldMap } from "../../utils/types/index.types"; |
| 8 | +import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types"; |
| 9 | + |
| 10 | +vi.mock("../../utils/visualBuilderPostMessage", () => ({ |
| 11 | + default: { |
| 12 | + send: vi.fn(), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("../../utils/multipleElementAddButton", () => ({ |
| 17 | + observeParentAndFocusNewInstance: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +describe("EmptyBlock", () => { |
| 21 | + const mockDetails = { |
| 22 | + fieldMetadata: { |
| 23 | + cslpValue: "parent.cslp.value", |
| 24 | + } as CslpData, |
| 25 | + fieldSchema: { |
| 26 | + display_name: "Test Block", |
| 27 | + } as ISchemaFieldMap, |
| 28 | + }; |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + test("should render correctly", () => { |
| 35 | + const { getByText, getByTestId } = render( |
| 36 | + <EmptyBlock details={mockDetails} /> |
| 37 | + ); |
| 38 | + |
| 39 | + expect( |
| 40 | + getByText( |
| 41 | + (_, element) => |
| 42 | + element?.textContent === |
| 43 | + "This page doesn’t have any Test Block added. Click the button below to add one." |
| 44 | + ) |
| 45 | + ).toBeTruthy(); |
| 46 | + expect( |
| 47 | + getByTestId("visual-builder__empty-block-add-button") |
| 48 | + ).toBeTruthy(); |
| 49 | + expect(getByText("Add Test Block")).toBeTruthy(); |
| 50 | + }); |
| 51 | + |
| 52 | + test("should call sendAddInstanceEvent on button click", async () => { |
| 53 | + const { getByTestId } = render(<EmptyBlock details={mockDetails} />); |
| 54 | + const addButton = getByTestId("visual-builder__empty-block-add-button"); |
| 55 | + |
| 56 | + fireEvent.click(addButton); |
| 57 | + |
| 58 | + await waitFor(() => { |
| 59 | + expect((visualBuilderPostMessage as any).send).toHaveBeenCalledWith( |
| 60 | + VisualBuilderPostMessageEvents.ADD_INSTANCE, |
| 61 | + { |
| 62 | + fieldMetadata: mockDetails.fieldMetadata, |
| 63 | + index: 0, |
| 64 | + } |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + expect(observeParentAndFocusNewInstance).toHaveBeenCalledWith({ |
| 69 | + parentCslp: mockDetails.fieldMetadata.cslpValue, |
| 70 | + index: 0, |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments