File size: 8,958 Bytes
fd2c364 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import {
planComponents,
createPlanComponents,
} from "#/components/features/markdown/plan-components";
describe("planComponents", () => {
describe("h1", () => {
it("should render h1 with correct text content", () => {
// Arrange
const H1 = planComponents.h1;
const text = "Main Heading";
// Act
render(<H1>{text}</H1>);
// Assert
const heading = screen.getByRole("heading", { level: 1 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(text);
});
it("should handle undefined children gracefully", () => {
// Arrange
const H1 = planComponents.h1;
// Act
render(<H1>{undefined}</H1>);
// Assert
const heading = screen.getByRole("heading", { level: 1 });
expect(heading).toBeInTheDocument();
expect(heading).toBeEmptyDOMElement();
});
it("should render complex children content", () => {
// Arrange
const H1 = planComponents.h1;
// Act
render(
<H1>
<span>Nested</span> Content
</H1>,
);
// Assert
const heading = screen.getByRole("heading", { level: 1 });
expect(heading).toHaveTextContent("Nested Content");
expect(heading.querySelector("span")).toHaveTextContent("Nested");
});
});
describe("h2", () => {
it("should render h2 with correct text content", () => {
// Arrange
const H2 = planComponents.h2;
const text = "Section Heading";
// Act
render(<H2>{text}</H2>);
// Assert
const heading = screen.getByRole("heading", { level: 2 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(text);
});
it("should handle null children gracefully", () => {
// Arrange
const H2 = planComponents.h2;
// Act
render(<H2>{null}</H2>);
// Assert
const heading = screen.getByRole("heading", { level: 2 });
expect(heading).toBeInTheDocument();
expect(heading).toBeEmptyDOMElement();
});
});
describe("h3", () => {
it("should render h3 with correct text content", () => {
// Arrange
const H3 = planComponents.h3;
const text = "Subsection Heading";
// Act
render(<H3>{text}</H3>);
// Assert
const heading = screen.getByRole("heading", { level: 3 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(text);
});
});
describe("h4", () => {
it("should render h4 with correct text content", () => {
// Arrange
const H4 = planComponents.h4;
const text = "Level 4 Heading";
// Act
render(<H4>{text}</H4>);
// Assert
const heading = screen.getByRole("heading", { level: 4 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(text);
});
});
describe("h5", () => {
it("should render h5 with correct text content", () => {
// Arrange
const H5 = planComponents.h5;
const text = "Level 5 Heading";
// Act
render(<H5>{text}</H5>);
// Assert
const heading = screen.getByRole("heading", { level: 5 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(text);
});
});
describe("h6", () => {
it("should render h6 with correct text content", () => {
// Arrange
const H6 = planComponents.h6;
const text = "Level 6 Heading";
// Act
render(<H6>{text}</H6>);
// Assert
const heading = screen.getByRole("heading", { level: 6 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(text);
});
});
describe("heading hierarchy", () => {
it("should render all heading levels correctly in sequence", () => {
// Arrange
const H1 = planComponents.h1;
const H2 = planComponents.h2;
const H3 = planComponents.h3;
const H4 = planComponents.h4;
const H5 = planComponents.h5;
const H6 = planComponents.h6;
// Act
render(
<div>
<H1>Heading 1</H1>
<H2>Heading 2</H2>
<H3>Heading 3</H3>
<H4>Heading 4</H4>
<H5>Heading 5</H5>
<H6>Heading 6</H6>
</div>,
);
// Assert
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent(
"Heading 1",
);
expect(screen.getByRole("heading", { level: 2 })).toHaveTextContent(
"Heading 2",
);
expect(screen.getByRole("heading", { level: 3 })).toHaveTextContent(
"Heading 3",
);
expect(screen.getByRole("heading", { level: 4 })).toHaveTextContent(
"Heading 4",
);
expect(screen.getByRole("heading", { level: 5 })).toHaveTextContent(
"Heading 5",
);
expect(screen.getByRole("heading", { level: 6 })).toHaveTextContent(
"Heading 6",
);
});
});
describe("p", () => {
it("should render paragraph with correct text content", () => {
// Arrange
const P = planComponents.p;
const text = "Paragraph text";
// Act
render(<P>{text}</P>);
// Assert
expect(screen.getByText(text)).toBeInTheDocument();
});
});
describe("ul", () => {
it("should render unordered list with correct content", () => {
// Arrange
const Ul = planComponents.ul;
const Li = planComponents.li;
// Act
render(
<Ul>
<Li>Item 1</Li>
<Li>Item 2</Li>
</Ul>,
);
// Assert
expect(screen.getByRole("list")).toBeInTheDocument();
expect(screen.getAllByRole("listitem")).toHaveLength(2);
});
});
describe("ol", () => {
it("should render ordered list with correct content", () => {
// Arrange
const Ol = planComponents.ol;
const Li = planComponents.li;
// Act
render(
<Ol>
<Li>Item 1</Li>
<Li>Item 2</Li>
</Ol>,
);
// Assert
expect(screen.getByRole("list")).toBeInTheDocument();
expect(screen.getAllByRole("listitem")).toHaveLength(2);
});
it("should support start attribute", () => {
// Arrange
const Ol = planComponents.ol;
const Li = planComponents.li;
// Act
const { container } = render(
<Ol start={5}>
<Li>Item 1</Li>
</Ol>,
);
// Assert
const ol = container.querySelector("ol");
expect(ol).toHaveAttribute("start", "5");
});
});
describe("a", () => {
it("should render anchor with correct href and content", () => {
// Arrange
const A = planComponents.a;
const href = "https://example.com";
const text = "Link text";
// Act
render(<A href={href}>{text}</A>);
// Assert
const link = screen.getByRole("link");
expect(link).toHaveTextContent(text);
expect(link).toHaveAttribute("href", href);
expect(link).toHaveAttribute("target", "_blank");
expect(link).toHaveAttribute("rel", "noopener noreferrer");
});
});
describe("code", () => {
it("should render inline code with correct content", () => {
// Arrange
const Code = planComponents.code;
const text = "const x = 1";
// Act
render(<Code>{text}</Code>);
// Assert
expect(screen.getByText(text)).toBeInTheDocument();
});
});
});
describe("createPlanComponents", () => {
it("should apply extraClassName to all elements when provided", () => {
// Arrange
const extraClassName = "test-extra-class";
const components = createPlanComponents(extraClassName);
// Act
const { container } = render(
<div>
<components.h1>H1</components.h1>
<components.p>Paragraph</components.p>
<components.ul>
<components.li>Item</components.li>
</components.ul>
<components.a href="#">Link</components.a>
<components.code>Code</components.code>
</div>,
);
// Assert
expect(container.querySelector("h1")).toHaveClass(extraClassName);
expect(container.querySelector("p")).toHaveClass(extraClassName);
expect(container.querySelector("ul")).toHaveClass(extraClassName);
expect(container.querySelector("li")).toHaveClass(extraClassName);
expect(container.querySelector("a")).toHaveClass(extraClassName);
expect(container.querySelector("code")).toHaveClass(extraClassName);
});
it("should not add undefined class when extraClassName is not provided", () => {
// Arrange
const components = createPlanComponents();
// Act
const { container } = render(<components.h1>H1</components.h1>);
// Assert
const h1 = container.querySelector("h1");
expect(h1?.className).not.toContain("undefined");
});
});
|