Skip to content

Commit 4857dc7

Browse files
authored
Defining ThreeDomainSecureClient component interface (#2447)
* chore: prettier * allow three-domain-secure component * refactor threedomainsecure component to class * correct typo * refactor test for class component * chore: fix lint * chore: fix flow issues * pin flow-remove-types and hermes-parser version due to flow errors * return methods only instead of entire class * modify interface to reflect future state
1 parent 798390b commit 4857dc7

File tree

8 files changed

+136
-77
lines changed

8 files changed

+136
-77
lines changed

Diff for: __sdk__.js

+3
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ module.exports = {
9393
"shopper-insights": {
9494
entry: "./src/shopper-insights/interface",
9595
},
96+
"three-domain-secure": {
97+
entry: "./src/three-domain-secure/interface",
98+
},
9699
};

Diff for: dist/button.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/test/button.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@
101101
"puppeteer": "^1.20.0",
102102
"serve": "^14.0.0",
103103
"vite": "^3.2.4",
104-
"vitest": "^1.3.1"
104+
"vitest": "^1.3.1",
105+
"flow-remove-types": "2.246.0",
106+
"hermes-parser": "0.23.1"
105107
},
106108
"dependencies": {
107109
"@krakenjs/beaver-logger": "^5.7.0",

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

+48-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
/* @flow */
2-
import { getLogger, getSDKToken } from "@paypal/sdk-client/src";
2+
import { type LoggerType } from "@krakenjs/beaver-logger/src";
3+
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
4+
import { create, type ZoidComponent } from "@krakenjs/zoid/src";
35
import { FPTI_KEY } from "@paypal/sdk-constants/src";
46

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

7-
export const getThreeDomainSecure = (): Function => {
8-
const sdkToken = getSDKToken();
9-
const ThreeDomainSecureAuth = () => {
10-
if (sdkToken) {
11-
// eslint-disable-next-line no-console
12-
console.log("Three Domain Secure Called");
13-
// Make a Zoid component and introduce methods here
14-
// onSuccess
15-
// onCancel
16-
// onClose
17-
getLogger()
18-
.info("three domain secure v2 invoked")
19-
.track({
20-
[FPTI_KEY.TRANSITION]: "three_DS_auth_v2",
21-
});
22-
} else {
23-
throw new ValidationError(
24-
`script data attribute sdk-client-token is required but was not passed`
25-
);
26-
}
27-
};
28-
29-
return ThreeDomainSecureAuth;
9+
type SdkConfig = {|
10+
sdkToken: ?string,
11+
|};
12+
13+
const parseSdkConfig = ({ sdkConfig, logger }): SdkConfig => {
14+
if (!sdkConfig.sdkToken) {
15+
throw new ValidationError(
16+
`script data attribute sdk-client-token is required but was not passed`
17+
);
18+
}
19+
20+
logger.info("three domain secure v2 invoked").track({
21+
[FPTI_KEY.TRANSITION]: "three_DS_auth_v2",
22+
});
23+
24+
return sdkConfig;
3025
};
26+
export interface ThreeDomainSecureComponentInterface {
27+
isEligible(): ZalgoPromise<boolean>;
28+
show(): ZoidComponent<void>;
29+
}
30+
export class ThreeDomainSecureComponent {
31+
logger: LoggerType;
32+
sdkConfig: SdkConfig;
33+
34+
constructor({
35+
logger,
36+
sdkConfig,
37+
}: {|
38+
logger: LoggerType,
39+
sdkConfig: SdkConfig,
40+
|}) {
41+
this.logger = logger;
42+
this.sdkConfig = parseSdkConfig({ sdkConfig, logger });
43+
}
44+
45+
isEligible(): ZalgoPromise<boolean> {
46+
return new ZalgoPromise((resolve) => {
47+
resolve(false);
48+
});
49+
}
50+
51+
show() {
52+
create({ tag: "", url: "" });
53+
}
54+
}

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

+48-33
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,59 @@
11
/* @flow */
2-
import { describe, expect, it, vi } from "vitest";
3-
import { getSDKToken } from "@paypal/sdk-client/src";
2+
import { describe, expect, vi } from "vitest";
43

5-
import { ValidationError } from "../lib";
4+
import { ThreeDomainSecureComponent } from "./component";
65

7-
import { getThreeDomainSecure } from "./component";
6+
const defaultSdkConfig = {
7+
sdkToken: "sdk-client-token",
8+
};
89

9-
vi.mock("@paypal/sdk-client/src", () => ({
10-
getSDKToken: vi.fn(),
11-
getLogger: vi.fn(() => ({
10+
const createThreeDomainSecureComponent = ({
11+
sdkConfig = defaultSdkConfig,
12+
logger = {
1213
info: vi.fn().mockReturnThis(),
14+
warn: vi.fn().mockReturnThis(),
15+
error: vi.fn().mockReturnThis(),
1316
track: vi.fn().mockReturnThis(),
14-
flush: vi.fn().mockReturnThis(),
15-
})),
16-
}));
17-
vi.mock("../lib", () => ({
18-
ValidationError: vi.fn(),
19-
}));
20-
describe("getThreeDomainSecure returns ThreeDomainSecureComponent", () => {
21-
it("should throw an error if sdkToken is not present", () => {
22-
// $FlowFixMe prop missing error
23-
getSDKToken.mockReturnValue(undefined);
24-
const ThreeDomainSecureComponent = getThreeDomainSecure();
25-
expect(() => ThreeDomainSecureComponent()).toThrowError(ValidationError);
26-
expect(ValidationError).toHaveBeenCalledWith(
27-
`script data attribute sdk-client-token is required but was not passed`
28-
);
17+
metricCounter: vi.fn().mockReturnThis(),
18+
},
19+
} = {}) =>
20+
new ThreeDomainSecureComponent({
21+
sdkConfig,
22+
// $FlowIssue
23+
logger,
2924
});
30-
it("should return the ThreeDomainSecure component and log the correct message", async () => {
31-
// eslint-disable-next-line no-empty-function
32-
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
33-
// $FlowFixMe prop missing error
34-
getSDKToken.mockReturnValue("84ghb8984");
35-
const ThreeDomainSecureComponent = getThreeDomainSecure();
36-
expect(typeof ThreeDomainSecureComponent).toBe("function");
3725

38-
// Call the returned component and check the console log
39-
await ThreeDomainSecureComponent();
40-
expect(consoleSpy).toHaveBeenCalledWith("Three Domain Secure Called");
26+
afterEach(() => {
27+
vi.clearAllMocks();
28+
});
29+
30+
describe("three domain secure component - isEligible method", () => {
31+
test("should return false", async () => {
32+
const threeDomainSecuretClient = createThreeDomainSecureComponent();
33+
const eligibility = await threeDomainSecuretClient.isEligible();
34+
expect(eligibility).toEqual(false);
35+
});
36+
});
4137

42-
consoleSpy.mockRestore();
38+
describe("three domain descure component - show method", () => {
39+
test.skip("should return a zoid component", () => {
40+
const threeDomainSecuretClient = createThreeDomainSecureComponent();
41+
threeDomainSecuretClient.show();
42+
// create test for zoid component
43+
});
44+
});
45+
46+
describe("three domain secure component - initialization", () => {
47+
test("should throw an error if sdkToken is not present", () => {
48+
expect(() =>
49+
createThreeDomainSecureComponent({
50+
sdkConfig: {
51+
...defaultSdkConfig,
52+
sdkToken: "",
53+
},
54+
})
55+
).toThrowError(
56+
`script data attribute sdk-client-token is required but was not passed`
57+
);
4358
});
4459
});

Diff for: src/three-domain-secure/interface.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
/* @flow */
2-
import { type ZoidComponent } from "@krakenjs/zoid/src";
2+
import { getLogger, getSDKToken } from "@paypal/sdk-client/src";
33

44
import type { LazyExport } from "../types";
55
import { protectedExport } from "../lib";
66

7-
import { getThreeDomainSecure } from "./component";
7+
import {
8+
ThreeDomainSecureComponent,
9+
type ThreeDomainSecureComponentInterface,
10+
} from "./component";
811

9-
type ThreeDomainSecureAuth = ZoidComponent<void>;
10-
11-
export const ThreeDomainSecureComponent: LazyExport<ThreeDomainSecureAuth> = {
12-
__get__: () => protectedExport(getThreeDomainSecure()),
13-
};
12+
export const ThreeDomainSecureClient: LazyExport<ThreeDomainSecureComponentInterface> =
13+
{
14+
__get__: () => {
15+
const threeDomainSecureInstance = new ThreeDomainSecureComponent({
16+
logger: getLogger(),
17+
sdkConfig: {
18+
sdkToken: getSDKToken(),
19+
},
20+
});
21+
return protectedExport({
22+
isEligible: () => threeDomainSecureInstance.isEligible(),
23+
show: () => threeDomainSecureInstance.show(),
24+
});
25+
},
26+
};

Diff for: src/zoid/card-fields/component.jsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ const CARD_FIELD_TYPE = {
4848
};
4949

5050
type InstallmentsConfiguration = {|
51-
financingCountryCode : string,
52-
currencyCode : string,
53-
billingCountryCode : string,
54-
amount : string,
55-
includeBuyerInstallments ? : boolean
51+
financingCountryCode: string,
52+
currencyCode: string,
53+
billingCountryCode: string,
54+
amount: string,
55+
includeBuyerInstallments?: boolean,
5656
|};
5757

5858
type CardFieldsProps = {|
@@ -109,10 +109,12 @@ type CardFieldsProps = {|
109109
hcfSessionID: string,
110110
partnerAttributionID: string,
111111
merchantID: $ReadOnlyArray<string>,
112-
installments? : {|
113-
onInstallmentsRequested : () => InstallmentsConfiguration | ZalgoPromise<InstallmentsConfiguration>,
114-
onInstallmentsAvailable : (Object) => void,
115-
onInstallmentsError? : (Object) => void
112+
installments?: {|
113+
onInstallmentsRequested: () =>
114+
| InstallmentsConfiguration
115+
| ZalgoPromise<InstallmentsConfiguration>,
116+
onInstallmentsAvailable: (Object) => void,
117+
onInstallmentsError?: (Object) => void,
116118
|},
117119
|};
118120

@@ -445,7 +447,7 @@ export const getCardFieldsComponent: () => CardFieldsComponent = memoize(
445447
installments: {
446448
type: "object",
447449
required: false,
448-
value: ({ props }) => props.parent.props.installments
450+
value: ({ props }) => props.parent.props.installments,
449451
},
450452
},
451453
});

0 commit comments

Comments
 (0)