Skip to content

Commit

Permalink
Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
9zees committed Oct 31, 2024
1 parent 60edb53 commit 30aae10
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/zoid/card-fields/component.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { describe, expect, vi } from "vitest";
import { getCardFieldsComponent } from "./component";
import { create } from "@krakenjs/zoid/src";
import { getRefinedFundingEligibility } from "@paypal/funding-components/src";
import { isPayPalDomain } from "@paypal/sdk-client/src";

vi.mock("@krakenjs/zoid/src", () => ({
create: vi.fn(() => ({
render: vi.fn(),
})),
}));

vi.mock("./component", () => ({
// Can try to mock this to return the Zoid Component
// Right now doing this is returning undefined
// Do we need to refactor the original code to all the configuration can be reused here?
getCardFieldsComponent: vi.fn(),
}));

vi.mock("@paypal/funding-components/src", () => ({
getRefinedFundingEligibility: vi.fn().mockReturnValue({
card: {
eligible: true,
},
}),
}));

vi.mock("@paypal/sdk-client/src", () => ({
isPayPalDomain: vi.fn(() => true),
}));

afterEach(() => {
vi.restoreAllMocks();
});

describe("createSubscription tests", () => {
it("should throw an error when using Card Fields subscriptions without the SDK token.", async () => {
expect(() => {
const CardFields = getCardFieldsComponent();

// CardFields keep returning undefined.
console.log("==>> CardFields: ", CardFields);

// Create CardFields with only createSubscription but no sdkToken
CardFields({
createSubscription: () => {
console.log("pass this callback");
},
// sdkToken: <Not being passed>
}).render("#doesntmatterRenderIsMocked");
}).toThrowError("SDK Token must be passed in for createSubscription");
});

/*
// NOTE: We can comment-out the rest of these UTs once we make the one above works.
it("should not throw an error when using Card Fields with subscriptions and with SDK token provided.", async () => {
expect(() => {
const CardFields = getCardFieldsComponent();
// CardFields keep returning undefined
console.log("==>> CardFields: ", CardFields);
// Create CardFields with createSubscription and with sdkToken
CardFields({
createSubscription: () => {
console.log("pass this callback");
},
sdkToken: "my-token",
}).render("#doesntmatterRenderIsMocked");
}).not.toThrowError("SDK Token must be passed in for createSubscription");
});
it("should not throw an error when using Card Fields without subscriptions and without the SDK token.", async () => {
expect(() => {
const CardFields = getCardFieldsComponent();
// CardFields keep returning undefined
console.log("==>> CardFields: ", CardFields);
// Create CardFields with createOrder and no sdkToken
CardFields({
createOrder: () => {
console.log("pass this callback");
},
// sdkToken: <Not being passed>
}).render("#doesntmatterRenderIsMocked");
}).toThrowError("SDK Token must be passed in for createSubscription");
});
it("should return CardFields when createSubscription is passed and isPayPalDomain() returns true", async () => {
// isPayPalDomain() is already mocked to return true
expect(() => {
const CardFields = getCardFieldsComponent();
// CardFields keep returning undefined
console.log("==>> CardFields: ", CardFields);
// Create CardFields with createSubscription and sdkToken
const instance = CardFields({
createSubscription: () => {
console.log("pass this callback");
},
sdkToken: "my-token",
})
// Check that zoid.create(...) gets called with createSubscription
});
});
*/
});

0 comments on commit 30aae10

Please sign in to comment.