-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathcomponent.test.js
239 lines (215 loc) · 7.14 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
import { describe, expect, vi, afterEach } from "vitest";
import { getEnv } from "@paypal/sdk-client/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";
import { ValidationError } from "../lib";
import { ThreeDomainSecureComponent } from "./component";
const defaultSdkConfig = {
authenticationToken: "sdk-client-token",
};
vi.mock("./utils", () => ({
getThreeDS: vi.fn(() => {
return vi.fn(() => ({
render: vi.fn().mockResolvedValue({}),
close: vi.fn(),
}));
}),
}));
vi.mock("@paypal/sdk-client/src");
vi.mocked(getEnv).mockReturnValue("stage");
const defaultEligibilityResponse = {
status: "PAYER_ACTION_REQUIRED",
links: [{ href: "https://testurl.com", rel: "payer-action" }],
};
const defaultMerchantPayload = {
amount: "1.00",
currency: "USD",
nonce: "test-nonce",
transactionContext: {},
};
const mockRestClient = {
setAccessToken: vi.fn().mockResolvedValue({}),
request: vi.fn().mockResolvedValue({
status: "PAYER_ACTION_REQUIRED",
links: [
{
href: "https://paypal.com/auth",
rel: "payer-action",
},
],
}),
};
const mockEligibilityRequest = (body = defaultEligibilityResponse) => {
return vi.fn().mockResolvedValue(body);
};
const createThreeDomainSecureComponent = ({
sdkConfig = defaultSdkConfig,
restClient = mockRestClient,
graphQLClient = vi.fn(),
logger = {
info: vi.fn().mockReturnThis(),
warn: vi.fn().mockReturnThis(),
error: vi.fn().mockReturnThis(),
track: vi.fn().mockReturnThis(),
metricCounter: vi.fn().mockReturnThis(),
},
} = {}) =>
new ThreeDomainSecureComponent({
// $FlowFixMe
sdkConfig,
// $FlowIssue
restClient,
// $FlowIssue
graphQLClient,
// $FlowIssue
logger,
});
afterEach(() => {
vi.clearAllMocks();
});
describe("three domain secure component - isEligible method", () => {
test("should return true if payer action required", async () => {
mockRestClient.request = mockEligibilityRequest();
const threeDomainSecureClient = createThreeDomainSecureComponent();
const eligibility = await threeDomainSecureClient.isEligible(
defaultMerchantPayload
);
expect(eligibility).toEqual(true);
});
test("should return false if payer action is not returned", async () => {
const inEligibilityResponse = {
status: "SUCCESS",
links: [{ href: "https://testurl.com", rel: "order" }],
};
mockRestClient.request = mockEligibilityRequest(inEligibilityResponse);
const threeDomainSecureClient = createThreeDomainSecureComponent();
const eligibility = await threeDomainSecureClient.isEligible(
defaultMerchantPayload
);
expect(eligibility).toEqual(false);
});
test("should assign correct URL to authenticationURL", async () => {
mockRestClient.request = mockEligibilityRequest(defaultEligibilityResponse);
const threeDomainSecureClient = createThreeDomainSecureComponent();
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
expect(threeDomainSecureClient.authenticationURL).toEqual(
"https://testurl.com"
);
});
test("create payload with correct parameters", async () => {
mockRestClient.request = mockEligibilityRequest();
const threeDomainSecureClient = createThreeDomainSecureComponent();
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
expect(mockRestClient.request).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
intent: "THREE_DS_VERIFICATION",
payment_source: expect.objectContaining({
card: expect.objectContaining({
single_use_token: defaultMerchantPayload.nonce,
verification_method: "SCA_WHEN_REQUIRED",
}),
}),
amount: expect.objectContaining({
currency_code: defaultMerchantPayload.currency,
value: defaultMerchantPayload.amount,
}),
}),
})
);
});
test("catch errors from the API", async () => {
mockRestClient.request = vi
.fn()
.mockRejectedValue(new Error("Error with API"));
const threeDomainSecureClient = createThreeDomainSecureComponent();
expect.assertions(2);
await expect(() =>
threeDomainSecureClient.isEligible(defaultMerchantPayload)
).rejects.toThrow(new Error("Error with API"));
expect(mockRestClient.request).toHaveBeenCalled();
});
test.each([
[
"undefined nonce",
{ currency: "USD", amount: "12.00" },
"[nonce] is required and must be a string. received: undefined",
],
[
"undefined currency",
{ amount: "12.00", nonce: "abc-nonce" },
"[currency] is required and must be a valid currency. received: undefined",
],
[
"invalid currency",
{ currency: "FOO", amount: "12.00", nonce: "abc-nonce" },
"[currency] is required and must be a valid currency. received: FOO",
],
[
"amount as a number",
{ currency: "USD", amount: 12, nonce: "abc-nonce" },
"[amount] is required and must be a string. received: 12",
],
[
"undefined amount",
{ currency: "USD", nonce: "abc-nonce" },
"[amount] is required and must be a string. received: undefined",
],
[
"multiple errors",
{},
"[amount] is required and must be a string. received: undefined\n" +
"[currency] is required and must be a valid currency. received: undefined\n" +
"[nonce] is required and must be a string. received: undefined",
],
])(
"should throw validation error on %s",
async (_assertion, params, expected) => {
const threeDomainSecureClient = createThreeDomainSecureComponent();
await expect(() =>
threeDomainSecureClient.isEligible(params)
).rejects.toThrow(new ValidationError(expected));
expect(threeDomainSecureClient.logger.warn).toHaveBeenCalledWith(
expected
);
}
);
});
describe("three domain descure component - show method", () => {
test("should reject if threeDSIframe is not available", async () => {
const threeDomainSecureClient = createThreeDomainSecureComponent();
// $FlowFixMe
threeDomainSecureClient.threeDSIframe = undefined;
await expect(threeDomainSecureClient.show()).rejects.toThrowError(
"Ineligible for three domain secure"
);
});
});
describe("three domain secure component - initialization", () => {
test("should throw an error if sdkToken is not present", () => {
expect(() =>
createThreeDomainSecureComponent({
sdkConfig: {
...defaultSdkConfig,
authenticationToken: "",
},
})
).toThrowError(
`script data attribute sdk-client-token is required but was not passed`
);
});
test("should log FPTI info on initialization", () => {
const logger = {
info: vi.fn().mockReturnThis(),
track: vi.fn().mockReturnThis(),
};
createThreeDomainSecureComponent({
logger,
});
expect(logger.info).toHaveBeenCalledWith("three domain secure v2 invoked");
expect(logger.track).toHaveBeenCalledWith({
[FPTI_KEY.TRANSITION]: "three_DS_auth_v2",
});
});
});