-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathcomponent.test.js
153 lines (131 loc) · 4.46 KB
/
component.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* @flow */
import { getUserIDToken, getSDKToken } from "@paypal/sdk-client/src";
import { loadAxo } from "@paypal/accelerated-checkout-loader";
import { describe, expect, test, vi } from "vitest";
import {
getConnectComponent,
getSdkVersion,
DEFAULT_BT_VERSION,
} from "./component";
vi.mock("@paypal/sdk-client/src", () => {
return {
getClientID: vi.fn(() => "mock-client-id"),
getClientMetadataID: vi.fn(() => "mock-cmid"),
getUserIDToken: vi.fn(() => "mock-uid"),
getSDKToken: vi.fn().mockReturnValue("mock-sdk-token"),
getDebug: vi.fn(() => false),
getLogger: vi.fn(() => ({
metric: vi.fn().mockReturnThis(),
error: vi.fn().mockReturnThis(),
track: vi.fn().mockReturnThis(),
flush: vi.fn().mockReturnThis(),
metricCounter: vi.fn().mockReturnThis(),
})),
getEnv: vi.fn().mockReturnValue("mock-env"),
getCSPNonce: vi.fn(),
loadFraudnet: vi.fn(() => ({ collect: vi.fn() })),
};
});
vi.mock("@paypal/accelerated-checkout-loader", () => {
return {
loadAxo: vi.fn(),
};
});
describe("getConnectComponent: returns ConnectComponent", () => {
const mockAxoMetadata = { someData: "data" };
const mockProps = { someProp: "value" };
beforeEach(() => {
vi.clearAllMocks();
window.braintree = {
connect: {
create: vi.fn(),
},
};
loadAxo.mockResolvedValue({ metadata: mockAxoMetadata });
});
test("uses user id token if no sdk token is present", async () => {
// $FlowIssue
getUserIDToken.mockReturnValue("user-id-token");
// $FlowIssue
getSDKToken.mockReturnValue(undefined);
await getConnectComponent(mockProps);
expect(window.braintree.connect.create).toHaveBeenCalledWith({
...mockAxoMetadata,
...mockProps,
platformOptions: {
platform: "PPCP",
clientId: "mock-client-id",
clientMetadataId: "mock-cmid",
userIdToken: "user-id-token",
fraudnet: expect.any(Function),
env: "mock-env",
},
});
});
test("uses sdk token if present", async () => {
// $FlowIssue
getUserIDToken.mockReturnValue("user-id-token");
// $FlowIssue
getSDKToken.mockReturnValue("sdk-client-token");
await getConnectComponent(mockProps);
expect(window.braintree.connect.create).toHaveBeenCalledWith({
...mockAxoMetadata,
...mockProps,
platformOptions: {
platform: "PPCP",
clientId: "mock-client-id",
clientMetadataId: "mock-cmid",
userIdToken: "sdk-client-token",
fraudnet: expect.any(Function),
env: "mock-env",
},
});
});
test("loadAxo failure is handled", async () => {
const errorMessage = "Something went wrong";
loadAxo.mockRejectedValue(errorMessage);
await expect(() => getConnectComponent(mockProps)).rejects.toThrow(
errorMessage
);
});
test("connect create failure is handled", async () => {
const expectedError = "create failed";
window.braintree.connect.create.mockRejectedValue(expectedError);
await expect(() => getConnectComponent(mockProps)).rejects.toThrow(
expectedError
);
});
test("loadAxo is call with default values", async () => {
await getConnectComponent(mockProps);
expect(loadAxo).toHaveBeenCalledWith({
minified: true,
btSdkVersion: "3.116.2",
metadata: undefined,
platform: "PPCP",
});
});
});
describe("getSdkVersion", () => {
test("returns minimum supported braintree version for AXO if input version is null", () => {
const version = getSdkVersion(null);
expect(version).toEqual(DEFAULT_BT_VERSION);
});
test("returns the version passed if it is supported for AXO", () => {
const result2 = getSdkVersion("3.97.3-alpha-test");
const result3 = getSdkVersion("4.34.3-beta-test");
const result4 = getSdkVersion("4.34.47");
const result5 = getSdkVersion("3.98.3");
const result6 = getSdkVersion("3.97.6");
expect(result2).toEqual("3.97.3-alpha-test");
expect(result3).toEqual("4.34.3-beta-test");
expect(result4).toEqual("4.34.47");
expect(result5).toEqual("3.98.3");
expect(result6).toEqual("3.97.6");
});
test("throws error if the version passed is not supported for AXO and is not null", () => {
expect(() => getSdkVersion("3.96.00")).toThrowError();
expect(() => getSdkVersion("3.97.00")).toThrowError();
expect(() => getSdkVersion("2.87.1-alpha-test")).toThrowError();
expect(() => getSdkVersion("3.34.2-beta-test")).toThrowError();
});
});