Files
calendar/packages/ui/components/editor/Editor.test.tsx
T
4ff2ce90f9 fix: whatsapp message body sometimes editable (Issue #16067) (#16185)
* Fixing issue #16067

* Creating reusable Editor component for Editor

* fix editor issue

* remove log

* fix: unit test

* fix: unit test

* add unit test for the plugin and changes

---------

Co-authored-by: unknown <adhabal2002@gmail.com>
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com>
2024-08-20 16:19:24 +09:00

73 lines
2.6 KiB
TypeScript

import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi } from "vitest";
import type { TextEditorProps } from "./Editor";
import { Editor } from "./Editor";
describe("Editor", () => {
const defaultProps: TextEditorProps = {
getText: vi.fn(),
setText: vi.fn(),
variables: ["name", "email"],
height: "200px",
placeholder: "Start typing...",
};
it("renders editor with default props", () => {
render(<Editor {...defaultProps} />);
expect(screen.getByRole("textbox")).toBeInTheDocument();
expect(screen.getByText("Start typing...")).toBeInTheDocument();
});
it("renders toolbar", () => {
render(<Editor {...defaultProps} />);
expect(screen.getByText("Normal")).toBeInTheDocument();
expect(screen.getByText("add_variable")).toBeInTheDocument();
});
it("excludes toolbar items", () => {
render(<Editor {...defaultProps} excludedToolbarItems={["bold", "italic"]} />);
expect(screen.queryByTitle("Bold")).not.toBeInTheDocument();
expect(screen.queryByTitle("Italic")).not.toBeInTheDocument();
});
it("renders variables plugin when variables are provided", () => {
render(<Editor {...defaultProps} />);
expect(screen.getByText("add_variable")).toBeInTheDocument();
});
it("does not render variables plugin when variables are not provided", () => {
render(<Editor {...defaultProps} variables={undefined} />);
expect(screen.queryByText("add_variable")).not.toBeInTheDocument();
});
it("disables lists when disableLists is true", () => {
render(<Editor {...defaultProps} disableLists={true} />);
expect(screen.queryByTitle("Bullet List")).not.toBeInTheDocument();
expect(screen.queryByTitle("Numbered List")).not.toBeInTheDocument();
});
it("calls getText when text is entered", async () => {
const user = userEvent.setup();
render(<Editor {...defaultProps} />);
const editor = screen.getByRole("textbox");
await user.type(editor, "Hello, world!");
expect(defaultProps.getText).toHaveBeenCalled();
});
it("applies custom height", () => {
render(<Editor {...defaultProps} height="300px" />);
const editorInner = screen.getByRole("textbox").parentElement;
expect(editorInner).toHaveStyle({ height: "300px" });
});
it("handles first render and update template", () => {
const setFirstRender = vi.fn();
render(
<Editor {...defaultProps} updateTemplate={true} firstRender={true} setFirstRender={setFirstRender} />
);
expect(setFirstRender).toHaveBeenCalled();
});
});