|
| 1 | +import React from "react" |
| 2 | +import { render, fireEvent } from "@testing-library/react-native" |
| 3 | + |
| 4 | +import { BackupNudgeModal } from "@app/components/backup-nudge-modal" |
| 5 | + |
| 6 | +const mockNavigate = jest.fn() |
| 7 | + |
| 8 | +jest.mock("@react-navigation/native", () => ({ |
| 9 | + ...jest.requireActual("@react-navigation/native"), |
| 10 | + useNavigation: () => ({ navigate: mockNavigate }), |
| 11 | +})) |
| 12 | + |
| 13 | +jest.mock("@rn-vui/themed", () => { |
| 14 | + const colors = { |
| 15 | + grey0: "#ccc", |
| 16 | + grey2: "#999", |
| 17 | + grey5: "#f5f5f5", |
| 18 | + primary: "#fc5805", |
| 19 | + black: "#000", |
| 20 | + white: "#fff", |
| 21 | + } |
| 22 | + return { |
| 23 | + makeStyles: |
| 24 | + (fn: (...args: unknown[]) => Record<string, object>) => (props?: unknown) => |
| 25 | + fn({ colors }, props ?? {}), |
| 26 | + Text: ({ children, ...props }: { children: React.ReactNode }) => |
| 27 | + React.createElement("Text", props, children), |
| 28 | + useTheme: () => ({ theme: { colors, mode: "light" } }), |
| 29 | + } |
| 30 | +}) |
| 31 | + |
| 32 | +const mockGaloyIcon = jest.fn<null, [Record<string, unknown>]>(() => null) |
| 33 | + |
| 34 | +jest.mock("@app/components/atomic/galoy-icon", () => ({ |
| 35 | + GaloyIcon: (props: Record<string, unknown>) => { |
| 36 | + mockGaloyIcon(props) |
| 37 | + return null |
| 38 | + }, |
| 39 | +})) |
| 40 | + |
| 41 | +jest.mock("@app/components/atomic/galoy-icon-button", () => ({ |
| 42 | + GaloyIconButton: ({ onPress }: { onPress: () => void }) => |
| 43 | + React.createElement("Pressable", { onPress, testID: "close-button" }), |
| 44 | +})) |
| 45 | + |
| 46 | +jest.mock("@app/components/atomic/galoy-primary-button", () => ({ |
| 47 | + GaloyPrimaryButton: ({ onPress, title }: { onPress: () => void; title: string }) => |
| 48 | + React.createElement( |
| 49 | + "Pressable", |
| 50 | + { onPress, testID: "secure-button" }, |
| 51 | + React.createElement("Text", {}, title), |
| 52 | + ), |
| 53 | +})) |
| 54 | + |
| 55 | +jest.mock("@app/components/atomic/galoy-secondary-button", () => ({ |
| 56 | + GaloySecondaryButton: () => null, |
| 57 | +})) |
| 58 | + |
| 59 | +jest.mock("react-native-modal", () => { |
| 60 | + const MockModal = ({ |
| 61 | + children, |
| 62 | + isVisible, |
| 63 | + }: { |
| 64 | + children: React.ReactNode |
| 65 | + isVisible: boolean |
| 66 | + }) => (isVisible ? React.createElement("View", { testID: "modal" }, children) : null) |
| 67 | + MockModal.displayName = "MockModal" |
| 68 | + return MockModal |
| 69 | +}) |
| 70 | + |
| 71 | +jest.mock("@app/utils/testProps", () => ({ |
| 72 | + testProps: (id: string) => ({ testID: id }), |
| 73 | +})) |
| 74 | + |
| 75 | +jest.mock("@app/i18n/i18n-react", () => ({ |
| 76 | + useI18nContext: () => ({ |
| 77 | + LL: { |
| 78 | + BackupNudge: { |
| 79 | + modalTitle: () => "Secure your funds", |
| 80 | + modalDescription: () => |
| 81 | + "We highly recommend you backup your wallet to prevent a complete loss of funds in case you lose this device.", |
| 82 | + secureMe: () => "Secure wallet", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }), |
| 86 | +})) |
| 87 | + |
| 88 | +describe("BackupNudgeModal", () => { |
| 89 | + beforeEach(() => { |
| 90 | + jest.clearAllMocks() |
| 91 | + }) |
| 92 | + |
| 93 | + it("renders when visible", () => { |
| 94 | + const { getByTestId } = render( |
| 95 | + <BackupNudgeModal isVisible={true} onClose={jest.fn()} />, |
| 96 | + ) |
| 97 | + |
| 98 | + expect(getByTestId("modal")).toBeTruthy() |
| 99 | + }) |
| 100 | + |
| 101 | + it("does not render when not visible", () => { |
| 102 | + const { queryByTestId } = render( |
| 103 | + <BackupNudgeModal isVisible={false} onClose={jest.fn()} />, |
| 104 | + ) |
| 105 | + |
| 106 | + expect(queryByTestId("modal")).toBeNull() |
| 107 | + }) |
| 108 | + |
| 109 | + it("renders title and description", () => { |
| 110 | + const { getByText } = render( |
| 111 | + <BackupNudgeModal isVisible={true} onClose={jest.fn()} />, |
| 112 | + ) |
| 113 | + |
| 114 | + expect(getByText("Secure your funds")).toBeTruthy() |
| 115 | + expect( |
| 116 | + getByText( |
| 117 | + "We highly recommend you backup your wallet to prevent a complete loss of funds in case you lose this device.", |
| 118 | + ), |
| 119 | + ).toBeTruthy() |
| 120 | + }) |
| 121 | + |
| 122 | + it("renders secure wallet button", () => { |
| 123 | + const { getByText } = render( |
| 124 | + <BackupNudgeModal isVisible={true} onClose={jest.fn()} />, |
| 125 | + ) |
| 126 | + |
| 127 | + expect(getByText("Secure wallet")).toBeTruthy() |
| 128 | + }) |
| 129 | + |
| 130 | + it("navigates to backup method screen and closes on button press", () => { |
| 131 | + const onClose = jest.fn() |
| 132 | + const { getByTestId } = render( |
| 133 | + <BackupNudgeModal isVisible={true} onClose={onClose} />, |
| 134 | + ) |
| 135 | + |
| 136 | + fireEvent.press(getByTestId("secure-button")) |
| 137 | + |
| 138 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 139 | + expect(mockNavigate).toHaveBeenCalledWith("sparkBackupMethodScreen") |
| 140 | + }) |
| 141 | + |
| 142 | + it("uses primary color for warning icon", () => { |
| 143 | + render(<BackupNudgeModal isVisible={true} onClose={jest.fn()} />) |
| 144 | + |
| 145 | + expect(mockGaloyIcon).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ |
| 147 | + name: "warning", |
| 148 | + size: 52, |
| 149 | + color: "#fc5805", |
| 150 | + }), |
| 151 | + ) |
| 152 | + }) |
| 153 | +}) |
0 commit comments