|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent } from "@testing-library/react-native"; |
| 3 | + |
| 4 | +import { FloatingActionButton } from "~/components/common/FloatingActionButton"; |
| 5 | +import type { FABAction } from "~/components/common/FloatingActionButton"; |
| 6 | + |
| 7 | +describe("FloatingActionButton", () => { |
| 8 | + it("renders with default props", () => { |
| 9 | + const { getByTestId } = render(<FloatingActionButton />); |
| 10 | + expect(getByTestId("fab")).toBeTruthy(); |
| 11 | + expect(getByTestId("fab-button")).toBeTruthy(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("calls onPress in simple mode", () => { |
| 15 | + const onPress = jest.fn(); |
| 16 | + const { getByTestId } = render(<FloatingActionButton onPress={onPress} />); |
| 17 | + fireEvent.press(getByTestId("fab-button")); |
| 18 | + expect(onPress).toHaveBeenCalledTimes(1); |
| 19 | + }); |
| 20 | + |
| 21 | + it("expands to show actions when pressed with actions", () => { |
| 22 | + const actions: FABAction[] = [ |
| 23 | + { id: "a1", label: "Action 1", onPress: jest.fn() }, |
| 24 | + { id: "a2", label: "Action 2", onPress: jest.fn() }, |
| 25 | + ]; |
| 26 | + const { getByTestId, queryByTestId } = render( |
| 27 | + <FloatingActionButton actions={actions} /> |
| 28 | + ); |
| 29 | + |
| 30 | + // Actions not visible initially |
| 31 | + expect(queryByTestId("fab-action-a1")).toBeNull(); |
| 32 | + |
| 33 | + // Press to expand |
| 34 | + fireEvent.press(getByTestId("fab-button")); |
| 35 | + expect(getByTestId("fab-action-a1")).toBeTruthy(); |
| 36 | + expect(getByTestId("fab-action-a2")).toBeTruthy(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("calls action onPress and collapses", () => { |
| 40 | + const actionFn = jest.fn(); |
| 41 | + const actions: FABAction[] = [ |
| 42 | + { id: "a1", label: "Action 1", onPress: actionFn }, |
| 43 | + ]; |
| 44 | + const { getByTestId, queryByTestId } = render( |
| 45 | + <FloatingActionButton actions={actions} /> |
| 46 | + ); |
| 47 | + |
| 48 | + fireEvent.press(getByTestId("fab-button")); |
| 49 | + fireEvent.press(getByTestId("fab-action-a1")); |
| 50 | + |
| 51 | + expect(actionFn).toHaveBeenCalledTimes(1); |
| 52 | + // Should collapse after action press |
| 53 | + expect(queryByTestId("fab-action-a1")).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("has correct accessibility labels", () => { |
| 57 | + const { getByTestId } = render(<FloatingActionButton />); |
| 58 | + expect(getByTestId("fab-button").props.accessibilityRole).toBe("button"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("uses custom testID", () => { |
| 62 | + const { getByTestId } = render(<FloatingActionButton testID="my-fab" />); |
| 63 | + expect(getByTestId("my-fab")).toBeTruthy(); |
| 64 | + }); |
| 65 | +}); |
0 commit comments