Skip to content

Commit 37f384a

Browse files
authored
Unit tests for amazon pay express and apple pay express (#1082)
1 parent 953500d commit 37f384a

4 files changed

Lines changed: 147 additions & 7 deletions

File tree

src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/amazonPayExpressPart2.test.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
let select;
66
let data;
7-
const saveShopperDetails = require('../../amazonPayExpressPart2');
7+
const {saveShopperDetails, constructAddress, wrapChangeAddressButton, showAddressDetails} = require('../../amazonPayExpressPart2');
88

99
beforeEach(async () => {
1010
document.body.innerHTML = `
@@ -38,4 +38,68 @@ describe('AmazonPay Express', () => {
3838
select.innerHTML.includes('EUR002'),
3939
);
4040
});
41+
42+
it('Should construct address correctly', () => {
43+
const shopperDetails = {
44+
shippingAddress: {
45+
name: 'John Doe',
46+
street: '123 Main St',
47+
city: 'Anytown',
48+
country: 'USA'
49+
},
50+
paymentDescriptor: 'Visa ending in 1234'
51+
};
52+
const expectedAddress = "John Doe\n123 Main St Anytown USA ";
53+
expect(constructAddress(shopperDetails)).toBe(expectedAddress);
54+
});
55+
56+
it('Should wrap change address button', () => {
57+
document.body.innerHTML = `
58+
<button class="adyen-checkout__button adyen-checkout__button--ghost adyen-checkout__amazonpay__button--changeAddress"></button>
59+
<button class="editAddressBtn"></button>
60+
`;
61+
wrapChangeAddressButton();
62+
const changeDetailsBtn = document.querySelector('.adyen-checkout__button.adyen-checkout__button--ghost.adyen-checkout__amazonpay__button--changeAddress');
63+
const editAddressBtn = document.querySelector('.editAddressBtn');
64+
editAddressBtn.click();
65+
expect(changeDetailsBtn.classList.contains('invisible')).toBe(true);
66+
});
67+
68+
it('Should show address details', () => {
69+
document.body.innerHTML = `
70+
<div id="address"></div>
71+
<div id="paymentStr"></div>
72+
<div id="amazon-container"></div>
73+
<select id="shippingMethods">
74+
<option> Child #1 </option>
75+
<option> Child #2 </option>
76+
</select>
77+
<div class="coupons-and-promos"></div>
78+
<button class="adyen-checkout__button adyen-checkout__button--standalone adyen-checkout__button--pay"></button>
79+
<button class="adyen-checkout__button adyen-checkout__button--ghost adyen-checkout__amazonpay__button--changeAddress"></button>
80+
<div id="amazonPayAddressDetails">
81+
<div> Child #1 </div>
82+
</div>
83+
`;
84+
85+
const shopperDetails = {
86+
shippingAddress: {
87+
name: 'John Doe',
88+
street: '123 Main St',
89+
city: 'Anytown',
90+
country: 'USA'
91+
},
92+
paymentDescriptor: 'Visa ending in 1234'
93+
};
94+
95+
showAddressDetails(shopperDetails);
96+
97+
const addressElement = document.getElementById('address');
98+
const paymentDescriptorElement = document.getElementById('paymentStr');
99+
const payBtn = document.querySelector('.adyen-checkout__button.adyen-checkout__button--standalone.adyen-checkout__button--pay');
100+
101+
expect(addressElement.innerText).toBe("John Doe\n123 Main St Anytown USA ");
102+
expect(paymentDescriptorElement.innerText).toBe('Visa ending in 1234');
103+
expect(payBtn.style.background).toBe('rgb(0, 161, 224)');
104+
});
41105
});

src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/applePayExpress.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ const {
77
handleError,
88
handleApplePayResponse,
99
callPaymentFromComponent,
10+
formatCustomerObject,
1011
} = require('../../applePayExpress');
1112

13+
1214
beforeEach(() => {
1315
jest.clearAllMocks();
1416

@@ -97,4 +99,69 @@ describe('Apple Pay Express', () => {
9799
JSON.stringify(response.fullResponse),
98100
);
99101
});
102+
103+
it('Should format customer and billing data correctly', () => {
104+
const customerData = {
105+
addressLines: ['123 Main St', 'Apt 2'],
106+
locality: 'City',
107+
country: 'United States',
108+
countryCode: 'US',
109+
givenName: 'John',
110+
familyName: 'Doe',
111+
emailAddress: 'john@example.com',
112+
postalCode: '12345',
113+
administrativeArea: 'State',
114+
phoneNumber: '+1234567890',
115+
};
116+
const billingData = {
117+
addressLines: ['456 Oak St'],
118+
locality: 'Town',
119+
country: 'United States',
120+
countryCode: 'US',
121+
givenName: 'Jane',
122+
familyName: 'Doe',
123+
postalCode: '54321',
124+
administrativeArea: 'Province',
125+
};
126+
const formattedData = formatCustomerObject(customerData, billingData);
127+
expect(formattedData).toEqual({
128+
addressBook: {
129+
addresses: {},
130+
preferredAddress: {
131+
address1: '123 Main St',
132+
address2: 'Apt 2',
133+
city: 'City',
134+
countryCode: {
135+
displayValue: 'United States',
136+
value: 'US',
137+
},
138+
firstName: 'John',
139+
lastName: 'Doe',
140+
ID: 'john@example.com',
141+
postalCode: '12345',
142+
stateCode: 'State',
143+
},
144+
},
145+
billingAddressDetails: {
146+
address1: '456 Oak St',
147+
address2: null,
148+
city: 'Town',
149+
countryCode: {
150+
displayValue: 'United States',
151+
value: 'US',
152+
},
153+
firstName: 'Jane',
154+
lastName: 'Doe',
155+
postalCode: '54321',
156+
stateCode: 'Province',
157+
},
158+
customer: {},
159+
profile: {
160+
firstName: 'John',
161+
lastName: 'Doe',
162+
email: 'john@example.com',
163+
phone: '+1234567890',
164+
},
165+
});
166+
});
100167
});

src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ function constructAddress(shopperDetails) {
3535
return addressStr;
3636
}
3737

38-
function positionElementBefore(elm) {
39-
const addressDetails = document.querySelector('#amazonPayAddressDetails');
40-
const containerNode = addressDetails.parentNode.parentNode.parentNode;
41-
containerNode.insertBefore(addressDetails, document.querySelector(elm));
38+
function positionElementBefore(elm, target) {
39+
const targetNode = document.querySelector(target);
40+
const addressDetails = document.querySelector(elm);
41+
if (targetNode && addressDetails) {
42+
targetNode.parentNode.insertBefore(addressDetails, targetNode);
43+
}
4244
}
4345

4446
function wrapChangeAddressButton() {
@@ -63,7 +65,7 @@ function showAddressDetails(shopperDetails) {
6365
addressElement.innerText = addressText;
6466
paymentDiscriptorElement.innerText = shopperDetails.paymentDescriptor;
6567

66-
positionElementBefore('.coupons-and-promos');
68+
positionElementBefore('#amazonPayAddressDetails', '.coupons-and-promos');
6769

6870
wrapChangeAddressButton();
6971

@@ -117,4 +119,10 @@ async function mountAmazonPayComponent() {
117119

118120
mountAmazonPayComponent();
119121

120-
module.exports = saveShopperDetails;
122+
module.exports = {
123+
saveShopperDetails,
124+
constructAddress,
125+
positionElementBefore,
126+
wrapChangeAddressButton,
127+
showAddressDetails,
128+
};

src/cartridges/app_adyen_SFRA/cartridge/client/default/js/applePayExpress.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,5 @@ module.exports = {
307307
handleError,
308308
handleApplePayResponse,
309309
callPaymentFromComponent,
310+
formatCustomerObject,
310311
};

0 commit comments

Comments
 (0)