test(truncate): add unit tests

This commit is contained in:
Eric Liu 2025-03-16 17:22:05 -07:00
commit d64465e774
4 changed files with 123 additions and 22 deletions

View file

@ -0,0 +1,11 @@
<script lang="ts">
import { Truncate } from "carbon-components-svelte";
export let clamp: "end" | "front" = "end";
export let text =
"This is a long text that should be truncated when it exceeds the available space";
</script>
<Truncate {clamp}>
{text}
</Truncate>

View file

@ -0,0 +1,90 @@
import { render, screen } from "@testing-library/svelte";
import Truncate from "./Truncate.test.svelte";
import TruncateAction from "./TruncateAction.test.svelte";
describe("Truncate", () => {
describe("component", () => {
test.each([
["end", "bx--text-truncate--end"],
["front", "bx--text-truncate--front"],
] as const)("should support %s clamp", (clamp, expectedClass) => {
render(Truncate, { props: { clamp } });
const element = screen.getByText(/This is a long text/);
expect(element).toHaveClass(expectedClass);
});
it("should render text content", () => {
const text = "Custom text content";
render(Truncate, { props: { text } });
expect(screen.getByText(text)).toBeInTheDocument();
});
it("should render as paragraph element", () => {
render(Truncate);
const element = screen.getByText(/This is a long text/);
expect(element.tagName).toBe("P");
});
it("should default to end clamp", () => {
render(Truncate);
const element = screen.getByText(/This is a long text/);
expect(element).toHaveClass("bx--text-truncate--end");
});
});
describe("action", () => {
test.each([
["end", "bx--text-truncate--end"],
["front", "bx--text-truncate--front"],
] as const)("should support %s clamp", (clamp, expectedClass) => {
render(TruncateAction, { props: { clamp } });
const element = screen.getByText(/This is a long text/);
expect(element).toHaveClass(expectedClass);
});
test.each([
["h1", "H1"],
["h2", "H2"],
["h3", "H3"],
["h4", "H4"],
["p", "P"],
["span", "SPAN"],
] as const)("should work with %s element", (elementType, expectedTag) => {
render(TruncateAction, { props: { element: elementType } });
const element = screen.getByText(/This is a long text/);
expect(element.tagName).toBe(expectedTag);
expect(element).toHaveClass("bx--text-truncate--end");
});
it("should preserve existing classes", () => {
render(TruncateAction, {
props: {
element: "p",
text: "Test",
},
});
const element = screen.getByText("Test");
element.classList.add("custom-class");
// Trigger update
element.dispatchEvent(new Event("update"));
expect(element).toHaveClass("custom-class");
expect(element).toHaveClass("bx--text-truncate--end");
});
it("should default to end clamp", () => {
render(TruncateAction);
const element = screen.getByText(/This is a long text/);
expect(element).toHaveClass("bx--text-truncate--end");
});
});
});

View file

@ -0,0 +1,22 @@
<script lang="ts">
import { truncate } from "carbon-components-svelte";
export let clamp: "end" | "front" = "end";
export let text =
"This is a long text that should be truncated when it exceeds the available space";
export let element: "h1" | "h2" | "h3" | "h4" | "p" | "span" = "p";
</script>
{#if element === "h1"}
<h1 use:truncate={{ clamp }}>{text}</h1>
{:else if element === "h2"}
<h2 use:truncate={{ clamp }}>{text}</h2>
{:else if element === "h3"}
<h3 use:truncate={{ clamp }}>{text}</h3>
{:else if element === "h4"}
<h4 use:truncate={{ clamp }}>{text}</h4>
{:else if element === "span"}
<span use:truncate={{ clamp }}>{text}</span>
{:else}
<p use:truncate={{ clamp }}>{text}</p>
{/if}