|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import { render, RenderResult } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { createElement } from "react"; |
| 5 | + |
| 6 | +import { BadgeButton, BadgeButtonProps } from "../BadgeButton"; |
| 7 | + |
| 8 | +describe("BadgeButton", () => { |
| 9 | + function renderBadgeButton(props: Partial<BadgeButtonProps>): RenderResult { |
| 10 | + return render(<BadgeButton {...props} />); |
| 11 | + } |
| 12 | + |
| 13 | + it("renders the structure correctly", () => { |
| 14 | + const badgeProps: BadgeButtonProps = { |
| 15 | + label: "Custom Label", |
| 16 | + onClick: jest.fn(), |
| 17 | + value: "0" |
| 18 | + }; |
| 19 | + |
| 20 | + const badge = renderBadgeButton(badgeProps); |
| 21 | + |
| 22 | + const button = badge.getByRole("button", { name: /custom label/i }); |
| 23 | + expect(button).toHaveClass("widget-badge-button btn btn-primary"); |
| 24 | + |
| 25 | + const label = badge.getByText("Custom Label"); |
| 26 | + expect(label).toHaveClass("widget-badge-button-text"); |
| 27 | + |
| 28 | + const badgeValue = badge.getByText("0"); |
| 29 | + expect(badgeValue).toHaveClass("badge"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("responds to click events", async () => { |
| 33 | + const user = userEvent.setup(); |
| 34 | + const onClickMock = jest.fn(); |
| 35 | + const badgeProps: BadgeButtonProps = { onClick: onClickMock }; |
| 36 | + const badge = renderBadgeButton(badgeProps); |
| 37 | + |
| 38 | + await user.click(badge.getByRole("button")); |
| 39 | + |
| 40 | + expect(onClickMock).toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("renders correctly with custom classes", () => { |
| 44 | + const badge = renderBadgeButton({ className: "btn-secondary" }); |
| 45 | + |
| 46 | + expect(badge.getByRole("button")).toHaveClass("btn btn-secondary"); |
| 47 | + }); |
| 48 | +}); |
0 commit comments