Skip to content

Commit

Permalink
test: add unit tests for AspectRatio, Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym authored Dec 29, 2024
1 parent cee6763 commit f3a8d99
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 13 deletions.
22 changes: 18 additions & 4 deletions tests/Accordion/Accordion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,25 @@ describe("Accordion", () => {
expect(items).toHaveLength(4);

// First item is open.
expect(items[0]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item bx--accordion__item--active"));
expect(items[0]).toHaveAttribute(
"class",
expect.stringContaining(
"bx--accordion__item bx--accordion__item--active",
),
);

// All other items are collapsed.
expect(items[1]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item"));
expect(items[2]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item"));
expect(items[3]).toHaveAttribute("class", expect.stringContaining("bx--accordion__item"));
expect(items[1]).toHaveAttribute(
"class",
expect.not.stringContaining("bx--accordion__item--active"),
);
expect(items[2]).toHaveAttribute(
"class",
expect.not.stringContaining("bx--accordion__item--active"),
);
expect(items[3]).toHaveAttribute(
"class",
expect.not.stringContaining("bx--accordion__item--active"),
);
});
});
12 changes: 12 additions & 0 deletions tests/App.test.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<script lang="ts">
import { TreeView as TreeViewNav } from "carbon-components-svelte";
import AspectRatio from "./AspectRatio/AspectRatio.test.svelte";
import Accordion from "./Accordion/Accordion.test.svelte";
import AccordionProgrammatic from "./Accordion/Accordion.programmatic.test.svelte";
import AccordionDisabled from "./Accordion/Accordion.disabled.test.svelte";
import TreeView from "./TreeView/TreeView.test.svelte";
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 = [
{
path: "/aspect-ratio",
name: "AspectRatio",
component: AspectRatio,
},
{
path: "/accordion",
name: "Accordion",
Expand Down Expand Up @@ -45,6 +52,11 @@
name: "TreeViewHierarchy",
component: TreeViewHierarchy,
},
{
path: "/tag",
name: "Tag",
component: Tag,
},
] as const;
let currentPath = window.location.pathname;
Expand Down
7 changes: 0 additions & 7 deletions tests/AspectRatio.test.svelte

This file was deleted.

13 changes: 13 additions & 0 deletions tests/AspectRatio/AspectRatio.test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import { AspectRatio } from "carbon-components-svelte";
</script>

<AspectRatio>2x1</AspectRatio>
<AspectRatio ratio="2x3">2x3</AspectRatio>
<AspectRatio ratio="16x9">16x9</AspectRatio>
<AspectRatio ratio="4x3">4x3</AspectRatio>
<AspectRatio ratio="1x1">1x1</AspectRatio>
<AspectRatio ratio="3x4">3x4</AspectRatio>
<AspectRatio ratio="3x2">3x2</AspectRatio>
<AspectRatio ratio="9x16">9x16</AspectRatio>
<AspectRatio ratio="1x2">1x2</AspectRatio>
15 changes: 15 additions & 0 deletions tests/AspectRatio/AspectRatio.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { render, screen } from "@testing-library/svelte";
import AspectRatio from "./AspectRatio.test.svelte";

describe("AspectRatio", () => {
it("renders correctly", () => {
render(AspectRatio);

["2x1", "2x3", "16x9", "4x3", "1x1", "3x4", "3x2", "9x16", "1x2"].forEach(
(ratio) => {
const boundingElement = screen.getByText(ratio).parentElement;
expect(boundingElement).toHaveClass(`bx--aspect-ratio--${ratio}`);
},
);
});
});
17 changes: 15 additions & 2 deletions tests/Tag.test.svelte → tests/Tag/Tag.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 85 additions & 0 deletions tests/Tag/Tag.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit f3a8d99

Please sign in to comment.