-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathcomponent.test.js
150 lines (133 loc) · 4.49 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
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native, compat/compat */
import { describe, expect, vi } from "vitest";
import { ThreeDomainSecureComponent } from "./component";
const defaultSdkConfig = {
authenticationToken: "sdk-client-token",
};
const defaultEligibilityResponse = {
status: "PAYER_ACTION_REQUIRED",
links: [{ href: "https://testurl.com", rel: "payer-action" }],
};
const defaultMerchantPayload = {
amount: "1.00",
currency: "USD",
nonce: "test-nonce",
};
const mockEligibilityRequest = (body = defaultEligibilityResponse) => {
return vi.fn().mockResolvedValue(body);
};
const createThreeDomainSecureComponent = ({
sdkConfig = defaultSdkConfig,
request = mockEligibilityRequest(),
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
request,
// $FlowIssue
logger,
});
afterEach(() => {
vi.clearAllMocks();
});
describe("three domain secure component - isEligible method", () => {
test("should return true if payer action required", async () => {
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 threeDomainSecureClient = createThreeDomainSecureComponent({
request: () =>
Promise.resolve({ ...defaultEligibilityResponse, status: "SUCCESS" }),
});
const eligibility = await threeDomainSecureClient.isEligible(
defaultMerchantPayload
);
expect(eligibility).toEqual(false);
});
test("should assign correct URL to authenticationURL", async () => {
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: () =>
Promise.resolve({
...defaultEligibilityResponse,
links: [
{ href: "https://not-payer-action.com", rel: "not-payer-action" },
...defaultEligibilityResponse.links,
],
}),
});
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
expect(threeDomainSecureClient.authenticationURL).toEqual(
"https://testurl.com"
);
});
test("create payload with correctly parameters", async () => {
const mockedRequest = mockEligibilityRequest();
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: mockedRequest,
});
await threeDomainSecureClient.isEligible(defaultMerchantPayload);
expect(mockedRequest).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 () => {
const mockRequest = vi.fn().mockRejectedValue(new Error("Error with API"));
const threeDomainSecureClient = createThreeDomainSecureComponent({
request: mockRequest,
});
expect.assertions(2);
await expect(() =>
threeDomainSecureClient.isEligible(defaultMerchantPayload)
).rejects.toThrow(new Error("Error with API"));
expect(mockRequest).toHaveBeenCalled();
});
});
describe("three domain descure component - show method", () => {
test.todo("should return a zoid component", () => {
const threeDomainSecureClient = createThreeDomainSecureComponent();
threeDomainSecureClient.show();
// create test for zoid component
});
});
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`
);
});
});