Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quick-lizards-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@paypal/react-paypal-js": patch
"@paypal/paypal-js": patch
---

Refactor paypal provider test utils.
6 changes: 3 additions & 3 deletions packages/paypal-js/src/v6/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ describe("loadCoreSdkScript()", () => {
test("should return PayPal namespace with version property", async () => {
const result = await loadCoreSdkScript();
expect(result).toBeDefined();
expect(result.version).toBeDefined();
expect(result.version).toBe("6");
expect(typeof result.version).toBe("string");
expect(result?.version).toBeDefined();
expect(result?.version).toBe("6");
expect(typeof result?.version).toBe("string");
});

test("should error due to unvalid input", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ import { PayPalProvider } from "./PayPalProvider";
import { usePayPal } from "../hooks/usePayPal";
import { INSTANCE_LOADING_STATE } from "../types/PayPalProviderEnums";
import { isServer } from "../utils";
import { TEST_CLIENT_TOKEN, expectInitialState } from "./providerTestUtils";

import type { CreateInstanceOptions, PayPalContextState } from "../types";

const TEST_CLIENT_TOKEN = "test-client-token";

function expectInitialState(state: Partial<PayPalContextState>): void {
expect(state.loadingStatus).toBe(INSTANCE_LOADING_STATE.PENDING);
expect(state.sdkInstance).toBe(null);
expect(state.eligiblePaymentMethods).toBe(null);
expect(state.error).toBe(null);
}

jest.mock("@paypal/paypal-js/sdk-v6", () => ({
loadCoreSdkScript: jest.fn(),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
import { PayPalProvider } from "./PayPalProvider";
import { usePayPal } from "../hooks/usePayPal";
import { INSTANCE_LOADING_STATE } from "../types/PayPalProviderEnums";
import {
TEST_CLIENT_TOKEN,
TEST_ERROR_MESSAGE,
TEST_ELIGIBILITY_RESULT,
createMockSdkInstance,
createMockPayPalNamespace,
expectPendingState,
expectResolvedState,
expectRejectedState,
expectReloadingState,
withConsoleSpy,
} from "./providerTestUtils";

import type { CreateInstanceOptions, PayPalContextState } from "../types";

import type {
CreateInstanceOptions,
PayPalContextState,
PayPalV6Namespace,
SdkInstance,

Check warning on line 14 in packages/react-paypal-js/src/v6/components/PayPalProvider.test.tsx

View workflow job for this annotation

GitHub Actions / main

'SdkInstance' is defined but never used

Check warning on line 14 in packages/react-paypal-js/src/v6/components/PayPalProvider.test.tsx

View workflow job for this annotation

GitHub Actions / main

'SdkInstance' is defined but never used
} from "../types";

// Test constants
export const TEST_CLIENT_TOKEN = "test-client-token";
export const TEST_ERROR_MESSAGE = "test error";
export const TEST_ELIGIBILITY_RESULT = {
paypal: { eligible: true },
venmo: { eligible: false },
};

jest.mock("@paypal/paypal-js/sdk-v6", () => ({
loadCoreSdkScript: jest.fn(),
Expand Down Expand Up @@ -57,6 +58,67 @@
return { ...result, state };
}

// Mock factories
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function createMockSdkInstance() {
return {
findEligibleMethods: jest
.fn()
.mockResolvedValue(TEST_ELIGIBILITY_RESULT),
createPayPalOneTimePaymentSession: jest.fn(),
updateLocale: jest.fn(),
};
}

function createMockPayPalNamespace(): PayPalV6Namespace {
return {
createInstance: jest.fn().mockResolvedValue(createMockSdkInstance()),
version: "6.0.0",
};
}

// State assertion helpers
function expectPendingState(state: Partial<PayPalContextState>): void {
expect(state.loadingStatus).toBe(INSTANCE_LOADING_STATE.PENDING);
}

function expectResolvedState(state: Partial<PayPalContextState>): void {
expect(state.loadingStatus).toBe(INSTANCE_LOADING_STATE.RESOLVED);
expect(state.sdkInstance).toBeTruthy();
expect(state.error).toBe(null);
}

function expectRejectedState(
state: Partial<PayPalContextState>,
error?: Error,
): void {
expect(state.loadingStatus).toBe(INSTANCE_LOADING_STATE.REJECTED);
expect(state.sdkInstance).toBe(null);
if (error) {
expect(state.error).toEqual(error);
}
}
function expectReloadingState(state: Partial<PayPalContextState>): void {
// When props change, only loadingStatus is reset to PENDING
// Old instance and eligibility remain until new ones are loaded
expect(state.loadingStatus).toBe(INSTANCE_LOADING_STATE.PENDING);
}

// Console spy utilities
function withConsoleSpy(
method: "error" | "warn" | "log",
testFn: (spy: jest.SpyInstance) => void | Promise<void>,
): void | Promise<void> {
const spy = jest.spyOn(console, method).mockImplementation();
const result = testFn(spy);

if (result instanceof Promise) {
return result.finally(() => spy.mockRestore());
}

spy.mockRestore();
}

describe("PayPalProvider", () => {
beforeEach(() => {
document.head.innerHTML = "";
Expand Down
79 changes: 0 additions & 79 deletions packages/react-paypal-js/src/v6/components/providerTestUtils.ts

This file was deleted.

Loading