Skip to content

Commit db5e3ed

Browse files
committed
Fix and add tests for user error mapping
1 parent d34edec commit db5e3ed

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/handle-cart-mutation.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ export default function handleCartMutation(mutationRootKey, client) {
99
if (rootData && rootData.cart) {
1010
return client.fetchAllPages(rootModel.cart.lines, {pageSize: 250}).then((lines) => {
1111
rootModel.cart.attrs.lines = lines;
12-
rootModel.cart.errors = errors;
13-
rootModel.cart.userErrors = rootData.userErrors;
12+
const checkoutUserErrors = checkoutUserErrorsMapper(rootData.userErrors, rootData.warnings);
1413

1514
try {
16-
return mapCartPayload(rootModel.cart, mutationRootKey);
15+
return Object.assign({},
16+
mapCartPayload(rootModel.cart, mutationRootKey),
17+
{userErrors: checkoutUserErrors, errors: rootModel.cart.errors}
18+
);
1719
} catch (error) {
18-
return Promise.reject(new Error(JSON.stringify(error.message)));
20+
return Promise.reject(error);
1921
}
2022
});
2123
}
@@ -24,10 +26,10 @@ export default function handleCartMutation(mutationRootKey, client) {
2426
return Promise.reject(new Error(JSON.stringify(errors)));
2527
}
2628

27-
if (rootData && (rootData.userErrors || rootData.warnings)) {
29+
if (rootData && (rootData.userErrors.length || rootData.warnings.length)) {
2830
const checkoutUserErrors = checkoutUserErrorsMapper(rootData.userErrors, rootData.warnings);
2931

30-
return Promise.reject(new Error(JSON.stringify(checkoutUserErrors)));
32+
return Promise.reject(checkoutUserErrors);
3133
}
3234

3335
return Promise.reject(new Error(`The ${mutationRootKey} mutation failed due to an unknown error.`));

test/client-checkout-integration-test.js

+66
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ suite('client-checkout-integration-test', () => {
123123
});
124124
});
125125

126+
test('it rejects the promise if there is an error with the input', () => {
127+
const input = {
128+
lineItems: [
129+
{
130+
variantId: 'gid://shopify/ProductVariant/50850334310456',
131+
quantity: 99999999
132+
},
133+
{
134+
variantId: 'gid://shopify/ProductVariant/50850336211000',
135+
quantity: 1
136+
}
137+
]
138+
};
139+
140+
return client.checkout.create(input).catch((error) => {
141+
assert.deepStrictEqual(error, [
142+
{
143+
code: 'INVALID',
144+
field: [
145+
'input',
146+
'lines',
147+
'0',
148+
'quantity'
149+
],
150+
message: 'The quantity for merchandise with id gid://shopify/ProductVariant/50850334310456 must be greater than zero but less than 1000000.'
151+
}
152+
]);
153+
});
154+
});
155+
126156
test('it resolves a localized non-empty checkout created with buyerIdentity.countryCode', () => {
127157
const input = {
128158
buyerIdentity: {
@@ -525,5 +555,41 @@ suite('client-checkout-integration-test', () => {
525555
});
526556
});
527557
});
558+
559+
test('it returns any user errors', () => {
560+
const inputWithHtmlTags = {
561+
shippingAddress: {
562+
address1: '<html>123 Oak St</html>',
563+
address2: '<script>Unit 2</script>',
564+
city: '<script>Ottawa</script>',
565+
company: '<script>Shopify</script>',
566+
country: '<script>Canada</script>',
567+
firstName: 'John',
568+
lastName: 'Doe',
569+
phone: '+16135551111',
570+
province: 'ON',
571+
zip: '123 ABC'
572+
}
573+
};
574+
575+
return client.checkout.create({}).then((checkout) => {
576+
577+
return client.checkout.updateShippingAddress(checkout.id, inputWithHtmlTags.shippingAddress).then((updatedCheckout) => {
578+
assert.deepStrictEqual(updatedCheckout.userErrors, [
579+
{
580+
code: 'INVALID',
581+
field: [
582+
'buyerIdentity',
583+
'deliveryAddressPreferences',
584+
'0',
585+
'deliveryAddress',
586+
'country'
587+
],
588+
message: 'invalid value'
589+
}
590+
]);
591+
});
592+
});
593+
});
528594
});
529595
});

0 commit comments

Comments
 (0)