Skip to content

Commit f3a8d99

Browse files
authored
test: add unit tests for AspectRatio, Tag
1 parent cee6763 commit f3a8d99

File tree

7 files changed

+158
-13
lines changed

7 files changed

+158
-13
lines changed

tests/Accordion/Accordion.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,25 @@ describe("Accordion", () => {
104104
expect(items).toHaveLength(4);
105105

106106
// First item is open.
107-
expect(items[0]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item bx--accordion__item--active"));
107+
expect(items[0]).toHaveAttribute(
108+
"class",
109+
expect.stringContaining(
110+
"bx--accordion__item bx--accordion__item--active",
111+
),
112+
);
108113

109114
// All other items are collapsed.
110-
expect(items[1]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item"));
111-
expect(items[2]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item"));
112-
expect(items[3]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item"));
115+
expect(items[1]).toHaveAttribute(
116+
"class",
117+
expect.not.stringContaining("bx--accordion__item--active"),
118+
);
119+
expect(items[2]).toHaveAttribute(
120+
"class",
121+
expect.not.stringContaining("bx--accordion__item--active"),
122+
);
123+
expect(items[3]).toHaveAttribute(
124+
"class",
125+
expect.not.stringContaining("bx--accordion__item--active"),
126+
);
113127
});
114128
});

tests/App.test.svelte

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
<script lang="ts">
22
import { TreeView as TreeViewNav } from "carbon-components-svelte";
3+
import AspectRatio from "./AspectRatio/AspectRatio.test.svelte";
34
import Accordion from "./Accordion/Accordion.test.svelte";
45
import AccordionProgrammatic from "./Accordion/Accordion.programmatic.test.svelte";
56
import AccordionDisabled from "./Accordion/Accordion.disabled.test.svelte";
67
import TreeView from "./TreeView/TreeView.test.svelte";
78
import TreeViewHierarchy from "./TreeView/TreeView.hierarchy.test.svelte";
89
import RecursiveList from "./RecursiveList/RecursiveList.test.svelte";
910
import RecursiveListHierarchy from "./RecursiveList/RecursiveList.hierarchy.test.svelte";
11+
import Tag from "./Tag/Tag.test.svelte";
1012
import { onMount } from "svelte";
1113
1214
const routes = [
15+
{
16+
path: "/aspect-ratio",
17+
name: "AspectRatio",
18+
component: AspectRatio,
19+
},
1320
{
1421
path: "/accordion",
1522
name: "Accordion",
@@ -45,6 +52,11 @@
4552
name: "TreeViewHierarchy",
4653
component: TreeViewHierarchy,
4754
},
55+
{
56+
path: "/tag",
57+
name: "Tag",
58+
component: Tag,
59+
},
4860
] as const;
4961
5062
let currentPath = window.location.pathname;

tests/AspectRatio.test.svelte

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
import { AspectRatio } from "carbon-components-svelte";
3+
</script>
4+
5+
<AspectRatio>2x1</AspectRatio>
6+
<AspectRatio ratio="2x3">2x3</AspectRatio>
7+
<AspectRatio ratio="16x9">16x9</AspectRatio>
8+
<AspectRatio ratio="4x3">4x3</AspectRatio>
9+
<AspectRatio ratio="1x1">1x1</AspectRatio>
10+
<AspectRatio ratio="3x4">3x4</AspectRatio>
11+
<AspectRatio ratio="3x2">3x2</AspectRatio>
12+
<AspectRatio ratio="9x16">9x16</AspectRatio>
13+
<AspectRatio ratio="1x2">1x2</AspectRatio>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { render, screen } from "@testing-library/svelte";
2+
import AspectRatio from "./AspectRatio.test.svelte";
3+
4+
describe("AspectRatio", () => {
5+
it("renders correctly", () => {
6+
render(AspectRatio);
7+
8+
["2x1", "2x3", "16x9", "4x3", "1x1", "3x4", "3x2", "9x16", "1x2"].forEach(
9+
(ratio) => {
10+
const boundingElement = screen.getByText(ratio).parentElement;
11+
expect(boundingElement).toHaveClass(`bx--aspect-ratio--${ratio}`);
12+
},
13+
);
14+
});
15+
});

tests/Tag.test.svelte renamed to tests/Tag/Tag.test.svelte

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,23 @@
2929

3030
<Tag type="outline">outline</Tag>
3131

32-
<Tag filter on:click on:close>Filterable</Tag>
32+
<Tag
33+
filter
34+
on:click={() => {
35+
console.log("click");
36+
}}
37+
on:close={() => {
38+
console.log("close");
39+
}}>Filterable</Tag
40+
>
3341

3442
<Tag icon={Add}>Custom icon</Tag>
3543

36-
<Tag interactive>Text</Tag>
44+
<Tag
45+
interactive
46+
on:click={() => {
47+
console.log("click");
48+
}}>Text</Tag
49+
>
3750

3851
<Tag skeleton />

tests/Tag/Tag.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { render, screen } from "@testing-library/svelte";
2+
import { user } from "../setup-tests";
3+
import Tag from "./Tag.test.svelte";
4+
5+
describe("Tag", () => {
6+
afterEach(() => {
7+
vi.clearAllMocks();
8+
});
9+
10+
it("renders all tag variants with correct styles", () => {
11+
render(Tag);
12+
13+
const basicTag = screen.getByText("IBM Cloud");
14+
expect(basicTag.parentElement).toHaveClass("my-class");
15+
expect(basicTag.parentElement).toHaveStyle({ margin: "1rem" });
16+
17+
[
18+
"red",
19+
"magenta",
20+
"purple",
21+
"blue",
22+
"cyan",
23+
"teal",
24+
"green",
25+
"gray",
26+
"cool-gray",
27+
"warm-gray",
28+
"high-contrast",
29+
"outline",
30+
].forEach((color) => {
31+
const tag = screen.getByText(color);
32+
expect(tag.parentElement).toHaveClass(`bx--tag--${color}`);
33+
});
34+
});
35+
36+
it("renders and handles filterable tag correctly", async () => {
37+
const consoleLog = vi.spyOn(console, "log");
38+
39+
render(Tag);
40+
41+
const filterableTag = screen.getByText("Filterable");
42+
expect(filterableTag).toHaveClass("bx--tag--filter");
43+
44+
const closeButton = filterableTag.querySelector("button")!;
45+
expect(closeButton).toHaveClass("bx--tag__close-icon");
46+
expect(closeButton).toHaveAttribute("title", "Clear filter");
47+
48+
await user.click(closeButton);
49+
expect(consoleLog).toHaveBeenCalledWith("close");
50+
expect(consoleLog).toHaveBeenCalledWith("click");
51+
});
52+
53+
it("renders custom icon tag correctly", () => {
54+
render(Tag);
55+
56+
const iconTag = screen.getByText("Custom icon");
57+
const iconContainer = iconTag.parentElement?.querySelector(
58+
".bx--tag__custom-icon",
59+
);
60+
expect(iconContainer).toBeInTheDocument();
61+
});
62+
63+
it("renders interactive tag as a button", () => {
64+
render(Tag);
65+
66+
const interactiveTag = screen.getByRole("button", { name: "Text" });
67+
expect(interactiveTag).toHaveClass("bx--tag--interactive");
68+
});
69+
70+
it("renders skeleton state", () => {
71+
render(Tag);
72+
73+
const skeleton = document.querySelector(".bx--skeleton");
74+
expect(skeleton).toBeInTheDocument();
75+
});
76+
77+
it("handles click events on interactive tag", async () => {
78+
const consoleLog = vi.spyOn(console, "log");
79+
render(Tag);
80+
81+
const interactiveTag = screen.getByRole("button", { name: "Text" });
82+
await user.click(interactiveTag);
83+
expect(consoleLog).toHaveBeenCalledWith("click");
84+
});
85+
});

0 commit comments

Comments
 (0)