|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | + |
| 3 | +import { TextRenderer } from "../../../../../packages/page-blocks/src/components/text-renderer"; |
| 4 | + |
| 5 | +jest.mock("@courselit/text-editor", () => ({ |
| 6 | + createExtensions: jest.fn(() => []), |
| 7 | + createId: jest.fn((text: string) => |
| 8 | + text.toLowerCase().replace(/\s+/g, "-"), |
| 9 | + ), |
| 10 | + emptyDoc: { type: "doc", content: [] }, |
| 11 | + extractTextFromNode: jest.fn((node) => |
| 12 | + node?.content?.map((child) => child.text ?? "").join(""), |
| 13 | + ), |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock("@tiptap/static-renderer", () => ({ |
| 17 | + renderToReactElement: jest.fn(({ content }) => { |
| 18 | + const hasEmptyTextNode = (node: any): boolean => { |
| 19 | + if (node?.type === "text" && node.text === "") { |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + return Array.isArray(node?.content) |
| 24 | + ? node.content.some(hasEmptyTextNode) |
| 25 | + : false; |
| 26 | + }; |
| 27 | + |
| 28 | + if (hasEmptyTextNode(content)) { |
| 29 | + throw new RangeError("Empty text nodes are not allowed"); |
| 30 | + } |
| 31 | + |
| 32 | + return <p>{content.content[0].content[0].text}</p>; |
| 33 | + }), |
| 34 | +})); |
| 35 | + |
| 36 | +describe("TextRenderer", () => { |
| 37 | + it("renders content with empty text nodes", () => { |
| 38 | + expect(() => |
| 39 | + render( |
| 40 | + <TextRenderer |
| 41 | + json={ |
| 42 | + { |
| 43 | + type: "doc", |
| 44 | + content: [ |
| 45 | + { |
| 46 | + type: "paragraph", |
| 47 | + content: [ |
| 48 | + { |
| 49 | + type: "text", |
| 50 | + text: "Post body", |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + { |
| 55 | + type: "paragraph", |
| 56 | + content: [ |
| 57 | + { |
| 58 | + type: "text", |
| 59 | + text: "", |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + ], |
| 64 | + } as any |
| 65 | + } |
| 66 | + />, |
| 67 | + ), |
| 68 | + ).not.toThrow(); |
| 69 | + |
| 70 | + expect(screen.getByText("Post body")).toBeInTheDocument(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments