Files
Athou_commafeed/commafeed-client/src/components/ActionButton.test.tsx

54 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-03-02 13:57:54 +01:00
import type { I18nContext } from "@lingui/react"
import { MantineProvider } from "@mantine/core"
import { fireEvent, render, screen, waitFor } from "@testing-library/react"
import { describe, expect, it, vi } from "vitest"
2025-06-27 16:29:31 +02:00
import { useActionButton } from "@/hooks/useActionButton"
2025-03-02 13:57:54 +01:00
import { ActionButton } from "./ActionButton"
2025-03-02 19:18:51 +01:00
vi.mock(import("@lingui/react"), () => ({
2025-03-02 13:57:54 +01:00
useLingui: vi.fn().mockReturnValue({
_: msg => msg,
} as I18nContext),
}))
2025-06-27 16:29:31 +02:00
vi.mock(import("@/hooks/useActionButton"))
2025-03-02 13:57:54 +01:00
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 })
2025-06-27 16:29:31 +02:00
render(<ActionButton label={label} icon={icon} />, {
wrapper: MantineProvider,
})
2025-03-02 13:57:54 +01:00
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 })
2025-06-27 16:29:31 +02:00
render(<ActionButton label={label} icon={icon} />, {
wrapper: MantineProvider,
})
2025-03-02 13:57:54 +01:00
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()
2025-06-27 16:29:31 +02:00
render(<ActionButton label={label} icon={icon} onClick={clickListener} />, {
wrapper: MantineProvider,
})
2025-03-02 13:57:54 +01:00
fireEvent.click(screen.getByRole("button"))
expect(clickListener).toHaveBeenCalled()
})
})