|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, vi } from "vitest"; |
| 3 | + |
| 4 | +import { Example } from "./index"; |
| 5 | + |
| 6 | +describe("<Example.Reason>", () => { |
| 7 | + it("renders the explanation prose", () => { |
| 8 | + render( |
| 9 | + <Example.Reason>Merge-patch is simpler for clients.</Example.Reason>, |
| 10 | + ); |
| 11 | + |
| 12 | + expect( |
| 13 | + screen.getByText(/merge-patch is simpler for clients/i), |
| 14 | + ).toBeInTheDocument(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("renders a 'Why:' lead-in", () => { |
| 18 | + render(<Example.Reason>Some explanation.</Example.Reason>); |
| 19 | + |
| 20 | + expect(screen.getByText("Why:")).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("accepts paragraph children without invalid DOM nesting", () => { |
| 24 | + // MDX wraps indented multi-line children in <p>, so the component's |
| 25 | + // container must not itself be a <p> (React warns via console.error). |
| 26 | + const error = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 27 | + |
| 28 | + render( |
| 29 | + <Example.Reason> |
| 30 | + <p>First sentence of the reason.</p> |
| 31 | + </Example.Reason>, |
| 32 | + ); |
| 33 | + |
| 34 | + expect( |
| 35 | + screen.getByText(/first sentence of the reason/i), |
| 36 | + ).toBeInTheDocument(); |
| 37 | + expect(error).not.toHaveBeenCalled(); |
| 38 | + |
| 39 | + error.mockRestore(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("renders inside an example beneath its code block", () => { |
| 43 | + render( |
| 44 | + <Example.Correct> |
| 45 | + <pre> |
| 46 | + <code>name: list-resources</code> |
| 47 | + </pre> |
| 48 | + <Example.Reason>The name field is required.</Example.Reason> |
| 49 | + </Example.Correct>, |
| 50 | + ); |
| 51 | + const code = screen.getByText("name: list-resources"); |
| 52 | + const reason = screen.getByText(/the name field is required/i); |
| 53 | + |
| 54 | + expect( |
| 55 | + code.compareDocumentPosition(reason) & Node.DOCUMENT_POSITION_FOLLOWING, |
| 56 | + ).toBeTruthy(); |
| 57 | + }); |
| 58 | +}); |
0 commit comments