mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
test(tag): add unit tests
This commit is contained in:
parent
907a4ed918
commit
ad74124206
3 changed files with 106 additions and 2 deletions
|
@ -8,6 +8,7 @@
|
|||
import TreeViewHierarchy from "./TreeView/TreeView.hierarchy.test.svelte";
|
||||
import RecursiveList from "./RecursiveList/RecursiveList.test.svelte";
|
||||
import RecursiveListHierarchy from "./RecursiveList/RecursiveList.hierarchy.test.svelte";
|
||||
import Tag from "./Tag/Tag.test.svelte";
|
||||
import { onMount } from "svelte";
|
||||
|
||||
const routes = [
|
||||
|
@ -51,6 +52,11 @@
|
|||
name: "TreeViewHierarchy",
|
||||
component: TreeViewHierarchy,
|
||||
},
|
||||
{
|
||||
path: "/tag",
|
||||
name: "Tag",
|
||||
component: Tag,
|
||||
},
|
||||
] as const;
|
||||
|
||||
let currentPath = window.location.pathname;
|
||||
|
|
|
@ -29,10 +29,23 @@
|
|||
|
||||
<Tag type="outline">outline</Tag>
|
||||
|
||||
<Tag filter on:click on:close>Filterable</Tag>
|
||||
<Tag
|
||||
filter
|
||||
on:click={() => {
|
||||
console.log("click");
|
||||
}}
|
||||
on:close={() => {
|
||||
console.log("close");
|
||||
}}>Filterable</Tag
|
||||
>
|
||||
|
||||
<Tag icon={Add}>Custom icon</Tag>
|
||||
|
||||
<Tag interactive>Text</Tag>
|
||||
<Tag
|
||||
interactive
|
||||
on:click={() => {
|
||||
console.log("click");
|
||||
}}>Text</Tag
|
||||
>
|
||||
|
||||
<Tag skeleton />
|
85
tests/Tag/Tag.test.ts
Normal file
85
tests/Tag/Tag.test.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
import { render, screen } from "@testing-library/svelte";
|
||||
import { user } from "../setup-tests";
|
||||
import Tag from "./Tag.test.svelte";
|
||||
|
||||
describe("Tag", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders all tag variants with correct styles", () => {
|
||||
render(Tag);
|
||||
|
||||
const basicTag = screen.getByText("IBM Cloud");
|
||||
expect(basicTag.parentElement).toHaveClass("my-class");
|
||||
expect(basicTag.parentElement).toHaveStyle({ margin: "1rem" });
|
||||
|
||||
[
|
||||
"red",
|
||||
"magenta",
|
||||
"purple",
|
||||
"blue",
|
||||
"cyan",
|
||||
"teal",
|
||||
"green",
|
||||
"gray",
|
||||
"cool-gray",
|
||||
"warm-gray",
|
||||
"high-contrast",
|
||||
"outline",
|
||||
].forEach((color) => {
|
||||
const tag = screen.getByText(color);
|
||||
expect(tag.parentElement).toHaveClass(`bx--tag--${color}`);
|
||||
});
|
||||
});
|
||||
|
||||
it("renders and handles filterable tag correctly", async () => {
|
||||
const consoleLog = vi.spyOn(console, "log");
|
||||
|
||||
render(Tag);
|
||||
|
||||
const filterableTag = screen.getByText("Filterable");
|
||||
expect(filterableTag).toHaveClass("bx--tag--filter");
|
||||
|
||||
const closeButton = filterableTag.querySelector("button")!;
|
||||
expect(closeButton).toHaveClass("bx--tag__close-icon");
|
||||
expect(closeButton).toHaveAttribute("title", "Clear filter");
|
||||
|
||||
await user.click(closeButton);
|
||||
expect(consoleLog).toHaveBeenCalledWith("close");
|
||||
expect(consoleLog).toHaveBeenCalledWith("click");
|
||||
});
|
||||
|
||||
it("renders custom icon tag correctly", () => {
|
||||
render(Tag);
|
||||
|
||||
const iconTag = screen.getByText("Custom icon");
|
||||
const iconContainer = iconTag.parentElement?.querySelector(
|
||||
".bx--tag__custom-icon",
|
||||
);
|
||||
expect(iconContainer).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders interactive tag as a button", () => {
|
||||
render(Tag);
|
||||
|
||||
const interactiveTag = screen.getByRole("button", { name: "Text" });
|
||||
expect(interactiveTag).toHaveClass("bx--tag--interactive");
|
||||
});
|
||||
|
||||
it("renders skeleton state", () => {
|
||||
render(Tag);
|
||||
|
||||
const skeleton = document.querySelector(".bx--skeleton");
|
||||
expect(skeleton).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("handles click events on interactive tag", async () => {
|
||||
const consoleLog = vi.spyOn(console, "log");
|
||||
render(Tag);
|
||||
|
||||
const interactiveTag = screen.getByRole("button", { name: "Text" });
|
||||
await user.click(interactiveTag);
|
||||
expect(consoleLog).toHaveBeenCalledWith("click");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue