mirror of
https://github.com/gethomepage/homepage.git
synced 2026-02-08 08:50:52 +08:00
test: add core component coverage (tabs, toggles, bookmarks)
This commit is contained in:
32
src/components/tab.test.jsx
Normal file
32
src/components/tab.test.jsx
Normal file
@@ -0,0 +1,32 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { TabContext } from "utils/contexts/tab";
|
||||
|
||||
import Tab, { slugifyAndEncode } from "./tab";
|
||||
|
||||
describe("components/tab", () => {
|
||||
it("slugifyAndEncode lowercases and encodes spaces", () => {
|
||||
expect(slugifyAndEncode("My Tab")).toBe("my-tab");
|
||||
expect(slugifyAndEncode(undefined)).toBe("");
|
||||
});
|
||||
|
||||
it("marks the matching tab as selected and updates hash on click", () => {
|
||||
const setActiveTab = vi.fn();
|
||||
|
||||
render(
|
||||
<TabContext.Provider value={{ activeTab: "my-tab", setActiveTab }}>
|
||||
<Tab tab="My Tab" />
|
||||
</TabContext.Provider>,
|
||||
);
|
||||
|
||||
const btn = screen.getByRole("tab");
|
||||
expect(btn.getAttribute("aria-selected")).toBe("true");
|
||||
|
||||
fireEvent.click(btn);
|
||||
expect(setActiveTab).toHaveBeenCalledWith("my-tab");
|
||||
expect(window.location.hash).toBe("#my-tab");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user