Skip to content

Commit

Permalink
refactor test for class component
Browse files Browse the repository at this point in the history
  • Loading branch information
mchoun committed Oct 16, 2024
1 parent 71c8127 commit 62afa53
Showing 1 changed file with 48 additions and 30 deletions.
78 changes: 48 additions & 30 deletions src/three-domain-secure/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,59 @@ import { getSDKToken } from "@paypal/sdk-client/src";

import { ValidationError } from "../lib";

Check failure on line 5 in src/three-domain-secure/component.test.js

View workflow job for this annotation

GitHub Actions / main

'ValidationError' is defined but never used

import { getThreeDomainSecure } from "./component";
import {
ThreeDomainSecureComponent,
type ThreeDomainSecureComponentInterface,

Check failure on line 9 in src/three-domain-secure/component.test.js

View workflow job for this annotation

GitHub Actions / main

'ThreeDomainSecureComponentInterface' is defined but never used
} from "./component";

vi.mock("@paypal/sdk-client/src", () => ({
getSDKToken: vi.fn(),
getLogger: vi.fn(() => ({
const defaultSdkConfig = {
sdkToken: "sdk-client-token",
};

const createThreeDomainSecureComponent = (
sdkConfig = defaultSdkConfig,
logger = {
info: vi.fn().mockReturnThis(),
warn: vi.fn().mockReturnThis(),
error: vi.fn().mockReturnThis(),
track: vi.fn().mockReturnThis(),
flush: vi.fn().mockReturnThis(),
})),
}));
vi.mock("../lib", () => ({
ValidationError: vi.fn(),
}));
describe("getThreeDomainSecure returns ThreeDomainSecureComponent", () => {
it("should throw an error if sdkToken is not present", () => {
// $FlowFixMe prop missing error
getSDKToken.mockReturnValue(undefined);
const ThreeDomainSecureComponent = getThreeDomainSecure();
expect(() => ThreeDomainSecureComponent()).toThrowError(ValidationError);
expect(ValidationError).toHaveBeenCalledWith(
`script data attribute sdk-client-token is required but was not passed`
);
metricCounter: vi.fn().mockReturnThis(),
}
) =>
new ThreeDomainSecureComponent({
sdkConfig,
logger,
});

describe("three domain secure component - isEligible method", () => {
test("should console log eligible", () => {
const consoleSpy = vi.spyOn(console, "log");
const threeDomainSecuretClient = createThreeDomainSecureComponent();
threeDomainSecuretClient.isEligible();
expect(consoleSpy).toHaveBeenCalledWith("eligible");
});
it("should return the ThreeDomainSecure component and log the correct message", async () => {
// eslint-disable-next-line no-empty-function
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
// $FlowFixMe prop missing error
getSDKToken.mockReturnValue("84ghb8984");
const ThreeDomainSecureComponent = getThreeDomainSecure();
expect(typeof ThreeDomainSecureComponent).toBe("function");
});

// Call the returned component and check the console log
await ThreeDomainSecureComponent();
expect(consoleSpy).toHaveBeenCalledWith("Three Domain Secure Called");
describe("three domain descure component - show method", () => {
test("should console log show", () => {
const consoleSpy = vi.spyOn(console, "log");
const threeDomainSecuretClient = createThreeDomainSecureComponent();
threeDomainSecuretClient.show();
expect(consoleSpy).toHaveBeenCalledWith("show");
});
});

consoleSpy.mockRestore();
describe("three domain secure component - initialization", () => {
test("should throw an error if sdkToken is not present", () => {
expect(() =>
createThreeDomainSecureComponent({
sdkConfig: {
...defaultSdkConfig,
sdkToken: "",
},
})
).toThrowError(
`script data attribute sdk-client-token is required but was not passed`
);
});
});

0 comments on commit 62afa53

Please sign in to comment.