* 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 <[email protected]> Co-authored-by: Anik Dhabal Babu <[email protected]> Co-authored-by: Amit Sharma <[email protected]>
This commit is contained in:
5 files changed
+81
-47
No files matched your search
@@ -26,13 +26,6 @@ describe("Editor", () => {
|
||||
expect(screen.getByText("add_variable")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("respects editable prop", () => {
|
||||
render(<Editor {...defaultProps} editable={false} />);
|
||||
const editor = screen.getByTestId("editor-input");
|
||||
// eslint-disable-next-line playwright/missing-playwright-await
|
||||
expect(editor).toHaveAttribute("contenteditable", "false");
|
||||
});
|
||||
|
||||
it("excludes toolbar items", () => {
|
||||
render(<Editor {...defaultProps} excludedToolbarItems={["bold", "italic"]} />);
|
||||
expect(screen.queryByTitle("Bold")).not.toBeInTheDocument();
|
||||
|
||||
@@ -20,6 +20,7 @@ import ExampleTheme from "./ExampleTheme";
|
||||
import { VariableNode } from "./nodes/VariableNode";
|
||||
import AddVariablesPlugin from "./plugins/AddVariablesPlugin";
|
||||
import AutoLinkPlugin from "./plugins/AutoLinkPlugin";
|
||||
import EditablePlugin from "./plugins/EditablePlugin";
|
||||
import ToolbarPlugin from "./plugins/ToolbarPlugin";
|
||||
import "./stylesEditor.css";
|
||||
|
||||
@@ -70,7 +71,7 @@ export const Editor = (props: TextEditorProps) => {
|
||||
const editable = props.editable ?? true;
|
||||
return (
|
||||
<div className="editor rounded-md">
|
||||
<LexicalComposer initialConfig={{ ...editorConfig, editable }}>
|
||||
<LexicalComposer initialConfig={{ ...editorConfig }}>
|
||||
<div className="editor-container hover:border-emphasis focus-within:ring-brand-default rounded-md p-0 transition focus-within:ring-2">
|
||||
<ToolbarPlugin
|
||||
getText={props.getText}
|
||||
@@ -88,7 +89,6 @@ export const Editor = (props: TextEditorProps) => {
|
||||
<RichTextPlugin
|
||||
contentEditable={
|
||||
<ContentEditable
|
||||
data-testid="editor-input"
|
||||
readOnly={!editable}
|
||||
style={{ height: props.height }}
|
||||
className="editor-input"
|
||||
@@ -117,6 +117,7 @@ export const Editor = (props: TextEditorProps) => {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<EditablePlugin editable={editable} />
|
||||
</LexicalComposer>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { LexicalComposer } from "@lexical/react/LexicalComposer";
|
||||
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
||||
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
|
||||
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import EditablePlugin from "./EditablePlugin";
|
||||
|
||||
const mockSetEditable = vi.fn();
|
||||
|
||||
const mockEditorContext = {
|
||||
setEditable: mockSetEditable,
|
||||
};
|
||||
|
||||
vi.mock("@lexical/react/LexicalComposerContext", () => ({
|
||||
useLexicalComposerContext: () => [mockEditorContext],
|
||||
}));
|
||||
|
||||
function setup(editable: boolean) {
|
||||
const initialConfig = {
|
||||
namespace: "test-editor",
|
||||
onError: (error: Error) => console.error(error),
|
||||
};
|
||||
|
||||
return render(
|
||||
<LexicalComposer initialConfig={initialConfig}>
|
||||
<RichTextPlugin
|
||||
contentEditable={<ContentEditable />}
|
||||
placeholder={<div>Enter some text...</div>}
|
||||
ErrorBoundary={LexicalErrorBoundary}
|
||||
/>
|
||||
<EditablePlugin editable={editable} />
|
||||
</LexicalComposer>
|
||||
);
|
||||
}
|
||||
|
||||
describe("EditablePlugin", () => {
|
||||
beforeEach(() => {
|
||||
mockSetEditable.mockClear();
|
||||
});
|
||||
|
||||
it("sets the editor to editable mode", () => {
|
||||
setup(true);
|
||||
expect(mockSetEditable).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("sets the editor to non-editable mode", () => {
|
||||
setup(false);
|
||||
expect(mockSetEditable).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it("renders without crashing", () => {
|
||||
setup(true);
|
||||
expect(screen.getByText("Enter some text...")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||||
|
||||
export default function EditablePlugin({ editable }: { editable: boolean }) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
editor.setEditable(editable);
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user