mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-16 02:41:05 +00:00
test(truncate): add unit tests
This commit is contained in:
parent
19a6c65313
commit
d64465e774
4 changed files with 123 additions and 22 deletions
90
tests/Truncate/Truncate.test.ts
Normal file
90
tests/Truncate/Truncate.test.ts
Normal 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");
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue