TruncatedTitle.spec.js
37 lines
| 1 | import { render, screen } from "@testing-library/react"; |
| 2 | import TruncatedTitle from "../TruncatedTitle"; |
| 3 | |
| 4 | const DEFAULT_MAX = 60; |
| 5 | |
| 6 | describe("TruncatedTitle", () => { |
| 7 | it("renders nothing when title is empty / null / undefined", () => { |
| 8 | const { container: c1 } = render(<TruncatedTitle title="" />); |
| 9 | expect(c1.firstChild).toBeNull(); |
| 10 | |
| 11 | const { container: c2 } = render(<TruncatedTitle title={null} />); |
| 12 | expect(c2.firstChild).toBeNull(); |
| 13 | |
| 14 | const { container: c3 } = render(<TruncatedTitle title={undefined} />); |
| 15 | expect(c3.firstChild).toBeNull(); |
| 16 | }); |
| 17 | |
| 18 | it("renders the full title without an ellipsis trigger when at or under maxChars", () => { |
| 19 | const title = "x".repeat(DEFAULT_MAX); |
| 20 | const { container } = render(<TruncatedTitle title={title} />); |
| 21 | expect(container.textContent).toBe(title); |
| 22 | expect(container.textContent).not.toContain("…"); |
| 23 | }); |
| 24 | |
| 25 | it("renders truncated prefix and an ellipsis trigger when over maxChars", () => { |
| 26 | const title = "x".repeat(DEFAULT_MAX + 10); |
| 27 | const { container } = render(<TruncatedTitle title={title} />); |
| 28 | // Slices to maxChars - 1 chars; the rest is replaced by the … trigger. |
| 29 | const prefix = "x".repeat(DEFAULT_MAX - 1); |
| 30 | expect(container.textContent).toContain(prefix); |
| 31 | expect(container.textContent).toContain("…"); |
| 32 | // Should be shorter than the original (the trailing chars are dropped). |
| 33 | expect(container.textContent.length).toBeLessThan(title.length + 1); |
| 34 | }); |
| 35 | |
| 36 | }); |
| 37 |