Skip to content

Commit

Permalink
add tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
siddy2181 committed Nov 9, 2023
1 parent b43bfac commit 97a6a06
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/connect/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
getClientID,
getClientMetadataID,
getUserIDToken,
getSessionID,
} from "@paypal/sdk-client/src";

// TODO: What's the expected structure/approach for this interface. It's not a zoid
Expand All @@ -22,6 +21,7 @@ export const getConnectComponent = async (merchantProps) => {
sdkVersion: "", // PPCP proposal: string instead of function
minified: false,
});

// FPTI: sdkversion, fraudnet info
return await window.braintree.connect.create({
...loadResult.metadata, // returns a localeURL for assets
Expand Down
74 changes: 74 additions & 0 deletions src/connect/component.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* @flow */

import {
getClientID,
getClientMetadataID,
getUserIDToken,
} from "@paypal/sdk-client/src";
import { loadAxo } from "@paypal/connect-loader-component";
import { describe, expect, test, vi } from "vitest";

import { getConnectComponent } from "./component";

describe("getConnectComponent: returns ConnectComponent", () => {
const mockAxoMetadata = { someData: "data" };
const mockProps = { someProp: "value" };
beforeEach(() => {
window.braintree = {
connect: {
create: vi.fn(),
},
};

vi.mock("@paypal/sdk-client/src", () => {
return {
getClientID: vi.fn(() => "mock-client-id"),
getClientMetadataID: vi.fn(() => "mock-cmid"),
getUserIDToken: vi.fn(() => "mock-uid"),
};
});

vi.mock("@paypal/connect-loader-component", () => {
return {
loadAxo: vi.fn(),
};
});

loadAxo.mockResolvedValue({ metadata: mockAxoMetadata });

// getClientID.mockReturnValue("mock-client-id");
// getClientMetadataID.mockReturnValue("mock-cmid");
// getUserIDToken.mockReturnValue("mock-uid");
});

test("loadAxo and window.braintree.connect.create are called with proper data", async () => {
await getConnectComponent(mockProps);

expect(getClientID).toHaveBeenCalled();
expect(getClientMetadataID).toHaveBeenCalled();
expect(getUserIDToken).toHaveBeenCalled();
expect(loadAxo).toHaveBeenCalled();

expect(window.braintree.connect.create).toHaveBeenCalledWith({
...mockAxoMetadata,
...mockProps,
ppcp: {
clientID: "mock-client-id",
clientMetadataID: "mock-cmid",
userIdToken: "mock-uid",
fraudnet: expect.any(Function),
},
});
});

test("loadAxo failure is handled", async () => {
const errorMessage = "Something went wrong";
// eslint-disable-next-line no-import-assign
loadAxo = vi.fn().mockRejectedValue(errorMessage);

const error = await getConnectComponent(mockProps);

expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual(errorMessage);
});
});
2 changes: 1 addition & 1 deletion src/connect/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import type { LazyExport } from "../types";
import { getConnectComponent, type ConnectComponent } from "./component";

export const Connect: LazyExport<ConnectComponent> = async (merchantProps) => {

Check failure on line 8 in src/connect/interface.js

View workflow job for this annotation

GitHub Actions / main

Async arrow function has no 'await' expression
return await getConnectComponent(merchantProps);
return getConnectComponent(merchantProps);
};
19 changes: 19 additions & 0 deletions src/connect/interface.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* @flow */

import { describe, expect, vi } from "vitest";

import { getConnectComponent } from "./component";
import { Connect } from "./interface";

describe("interface.js", () => {
vi.mock("./component", () => {
return {
getConnectComponent: vi.fn(),
};
});
it("should call getConnectComponent with merchant props", async () => {
const merchantProps = { props: "someProps" };
await Connect(merchantProps);
expect(getConnectComponent).toBeCalledWith(merchantProps);
});
});
74 changes: 0 additions & 74 deletions src/interface/connect.test.js

This file was deleted.

0 comments on commit 97a6a06

Please sign in to comment.