UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

99 lines (79 loc) 2.62 kB
import * as React from "react"; import { cleanup, renderHook } from "@testing-library/react-hooks"; import { CellTapContext } from "@applicaster/zapp-react-native-ui-components/Contexts/CellTapContext"; import { ActionExecutorContext } from "@applicaster/zapp-react-native-utils/actionsExecutor/ActionExecutorContext"; import { waitFor } from "@testing-library/react-native"; const mockNavigator = { getPathname: () => "pathnameMock", push: jest.fn(), data: {}, }; jest.mock("@applicaster/zapp-react-native-utils/reactHooks/navigation", () => ({ useNavigation: () => mockNavigator, useRoute: () => ({ pathname: "pathnameMock" }), useProfilerLogging: () => { return jest.fn(); }, })); jest.mock("@applicaster/zapp-react-native-utils/analyticsUtils/", () => ({ useAnalytics: () => ({ sendOnClickEvent: jest.fn(), }), })); jest.mock("@applicaster/zapp-react-native-utils/reactHooks/screen", () => ({ useTargetScreenData: jest.fn(() => ({})), useCurrentScreenData: jest.fn(() => ({})), useScreenContext: jest.fn().mockReturnValue({ screen: {}, entry: {} }), })); jest.mock("@applicaster/zapp-react-native-ui-components/Contexts", () => ({ ...jest.requireActual( "@applicaster/zapp-react-native-ui-components/Contexts" ), ZappPipesEntryContext: { useZappPipesContext: jest.fn(() => [undefined, jest.fn()]), }, })); const { useCellClick } = require("../"); const mockProps = { item: {}, component: { rules: { component_cells_selectable: true, }, }, index: 0, }; const onCellTap = jest.fn(); const actionExecutor = { handleEntryActions: jest.fn(() => Promise.resolve()), }; const wrapper = ({ children }) => ( <CellTapContext.Provider value={onCellTap}> <ActionExecutorContext.Provider value={actionExecutor}> {children} </ActionExecutorContext.Provider> </CellTapContext.Provider> ); describe("useCellClick", () => { it("returns a function", () => { const { result } = renderHook(() => useCellClick(mockProps)); expect(typeof result.current).toBe("function"); cleanup(); }); it("runs default logic", () => { const { result } = renderHook(() => useCellClick(mockProps)); const useCellClickHook = result.current; useCellClickHook(); expect(onCellTap).not.toHaveBeenCalled(); cleanup(); }); it("runs onCellTap", async () => { const { result } = renderHook(() => useCellClick(mockProps), { wrapper }); const useCellClickHook = result.current; useCellClickHook(); await waitFor(() => { expect(onCellTap).toHaveBeenCalled(); }); cleanup(); }); });