Skip to content

Commit 221b120

Browse files
committed
remove automatic:true flag
tests, make show async
1 parent 5073359 commit 221b120

File tree

5 files changed

+113
-88
lines changed

5 files changed

+113
-88
lines changed

Diff for: __sdk__.js

-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ module.exports = {
9494
entry: "./src/shopper-insights/interface",
9595
},
9696
"three-domain-secure": {
97-
globals,
98-
automatic: true,
9997
entry: "./src/three-domain-secure/interface",
10098
},
10199
};

Diff for: src/three-domain-secure/api.test.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { request } from "@krakenjs/belter/src";
55
import { callRestAPI } from "../lib";
66
import { HEADERS } from "../constants/api";
77

8-
import { RestClient, GraphQLClient, callGraphQLAPI, HTTPClient } from "./api";
8+
import { RestClient, callGraphQLAPI, HTTPClient } from "./api";
99

1010
vi.mock("@krakenjs/belter/src", async () => {
1111
return {
@@ -28,7 +28,7 @@ vi.mock("../lib", () => ({
2828

2929
describe("API", () => {
3030
const accessToken = "access_token";
31-
const baseURL = "http://localhost.paypal.com:8080";
31+
const baseURL = "http://test.paypal.com:port";
3232

3333
afterEach(() => {
3434
vi.clearAllMocks();
@@ -64,27 +64,6 @@ describe("API", () => {
6464
});
6565
});
6666

67-
describe("GraphQLClient", () => {
68-
const query = { test: "data" };
69-
const data = { query };
70-
const headers = { "Content-Type": "application/json" };
71-
72-
it.skip("should make a GraphQL API call with correct params", () => {
73-
vi.spyOn({ callGraphQLAPI }, "callGraphQLAPI").mockResolvedValue({
74-
data: { test: "data" },
75-
});
76-
const client = new GraphQLClient({ accessToken, baseURL });
77-
client.request({ data, headers }).then(() => {
78-
expect(callGraphQLAPI).toHaveBeenCalledWith({
79-
accessToken,
80-
baseURL,
81-
data,
82-
headers,
83-
});
84-
});
85-
});
86-
});
87-
8867
describe("callGraphQLAPI", () => {
8968
const query = '{ "test": "data" }';
9069
const variables = { option: "param1" };

Diff for: src/three-domain-secure/component.jsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const parseMerchantPayload = ({
6363

6464
export interface ThreeDomainSecureComponentInterface {
6565
isEligible(payload: MerchantPayloadData): Promise<boolean>;
66-
show(): ZalgoPromise<threeDSResponse>;
66+
show(): Promise<threeDSResponse>;
6767
}
6868

6969
export class ThreeDomainSecureComponent {
@@ -131,11 +131,9 @@ export class ThreeDomainSecureComponent {
131131
}
132132
}
133133

134-
show(): ZalgoPromise<threeDSResponse> {
134+
async show(): Promise<threeDSResponse> {
135135
if (!this.threeDSIframe) {
136-
return ZalgoPromise.reject(
137-
new ValidationError(`Ineligible for three domain secure`)
138-
);
136+
throw new ValidationError(`Ineligible for three domain secure`);
139137
}
140138
const promise = new ZalgoPromise();
141139
const cancelThreeDS = () => {
@@ -147,7 +145,7 @@ export class ThreeDomainSecureComponent {
147145
});
148146
};
149147
// $FlowFixMe
150-
const instance = this.threeDSIframe({
148+
const instance = await this.threeDSIframe({
151149
payerActionUrl: this.authenticationURL,
152150
onSuccess: async (res) => {
153151
const { reference_id, authentication_status, liability_shift } = res;

Diff for: src/three-domain-secure/component.test.js

+107-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* @flow */
22
/* eslint-disable eslint-comments/disable-enable-pair */
3-
/* eslint-disable no-restricted-globals, promise/no-native, compat/compat */
4-
import { describe, expect, vi } from "vitest";
3+
import { describe, expect, vi, afterEach } from "vitest";
4+
import { getEnv } from "@paypal/sdk-client/src";
5+
import { FPTI_KEY } from "@paypal/sdk-constants/src";
56

67
import { ThreeDomainSecureComponent } from "./component";
78

@@ -11,9 +12,18 @@ const defaultSdkConfig = {
1112
vi.mock("./utils", async () => {
1213
return {
1314
...(await vi.importActual("./utils")),
14-
getThreeDomainSecureComponent: vi.fn(),
15+
getFastlaneThreeDS: vi.fn(() => ({
16+
render: vi.fn().mockResolvedValue({}),
17+
close: vi.fn().mockResolvedValue({}),
18+
})),
1519
};
1620
});
21+
const mockThreeDSIframe = vi.fn(() => ({
22+
render: vi.fn().mockResolvedValue({}),
23+
close: vi.fn().mockResolvedValue({}),
24+
}));
25+
vi.mock("@paypal/sdk-client/src");
26+
vi.mocked(getEnv).mockReturnValue("stage");
1727
const defaultEligibilityResponse = {
1828
status: "PAYER_ACTION_REQUIRED",
1929
links: [{ href: "https://testurl.com", rel: "payer-action" }],
@@ -23,6 +33,20 @@ const defaultMerchantPayload = {
2333
amount: "1.00",
2434
currency: "USD",
2535
nonce: "test-nonce",
36+
transactionContext: {},
37+
};
38+
39+
const mockRestClient = {
40+
setAccessToken: vi.fn().mockResolvedValue({}),
41+
request: vi.fn().mockResolvedValue({
42+
status: "PAYER_ACTION_REQUIRED",
43+
links: [
44+
{
45+
href: "https://paypal.com/auth",
46+
rel: "payer-action",
47+
},
48+
],
49+
}),
2650
};
2751

2852
const mockEligibilityRequest = (body = defaultEligibilityResponse) => {
@@ -31,7 +55,7 @@ const mockEligibilityRequest = (body = defaultEligibilityResponse) => {
3155

3256
const createThreeDomainSecureComponent = ({
3357
sdkConfig = defaultSdkConfig,
34-
restClient = mockEligibilityRequest(),
58+
restClient = mockRestClient,
3559
graphQLClient = vi.fn(),
3660
logger = {
3761
info: vi.fn().mockReturnThis(),
@@ -56,8 +80,9 @@ afterEach(() => {
5680
vi.clearAllMocks();
5781
});
5882

59-
describe.skip("three domain secure component - isEligible method", () => {
83+
describe("three domain secure component - isEligible method", () => {
6084
test("should return true if payer action required", async () => {
85+
mockRestClient.request = mockEligibilityRequest();
6186
const threeDomainSecureClient = createThreeDomainSecureComponent();
6287
const eligibility = await threeDomainSecureClient.isEligible(
6388
defaultMerchantPayload
@@ -66,42 +91,34 @@ describe.skip("three domain secure component - isEligible method", () => {
6691
});
6792

6893
test("should return false if payer action is not returned", async () => {
69-
const threeDomainSecureClient = createThreeDomainSecureComponent({
70-
restClient: () =>
71-
Promise.resolve({ ...defaultEligibilityResponse, status: "SUCCESS" }),
72-
});
94+
const inEligibilityResponse = {
95+
status: "SUCCESS",
96+
links: [{ href: "https://testurl.com", rel: "order" }],
97+
};
98+
mockRestClient.request = mockEligibilityRequest(inEligibilityResponse);
99+
const threeDomainSecureClient = createThreeDomainSecureComponent();
73100
const eligibility = await threeDomainSecureClient.isEligible(
74101
defaultMerchantPayload
75102
);
76103
expect(eligibility).toEqual(false);
77104
});
78105

79106
test("should assign correct URL to authenticationURL", async () => {
80-
const threeDomainSecureClient = createThreeDomainSecureComponent({
81-
restClient: () =>
82-
Promise.resolve({
83-
...defaultEligibilityResponse,
84-
links: [
85-
{ href: "https://not-payer-action.com", rel: "not-payer-action" },
86-
...defaultEligibilityResponse.links,
87-
],
88-
}),
89-
});
107+
mockRestClient.request = mockEligibilityRequest(defaultEligibilityResponse);
108+
const threeDomainSecureClient = createThreeDomainSecureComponent();
90109
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
91110
expect(threeDomainSecureClient.authenticationURL).toEqual(
92111
"https://testurl.com"
93112
);
94113
});
95114

96-
test("create payload with correctly parameters", async () => {
97-
const mockedRequest = mockEligibilityRequest();
98-
const threeDomainSecureClient = createThreeDomainSecureComponent({
99-
restClient: mockedRequest,
100-
});
115+
test("create payload with correct parameters", async () => {
116+
mockRestClient.request = mockEligibilityRequest();
117+
const threeDomainSecureClient = createThreeDomainSecureComponent();
101118

102119
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
103120

104-
expect(mockedRequest).toHaveBeenCalledWith(
121+
expect(mockRestClient.request).toHaveBeenCalledWith(
105122
expect.objectContaining({
106123
data: expect.objectContaining({
107124
intent: "THREE_DS_VERIFICATION",
@@ -120,25 +137,66 @@ describe.skip("three domain secure component - isEligible method", () => {
120137
);
121138
});
122139

123-
test.skip("catch errors from the API", async () => {
124-
const mockRequest = vi.fn().mockRejectedValue(new Error("Error with API"));
125-
const threeDomainSecureClient = createThreeDomainSecureComponent({
126-
restClient: mockRequest,
127-
});
140+
test("catch errors from the API", async () => {
141+
mockRestClient.request = vi
142+
.fn()
143+
.mockRejectedValue(new Error("Error with API"));
144+
const threeDomainSecureClient = createThreeDomainSecureComponent();
128145

129146
expect.assertions(2);
130147
await expect(() =>
131148
threeDomainSecureClient.isEligible(defaultMerchantPayload)
132149
).rejects.toThrow(new Error("Error with API"));
133-
expect(mockRequest).toHaveBeenCalled();
150+
expect(mockRestClient.request).toHaveBeenCalled();
134151
});
135152
});
136153

137-
describe("three domain descure component - show method", () => {
138-
test.todo("should return a zoid component", () => {
154+
describe.todo("three domain descure component - show method", () => {
155+
test("should resolve successfully when threeDSIframe onSuccess is called", async () => {
156+
mockRestClient.request = mockEligibilityRequest();
157+
const threeDomainSecureClient = createThreeDomainSecureComponent();
158+
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
159+
// Arrange
160+
const mockSuccessResponse = {
161+
reference_id: "ref-123",
162+
authentication_status: "authenticated",
163+
liability_shift: true,
164+
};
165+
166+
const mockClose = vi.fn();
167+
168+
mockThreeDSIframe.mockImplementation(({ onSuccess }) => {
169+
setTimeout(() => onSuccess(mockSuccessResponse), 0);
170+
return { close: mockClose };
171+
});
172+
const promise = threeDomainSecureClient.show();
173+
174+
await expect(promise).resolves.toBeUndefined();
175+
expect(mockThreeDSIframe).toHaveBeenCalledWith({
176+
payerActionUrl: "test-url",
177+
onSuccess: expect.any(Function),
178+
});
179+
});
180+
test("should create a zoid component and assign to threeDSIframe", async () => {
181+
mockRestClient.request = mockEligibilityRequest();
182+
const threeDomainSecureClient = createThreeDomainSecureComponent();
183+
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
184+
expect(threeDomainSecureClient.threeDSIframe).toBeDefined();
185+
threeDomainSecureClient.threeDSIframe = mockThreeDSIframe;
186+
expect(await threeDomainSecureClient.show()).toEqual({
187+
liabilityShift: undefined,
188+
authenticationStatus: undefined,
189+
nonce: "test_nonce",
190+
});
191+
});
192+
193+
test("should render threeDS Iframe", async () => {
194+
mockRestClient.request = mockEligibilityRequest();
139195
const threeDomainSecureClient = createThreeDomainSecureComponent();
140-
threeDomainSecureClient.show();
141-
// create test for zoid component
196+
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
197+
198+
await threeDomainSecureClient.show();
199+
expect(threeDomainSecureClient.threeDSIframe).toBeCalled();
142200
});
143201
});
144202

@@ -155,4 +213,18 @@ describe("three domain secure component - initialization", () => {
155213
`script data attribute sdk-client-token is required but was not passed`
156214
);
157215
});
216+
217+
test("should log FPTI info on initialization", () => {
218+
const logger = {
219+
info: vi.fn().mockReturnThis(),
220+
track: vi.fn().mockReturnThis(),
221+
};
222+
createThreeDomainSecureComponent({
223+
logger,
224+
});
225+
expect(logger.info).toHaveBeenCalledWith("three domain secure v2 invoked");
226+
expect(logger.track).toHaveBeenCalledWith({
227+
[FPTI_KEY.TRANSITION]: "three_DS_auth_v2",
228+
});
229+
});
158230
});

Diff for: src/three-domain-secure/utils.test.js

-22
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,6 @@ describe("Three Domain Secure Utils", () => {
3232
// expect(createMock).toHaveBeenCalledTimes(1);
3333
});
3434

35-
it.skip("Should call zoid create with correct params", () => {
36-
vi.resetModules();
37-
const createMock = vi.fn();
38-
39-
vi.doMock("@krakenjs/zoid/src", () => ({
40-
create: createMock,
41-
type: "zoidComponent",
42-
}));
43-
44-
// const { create } = await import("@krakenjs/zoid/src");
45-
46-
getFastlaneThreeDS();
47-
48-
expect(createMock).toHaveBeenCalledTimes(1);
49-
expect(createMock).toHaveBeenCalledWith(
50-
expect.objectContaining({
51-
tag: "fastlane-threeds",
52-
url: expect.any(Function),
53-
})
54-
);
55-
});
56-
5735
it("Should set window.xchild if component is child", () => {
5836
vi.mock("@krakenjs/zoid/src", () => ({
5937
create: vi.fn(() => ({

0 commit comments

Comments
 (0)