diff --git a/src/connect/component.jsx b/src/connect/component.jsx index 51cf35e41c..5e8b7343ec 100644 --- a/src/connect/component.jsx +++ b/src/connect/component.jsx @@ -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 @@ -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 diff --git a/src/connect/component.test.js b/src/connect/component.test.js index e69de29bb2..6260583000 100644 --- a/src/connect/component.test.js +++ b/src/connect/component.test.js @@ -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); + }); +}); diff --git a/src/connect/interface.js b/src/connect/interface.js index 42705472cf..deaaa82fa2 100644 --- a/src/connect/interface.js +++ b/src/connect/interface.js @@ -6,5 +6,5 @@ import type { LazyExport } from "../types"; import { getConnectComponent, type ConnectComponent } from "./component"; export const Connect: LazyExport = async (merchantProps) => { - return await getConnectComponent(merchantProps); + return getConnectComponent(merchantProps); }; diff --git a/src/connect/interface.test.js b/src/connect/interface.test.js index e69de29bb2..50918157ae 100644 --- a/src/connect/interface.test.js +++ b/src/connect/interface.test.js @@ -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); + }); +}); diff --git a/src/interface/connect.test.js b/src/interface/connect.test.js deleted file mode 100644 index 5c3c07a094..0000000000 --- a/src/interface/connect.test.js +++ /dev/null @@ -1,74 +0,0 @@ -/* @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 { connect } from "./connect"; - -describe("connect: LazyExport AXOComponent", () => { - 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().mockResolvedValue({ metadata: { someData: "data" } }), - }; - }); - - // 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 () => { - const mockAxoMetadata = { someData: "data" }; - loadAxo.mockResolvedValue({ metadata: mockAxoMetadata }); - const mockProps = { someProp: "value" }; - const result = connect.__get__(); - - await result(mockProps); - - expect(getClientID).toHaveBeenCalled(); - expect(getClientMetadataID).toHaveBeenCalled(); - expect(getUserIDToken).toHaveBeenCalled(); - expect(loadAxo).toHaveBeenCalled(); - - expect(window.braintree.connect.create).toHaveBeenCalledWith({ - ...mockAxoMetadata, - ...mockProps, - clientID: "mock-client-id", - cmid: "mock-cmid", - userIdToken: "mock-uid", - }); - }); - - test("loadAxo failure is handled", async () => { - const mockProps = { someProp: "value" }; - const errorMessage = "Something went wrong"; - // eslint-disable-next-line no-import-assign - loadAxo = vi.fn().mockRejectedValue(errorMessage); - - const result = connect.__get__(); - const error = await result(mockProps); - - expect(error).toBeInstanceOf(Error); - expect(error.message).toEqual(errorMessage); - }); -});