|
1 | 1 | import { describe, expect, it } from "vitest"; |
| 2 | +import { userEvent } from "vitest/browser"; |
2 | 3 | import { render } from "vitest-browser-react"; |
3 | 4 | import type { DocumentJson } from "../parser/document"; |
4 | 5 | import { groupConsecutiveNonTableValues, P } from "./P"; |
5 | 6 | import { TagCatalogProvider } from "./TagCatalogProvider"; |
6 | 7 | import { tagCatalog } from "./tagCatalog"; |
7 | 8 |
|
| 9 | +// ...existing code... |
| 10 | + |
| 11 | +it("should have a tooltip for InlineFigure inside paragraph if it contains displayable content", async () => { |
| 12 | + const jsonValue: DocumentJson = { |
| 13 | + tag: "p", |
| 14 | + attributes: {}, |
| 15 | + value: [ |
| 16 | + { tag: "#text", value: "This is a paragraph with an inline figure: " }, |
| 17 | + { |
| 18 | + tag: "figure", |
| 19 | + value: [ |
| 20 | + { |
| 21 | + tag: "figDesc", |
| 22 | + attributes: {}, |
| 23 | + value: [{ tag: "#text", value: "This is a figure description." }], |
| 24 | + }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + { |
| 28 | + tag: "#text", |
| 29 | + value: ".", |
| 30 | + }, |
| 31 | + ], |
| 32 | + }; |
| 33 | + |
| 34 | + const screen = await render(<P data={jsonValue} />, { |
| 35 | + wrapper: ({ children }) => ( |
| 36 | + <TagCatalogProvider tagCatalog={tagCatalog}> |
| 37 | + {children} |
| 38 | + </TagCatalogProvider> |
| 39 | + ), |
| 40 | + }); |
| 41 | + |
| 42 | + const inlineFigure = screen.getByText("figure.unloaded"); |
| 43 | + await expect.element(inlineFigure).toBeVisible(); |
| 44 | + |
| 45 | + // Use userEvent.hover from vitest browser context |
| 46 | + await userEvent.hover(inlineFigure.element()); |
| 47 | + |
| 48 | + // Wait for tooltip to appear |
| 49 | + const tooltip = screen.getByRole("tooltip"); |
| 50 | + await expect.element(tooltip).toBeVisible(); |
| 51 | + await expect |
| 52 | + .element(tooltip) |
| 53 | + .toHaveTextContent("This is a figure description."); |
| 54 | +}); |
| 55 | + |
8 | 56 | describe("groupConsecutiveNonTableValues", () => { |
9 | 57 | it("should return a single group when there are no tables", () => { |
10 | 58 | const values: DocumentJson[] = [ |
@@ -301,4 +349,111 @@ describe("P", () => { |
301 | 349 | .element(screen.getByRole("paragraph")) |
302 | 350 | .toHaveTextContent("Valid text"); |
303 | 351 | }); |
| 352 | + |
| 353 | + it("should interpret nested p as NoOp and not create nested <p> tags", async () => { |
| 354 | + const jsonValue: DocumentJson = { |
| 355 | + tag: "p", |
| 356 | + attributes: {}, |
| 357 | + value: [ |
| 358 | + { tag: "#text", value: "This is a paragraph with " }, |
| 359 | + { |
| 360 | + tag: "p", |
| 361 | + attributes: {}, |
| 362 | + value: [{ tag: "#text", value: "nested p tag" }], |
| 363 | + }, |
| 364 | + { |
| 365 | + tag: "#text", |
| 366 | + value: ".", |
| 367 | + }, |
| 368 | + ], |
| 369 | + }; |
| 370 | + |
| 371 | + const screen = await render(<P data={jsonValue} />, { |
| 372 | + wrapper: ({ children }) => ( |
| 373 | + <TagCatalogProvider tagCatalog={tagCatalog}> |
| 374 | + {children} |
| 375 | + </TagCatalogProvider> |
| 376 | + ), |
| 377 | + }); |
| 378 | + |
| 379 | + expect(screen.getByRole("paragraph")).toHaveTextContent( |
| 380 | + "This is a paragraph with nested p tag.", |
| 381 | + ); |
| 382 | + }); |
| 383 | + |
| 384 | + it("should render figure with image as InlineFigure inside paragraph", async () => { |
| 385 | + const jsonValue: DocumentJson = { |
| 386 | + tag: "p", |
| 387 | + attributes: {}, |
| 388 | + value: [ |
| 389 | + { tag: "#text", value: "This is a paragraph with an inline figure: " }, |
| 390 | + { |
| 391 | + tag: "figure", |
| 392 | + value: [ |
| 393 | + { |
| 394 | + tag: "graphic", |
| 395 | + }, |
| 396 | + ], |
| 397 | + }, |
| 398 | + { |
| 399 | + tag: "#text", |
| 400 | + value: ".", |
| 401 | + }, |
| 402 | + ], |
| 403 | + }; |
| 404 | + |
| 405 | + const screen = await render(<P data={jsonValue} />, { |
| 406 | + wrapper: ({ children }) => ( |
| 407 | + <TagCatalogProvider tagCatalog={tagCatalog}> |
| 408 | + {children} |
| 409 | + </TagCatalogProvider> |
| 410 | + ), |
| 411 | + }); |
| 412 | + |
| 413 | + expect(screen.getByRole("paragraph")).toHaveTextContent( |
| 414 | + "This is a paragraph with an inline figure: figure.unloaded.", |
| 415 | + ); |
| 416 | + }); |
| 417 | + |
| 418 | + it("should have a tooltip for InlineFigure inside paragraph if it contains displayable content", async () => { |
| 419 | + const jsonValue: DocumentJson = { |
| 420 | + tag: "p", |
| 421 | + attributes: {}, |
| 422 | + value: [ |
| 423 | + { tag: "#text", value: "This is a paragraph with an inline figure: " }, |
| 424 | + { |
| 425 | + tag: "figure", |
| 426 | + value: [ |
| 427 | + { |
| 428 | + tag: "figDesc", |
| 429 | + attributes: {}, |
| 430 | + value: [{ tag: "#text", value: "This is a figure description." }], |
| 431 | + }, |
| 432 | + ], |
| 433 | + }, |
| 434 | + { |
| 435 | + tag: "#text", |
| 436 | + value: ".", |
| 437 | + }, |
| 438 | + ], |
| 439 | + }; |
| 440 | + userEvent.setup(); |
| 441 | + |
| 442 | + const screen = await render(<P data={jsonValue} />, { |
| 443 | + wrapper: ({ children }) => ( |
| 444 | + <TagCatalogProvider tagCatalog={tagCatalog}> |
| 445 | + {children} |
| 446 | + </TagCatalogProvider> |
| 447 | + ), |
| 448 | + }); |
| 449 | + |
| 450 | + const inlineFigure = screen.getByText("figure.unloaded"); |
| 451 | + expect(inlineFigure).toBeVisible(); |
| 452 | + |
| 453 | + await userEvent.hover(inlineFigure.element()); |
| 454 | + |
| 455 | + const tooltip = screen.getByRole("tooltip"); |
| 456 | + expect(tooltip).toBeVisible(); |
| 457 | + expect(tooltip).toHaveTextContent("This is a figure description."); |
| 458 | + }); |
304 | 459 | }); |
0 commit comments