Skip to content

Commit

Permalink
create new types for createOrder and creatVaultToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Hai-Yen Nguyen committed Jan 15, 2025
1 parent 4595036 commit 5748656
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/paypal-js/types/components/card-fields.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,31 @@ export interface PayPalCardFieldsIndividualField {
close: () => Promise<void>;
}

export interface PayPalCardFieldsComponentOptions {
createOrder: () => Promise<string>;
export interface PayPalCardFieldsComponentBasics {
onApprove: (data: CardFieldsOnApproveData) => void;
onError: (err: Record<string, unknown>) => void;
createVaultSetupToken?: () => Promise<string>;
inputEvents?: PayPalCardFieldsInputEvents;
style?: Record<string, PayPalCardFieldsStyleOptions>;
}

export interface PayPalCardFieldsComponent {
export interface PayPalCardFieldsComponentCreateOrder
extends PayPalCardFieldsComponentBasics {
createOrder: () => Promise<string>;
createVaultSetupToken?: never;
}

export interface PayPalCardFieldsComponentCreateVaultSetupToken
extends PayPalCardFieldsComponentBasics {
createOrder?: never;
createVaultSetupToken: () => Promise<string>;
}

export type PayPalCardFieldsComponentOptions =
| PayPalCardFieldsComponentCreateOrder
| PayPalCardFieldsComponentCreateVaultSetupToken;

export interface PayPalCardFieldsComponent
extends PayPalCardFieldsComponentOptions {
getState: () => Promise<PayPalCardFieldsStateObject>;
isEligible: () => boolean;
submit: () => Promise<void>;
Expand Down
48 changes: 48 additions & 0 deletions packages/paypal-js/types/tests/card-fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, vi, describe, expect } from "vitest";

import { PayPalCardFieldsComponentOptions } from "../components/card-fields";

describe("testing", () => {
test("only creatOrder", () => {
const createOrderCallback: PayPalCardFieldsComponentOptions = {
createOrder: vi.fn(),
onApprove: vi.fn(),
onError: vi.fn(),
};

expect(createOrderCallback.createVaultSetupToken).toBeUndefined();
});

test("only createVaultSetupToken", () => {
const createVaultSetupTokenCallback: PayPalCardFieldsComponentOptions =
{
createVaultSetupToken: vi.fn(),
onApprove: vi.fn(),
onError: vi.fn(),
};

expect(createVaultSetupTokenCallback.createOrder).toBeUndefined();
});

test.skip("Can't have both", () => {
// @ts-expect-error : should throw error if both createOrder and createVaultSetuptoken called
const callback: PayPalCardFieldsComponentOptions = {
createOrder: vi.fn(),
createVaultSetupToken: vi.fn(),
onApprove: vi.fn(),
onError: vi.fn(),
};

expect(callback).toThrowError();
});

test.skip("Can't have both", () => {
// @ts-expect-error: should throw error if neither createOrder or createVaultSetuptoken called
const callback: PayPalCardFieldsComponentOptions = {
onApprove: vi.fn(),
onError: vi.fn(),
};

expect(callback).toThrowError();
});
});

0 comments on commit 5748656

Please sign in to comment.