add tests for ActionButton

This commit is contained in:
Athou
2025-03-02 13:57:54 +01:00
parent 5992795579
commit 7a444e4861
2 changed files with 51 additions and 1 deletions

View File

@@ -0,0 +1,50 @@
import type { I18nContext } from "@lingui/react"
import { MantineProvider } from "@mantine/core"
import { fireEvent, render, screen, waitFor } from "@testing-library/react"
import { useActionButton } from "hooks/useActionButton"
import { describe, expect, it, vi } from "vitest"
import { ActionButton } from "./ActionButton"
vi.mock("@lingui/react", () => ({
useLingui: vi.fn().mockReturnValue({
_: msg => msg,
} as I18nContext),
}))
vi.mock("hooks/useActionButton")
// reduce delay for faster tests
vi.mock("app/constants", () => ({ Constants: { tooltip: { delay: 10 } } }))
const label = "Test Label"
const icon = "Test Icon"
describe("ActionButton", () => {
it("renders Button with label on desktop", () => {
vi.mocked(useActionButton).mockReturnValue({ mobile: false, spacing: 0 })
render(<ActionButton label={label} icon={icon} />, { wrapper: MantineProvider })
expect(screen.getByText(label)).toBeInTheDocument()
expect(screen.getByText(icon)).toBeInTheDocument()
})
it("renders ActionIcon with tooltip on mobile", async () => {
vi.mocked(useActionButton).mockReturnValue({ mobile: true, spacing: 0 })
render(<ActionButton label={label} icon={icon} />, { wrapper: MantineProvider })
expect(screen.queryByText(label)).not.toBeInTheDocument()
expect(screen.getByText(icon)).toBeInTheDocument()
fireEvent.mouseEnter(screen.getByRole("button"))
const tooltip = await waitFor(() => screen.getByRole("tooltip"))
expect(tooltip).toContainHTML(label)
})
it("calls onClick handler when clicked", () => {
vi.mocked(useActionButton).mockReturnValue({ mobile: false, spacing: 0 })
const clickListener = vi.fn()
render(<ActionButton label={label} icon={icon} onClick={clickListener} />, { wrapper: MantineProvider })
fireEvent.click(screen.getByRole("button"))
expect(clickListener).toHaveBeenCalled()
})
})

View File

@@ -7,8 +7,8 @@ import { useActionButton } from "hooks/useActionButton"
import { type MouseEventHandler, type ReactNode, forwardRef } from "react"
interface ActionButtonProps {
icon: ReactNode
className?: string
icon?: ReactNode
label?: string | MessageDescriptor
onClick?: MouseEventHandler
variant?: ActionIconVariant & ButtonVariant