Skip to content

Commit 32c7cb8

Browse files
committed
test(tabs): add tests for container type with icons and secondary labels
1 parent 780551f commit 32c7cb8

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts">
2+
import { Tab, TabContent, Tabs } from "carbon-components-svelte";
3+
import Calendar from "../../src/icons/Calendar.svelte";
4+
import Information from "../../src/icons/Information.svelte";
5+
import Settings from "../../src/icons/Settings.svelte";
6+
</script>
7+
8+
<Tabs type="container">
9+
<Tab label="Calendar" icon={Calendar} secondaryLabel="(12 events)" />
10+
<Tab label="Information" icon={Information} secondaryLabel="(3 new)" />
11+
<Tab label="Settings" icon={Settings} secondaryLabel="(2 pending)" disabled />
12+
<svelte:fragment slot="content">
13+
<TabContent>Calendar content</TabContent>
14+
<TabContent>Information content</TabContent>
15+
<TabContent>Settings content</TabContent>
16+
</svelte:fragment>
17+
</Tabs>

tests/Tabs/Tabs.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { user } from "../setup-tests";
77
import Tab from "./Tab.test.svelte";
88
import TabIcon from "./TabIcon.test.svelte";
99
import TabIconContainer from "./TabIconContainer.test.svelte";
10+
import TabIconSecondaryLabel from "./TabIconSecondaryLabel.test.svelte";
1011
import TabSecondaryLabel from "./TabSecondaryLabel.test.svelte";
1112
import Tabs from "./Tabs.test.svelte";
1213
import TabsDynamic from "./TabsDynamic.test.svelte";
@@ -521,6 +522,70 @@ describe("Container tabs with icon", () => {
521522
});
522523
});
523524

525+
describe("Container tabs with icons and secondary label", () => {
526+
it("should render container type with icon and secondary label on each tab", () => {
527+
const { container } = render(TabIconSecondaryLabel);
528+
529+
const tabsContainer = screen.getByRole("navigation");
530+
expect(tabsContainer).toHaveClass("bx--tabs--container");
531+
532+
expect(screen.getByRole("tab", { name: /Calendar/ })).toBeInTheDocument();
533+
expect(screen.getByText("(12 events)")).toBeInTheDocument();
534+
535+
expect(
536+
screen.getByRole("tab", { name: /Information/ }),
537+
).toBeInTheDocument();
538+
expect(screen.getByText("(3 new)")).toBeInTheDocument();
539+
540+
expect(screen.getByRole("tab", { name: /Settings/ })).toBeInTheDocument();
541+
expect(screen.getByText("(2 pending)")).toBeInTheDocument();
542+
543+
const iconWrappers = container.querySelectorAll(
544+
".bx--tabs__nav-item--icon",
545+
);
546+
expect(iconWrappers).toHaveLength(3);
547+
});
548+
549+
it("should have label wrapper and secondary label when tab has icon and secondaryLabel", () => {
550+
const { container } = render(TabIconSecondaryLabel);
551+
552+
const navItems = container.querySelectorAll(".bx--tabs__nav-item");
553+
const calendarTab = navItems[0];
554+
const labelWrapper = calendarTab?.querySelector(
555+
".bx--tabs__nav-item-label-wrapper",
556+
);
557+
const secondaryLabel = calendarTab?.querySelector(
558+
".bx--tabs__nav-item-secondary-label",
559+
);
560+
const iconWrapper = calendarTab?.querySelector(".bx--tabs__nav-item--icon");
561+
562+
expect(labelWrapper).toBeInTheDocument();
563+
expect(secondaryLabel).toBeInTheDocument();
564+
expect(secondaryLabel).toHaveTextContent("(12 events)");
565+
expect(iconWrapper).toBeInTheDocument();
566+
});
567+
568+
it("should be clickable and show content when tab has icon and secondary label", async () => {
569+
render(TabIconSecondaryLabel);
570+
571+
const informationTab = screen.getByRole("tab", { name: /Information/ });
572+
await user.click(informationTab);
573+
574+
expect(informationTab).toHaveAttribute("aria-selected", "true");
575+
expect(screen.getByText("Information content")).toBeVisible();
576+
});
577+
578+
it("should not select disabled tab with icon and secondary label", async () => {
579+
render(TabIconSecondaryLabel);
580+
581+
const settingsTab = screen.getByRole("tab", { name: /Settings/ });
582+
expect(settingsTab).toHaveAttribute("aria-disabled", "true");
583+
await user.click(settingsTab);
584+
585+
expect(settingsTab).toHaveAttribute("aria-selected", "false");
586+
});
587+
});
588+
524589
describe("Container tabs with secondary label", () => {
525590
it("should render container with tall class when any tab has secondary label", () => {
526591
render(TabSecondaryLabel);

0 commit comments

Comments
 (0)