|
| 1 | +import { render, screen } from "@testing-library/svelte"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { expect, test, describe } from "vitest"; |
| 4 | + |
| 5 | +import MarkdownField from "../../lib/Yancy/Editor/src/markdown-field.svelte"; |
| 6 | + |
| 7 | +describe("MarkdownField", () => { |
| 8 | + test("shows markdown field", async () => { |
| 9 | + render(MarkdownField, { id: "mdfield", value: "# heading\n" }); |
| 10 | + const field = screen.getByPlaceholderText("Markdown content"); |
| 11 | + expect(field).toBeInstanceOf(HTMLTextAreaElement); |
| 12 | + expect(field).toHaveValue("# heading\n"); |
| 13 | + |
| 14 | + const button = screen.getByText("Preview"); |
| 15 | + expect(button).toBeVisible(); |
| 16 | + }); |
| 17 | + test("preview button shows rendered markdown", async () => { |
| 18 | + render(MarkdownField, { id: "mdfield", value: "# heading\n\nbody" }); |
| 19 | + screen.getByText("Preview").click(); |
| 20 | + const preview = screen.getByTestId("markdown-preview"); |
| 21 | + expect(preview).toBeVisible(); |
| 22 | + expect(preview).toContainHTML("<h1>heading</h1>"); |
| 23 | + }); |
| 24 | + test("value is updated", async () => { |
| 25 | + const user = userEvent.setup(); |
| 26 | + let value = "# heading\n\nbody"; |
| 27 | + render(MarkdownField, { |
| 28 | + id: "mdfield", |
| 29 | + get value() { |
| 30 | + return value; |
| 31 | + }, |
| 32 | + set value(nextValue) { |
| 33 | + value = nextValue; |
| 34 | + }, |
| 35 | + }); |
| 36 | + const field = screen.getByPlaceholderText("Markdown content"); |
| 37 | + await user.type(field, "\n\n## second heading\n"); |
| 38 | + expect(value).toMatch(/## second heading/); |
| 39 | + }); |
| 40 | +}); |
0 commit comments