|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent } from "@testing-library/react"; |
| 3 | + |
| 4 | +import { PayPalCreditOneTimePaymentButton } from "./PayPalCreditOneTimePaymentButton"; |
| 5 | +import { usePayPalCreditOneTimePaymentSession } from "../hooks/usePayPalCreditOneTimePaymentSession"; |
| 6 | +import { usePayPal } from "../hooks/usePayPal"; |
| 7 | + |
| 8 | +jest.mock("../hooks/usePayPalCreditOneTimePaymentSession", () => ({ |
| 9 | + usePayPalCreditOneTimePaymentSession: jest.fn(), |
| 10 | +})); |
| 11 | +jest.mock("../hooks/usePayPal", () => ({ |
| 12 | + usePayPal: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +describe("PayPalCreditOneTimePaymentButton", () => { |
| 16 | + const mockHandleClick = jest.fn(); |
| 17 | + const mockUsePayPalCreditOneTimePaymentSession = |
| 18 | + usePayPalCreditOneTimePaymentSession as jest.Mock; |
| 19 | + const mockUsePayPal = usePayPal as jest.Mock; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + mockUsePayPalCreditOneTimePaymentSession.mockReturnValue({ |
| 24 | + error: null, |
| 25 | + isPending: false, |
| 26 | + handleClick: mockHandleClick, |
| 27 | + }); |
| 28 | + mockUsePayPal.mockReturnValue({ |
| 29 | + eligiblePaymentMethods: null, |
| 30 | + isHydrated: true, |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should render paypal-credit-button when hydrated", () => { |
| 35 | + const { container } = render( |
| 36 | + <PayPalCreditOneTimePaymentButton |
| 37 | + onApprove={() => Promise.resolve()} |
| 38 | + orderId="123" |
| 39 | + presentationMode="auto" |
| 40 | + />, |
| 41 | + ); |
| 42 | + expect( |
| 43 | + container.querySelector("paypal-credit-button"), |
| 44 | + ).toBeInTheDocument(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should render a div when not hydrated", () => { |
| 48 | + mockUsePayPal.mockReturnValue({ |
| 49 | + eligiblePaymentMethods: null, |
| 50 | + isHydrated: false, |
| 51 | + }); |
| 52 | + const { container } = render( |
| 53 | + <PayPalCreditOneTimePaymentButton |
| 54 | + onApprove={() => Promise.resolve()} |
| 55 | + orderId="123" |
| 56 | + presentationMode="auto" |
| 57 | + />, |
| 58 | + ); |
| 59 | + expect( |
| 60 | + container.querySelector("paypal-credit-button"), |
| 61 | + ).not.toBeInTheDocument(); |
| 62 | + expect(container.querySelector("div")).toBeInTheDocument(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should call handleClick when button is clicked", () => { |
| 66 | + const { container } = render( |
| 67 | + <PayPalCreditOneTimePaymentButton |
| 68 | + onApprove={() => Promise.resolve()} |
| 69 | + orderId="123" |
| 70 | + presentationMode="auto" |
| 71 | + />, |
| 72 | + ); |
| 73 | + const button = container.querySelector("paypal-credit-button"); |
| 74 | + |
| 75 | + // @ts-expect-error button should be defined at this point, test will error if not |
| 76 | + fireEvent.click(button); |
| 77 | + |
| 78 | + expect(mockHandleClick).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should disable the button when disabled=true is given as a prop", () => { |
| 82 | + const { container } = render( |
| 83 | + <PayPalCreditOneTimePaymentButton |
| 84 | + onApprove={() => Promise.resolve()} |
| 85 | + orderId="123" |
| 86 | + presentationMode="auto" |
| 87 | + disabled={true} |
| 88 | + />, |
| 89 | + ); |
| 90 | + const button = container.querySelector("paypal-credit-button"); |
| 91 | + expect(button).toHaveAttribute("disabled"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should disable button when error is present", () => { |
| 95 | + jest.spyOn(console, "error").mockImplementation(); |
| 96 | + mockUsePayPalCreditOneTimePaymentSession.mockReturnValue({ |
| 97 | + error: new Error("Test error"), |
| 98 | + isPending: false, |
| 99 | + handleClick: mockHandleClick, |
| 100 | + }); |
| 101 | + const { container } = render( |
| 102 | + <PayPalCreditOneTimePaymentButton |
| 103 | + onApprove={() => Promise.resolve()} |
| 104 | + orderId="123" |
| 105 | + presentationMode="auto" |
| 106 | + />, |
| 107 | + ); |
| 108 | + const button = container.querySelector("paypal-credit-button"); |
| 109 | + expect(button).toHaveAttribute("disabled"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should not disable button when error is null", () => { |
| 113 | + mockUsePayPalCreditOneTimePaymentSession.mockReturnValue({ |
| 114 | + error: null, |
| 115 | + isPending: false, |
| 116 | + handleClick: mockHandleClick, |
| 117 | + }); |
| 118 | + const { container } = render( |
| 119 | + <PayPalCreditOneTimePaymentButton |
| 120 | + onApprove={() => Promise.resolve()} |
| 121 | + orderId="123" |
| 122 | + presentationMode="auto" |
| 123 | + />, |
| 124 | + ); |
| 125 | + const button = container.querySelector("paypal-credit-button"); |
| 126 | + expect(button).not.toHaveAttribute("disabled"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should return null when isPending is true", () => { |
| 130 | + mockUsePayPalCreditOneTimePaymentSession.mockReturnValue({ |
| 131 | + error: null, |
| 132 | + isPending: true, |
| 133 | + handleClick: mockHandleClick, |
| 134 | + }); |
| 135 | + const { container } = render( |
| 136 | + <PayPalCreditOneTimePaymentButton |
| 137 | + onApprove={() => Promise.resolve()} |
| 138 | + orderId="123" |
| 139 | + presentationMode="auto" |
| 140 | + />, |
| 141 | + ); |
| 142 | + expect( |
| 143 | + container.querySelector("paypal-credit-button"), |
| 144 | + ).not.toBeInTheDocument(); |
| 145 | + expect(container.firstChild).toBeNull(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should pass hook props to usePayPalCreditOneTimePaymentSession", () => { |
| 149 | + const hookProps = { |
| 150 | + clientToken: "test-token", |
| 151 | + amount: "10.00", |
| 152 | + currency: "USD", |
| 153 | + onApprove: () => Promise.resolve(), |
| 154 | + orderId: "123", |
| 155 | + presentationMode: "auto" as const, |
| 156 | + }; |
| 157 | + render(<PayPalCreditOneTimePaymentButton {...hookProps} />); |
| 158 | + expect(mockUsePayPalCreditOneTimePaymentSession).toHaveBeenCalledWith( |
| 159 | + hookProps, |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + describe("auto-population from eligibility context", () => { |
| 164 | + it("should auto-populate countryCode from eligibility context", () => { |
| 165 | + mockUsePayPal.mockReturnValue({ |
| 166 | + isHydrated: true, |
| 167 | + eligiblePaymentMethods: { |
| 168 | + isEligible: jest.fn().mockReturnValue(true), |
| 169 | + getDetails: jest.fn().mockReturnValue({ |
| 170 | + countryCode: "US", |
| 171 | + canBeVaulted: false, |
| 172 | + }), |
| 173 | + }, |
| 174 | + }); |
| 175 | + |
| 176 | + const { container } = render( |
| 177 | + <PayPalCreditOneTimePaymentButton |
| 178 | + onApprove={() => Promise.resolve()} |
| 179 | + orderId="123" |
| 180 | + presentationMode="auto" |
| 181 | + />, |
| 182 | + ); |
| 183 | + |
| 184 | + const button = container.querySelector("paypal-credit-button"); |
| 185 | + expect(button).toHaveAttribute("countrycode", "US"); |
| 186 | + }); |
| 187 | + |
| 188 | + it("should handle when eligibility was not fetched", () => { |
| 189 | + mockUsePayPal.mockReturnValue({ |
| 190 | + isHydrated: true, |
| 191 | + eligiblePaymentMethods: null, |
| 192 | + }); |
| 193 | + |
| 194 | + const { container } = render( |
| 195 | + <PayPalCreditOneTimePaymentButton |
| 196 | + onApprove={() => Promise.resolve()} |
| 197 | + orderId="123" |
| 198 | + presentationMode="auto" |
| 199 | + />, |
| 200 | + ); |
| 201 | + |
| 202 | + const button = container.querySelector("paypal-credit-button"); |
| 203 | + expect(button).not.toHaveAttribute("countrycode"); |
| 204 | + }); |
| 205 | + |
| 206 | + it("should handle when credit details are not available", () => { |
| 207 | + mockUsePayPal.mockReturnValue({ |
| 208 | + isHydrated: true, |
| 209 | + eligiblePaymentMethods: { |
| 210 | + isEligible: jest.fn().mockReturnValue(false), |
| 211 | + getDetails: jest.fn().mockReturnValue(undefined), |
| 212 | + }, |
| 213 | + }); |
| 214 | + |
| 215 | + const { container } = render( |
| 216 | + <PayPalCreditOneTimePaymentButton |
| 217 | + onApprove={() => Promise.resolve()} |
| 218 | + orderId="123" |
| 219 | + presentationMode="auto" |
| 220 | + />, |
| 221 | + ); |
| 222 | + |
| 223 | + const button = container.querySelector("paypal-credit-button"); |
| 224 | + expect(button).not.toHaveAttribute("countrycode"); |
| 225 | + }); |
| 226 | + }); |
| 227 | +}); |
0 commit comments