Skip to content

Commit 66f61be

Browse files
committed
Update totalPrice mapping and add more integration tests
There was a mistake originally, as cart's total amount includes any gift cards that have been applied, but checkout's total price should reflect the price before gift cards have been applied. Additionally, I've added more integration tests to ensure that all the expected fields are being returned (not null).
1 parent a8f97fd commit 66f61be

5 files changed

+167
-3
lines changed

src/cart-payload-mapper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function calculateTotalPrice(cart, totalAmount) {
219219
}
220220

221221
return {
222-
amount: (parseFloat(totalAmount.amount) - giftCardTotal).toFixed(2),
222+
amount: (parseFloat(totalAmount.amount) + giftCardTotal).toFixed(2),
223223
currencyCode: totalAmount.currencyCode,
224224
type: totalAmount.type
225225
};

test/cart-payload-mapper-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ suite('cart-payload-mapper-test', () => {
364364
const result = mapCartPayload(cart);
365365

366366
assert.deepStrictEqual(result.subtotalPrice, withType({
367-
amount: '269.82',
367+
amount: '314.18',
368368
currencyCode: 'USD'
369369
}, 'MoneyV2'));
370370
});
@@ -425,7 +425,7 @@ suite('cart-payload-mapper-test', () => {
425425
assert.deepStrictEqual(
426426
result.totalPrice,
427427
withType({
428-
amount: '294.82',
428+
amount: '339.18',
429429
currencyCode: 'USD'
430430
}, 'MoneyV2')
431431
);

test/client-checkout-discounts-integration-test.js

+59
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ suite('client-checkout-discounts-integration-test', () => {
151151
assert.strictEqual(updatedCheckout.discountApplications.length, 1);
152152
assert.strictEqual(updatedCheckout.lineItems[0].discountAllocations.length, 1);
153153

154+
// Add assertions for previously missing price fields
155+
assert.ok(updatedCheckout.lineItemsSubtotalPrice, 'lineItemsSubtotalPrice exists');
156+
assert.ok(updatedCheckout.lineItemsSubtotalPrice.amount, 'lineItemsSubtotalPrice amount exists');
157+
assert.ok(updatedCheckout.lineItemsSubtotalPrice.currencyCode, 'lineItemsSubtotalPrice currencyCode exists');
158+
159+
assert.ok(updatedCheckout.subtotalPrice, 'subtotalPrice exists');
160+
assert.ok(updatedCheckout.subtotalPriceV2, 'subtotalPriceV2 exists');
161+
assert.strictEqual(updatedCheckout.subtotalPrice.amount, updatedCheckout.subtotalPriceV2.amount, 'subtotalPrice amount matches V2');
162+
163+
assert.ok(updatedCheckout.totalPrice, 'totalPrice exists');
164+
assert.ok(updatedCheckout.totalPriceV2, 'totalPriceV2 exists');
165+
// Total price should be less than original due to discount
166+
const totalPriceNum = parseFloat(updatedCheckout.totalPrice.amount);
167+
const lineItemPrice = parseFloat(updatedCheckout.lineItems[0].variant.price.amount);
168+
169+
assert.ok(totalPriceNum < lineItemPrice, 'totalPrice reflects discount');
170+
171+
assert.ok(updatedCheckout.totalTax, 'totalTax exists');
172+
assert.ok(updatedCheckout.totalTaxV2, 'totalTaxV2 exists');
173+
174+
// Verify UNSUPPORTED_FIELDS maintain expected values with discounts applied
175+
assert.strictEqual(updatedCheckout.completedAt, null, 'completedAt is null');
176+
assert.strictEqual(updatedCheckout.order, null, 'order is null');
177+
assert.strictEqual(updatedCheckout.orderStatusUrl, null, 'orderStatusUrl is null');
178+
assert.strictEqual(updatedCheckout.ready, false, 'ready is false');
179+
assert.strictEqual(updatedCheckout.requiresShipping, true, 'requiresShipping is true');
180+
assert.strictEqual(updatedCheckout.shippingLine, null, 'shippingLine is null');
181+
assert.strictEqual(updatedCheckout.taxExempt, false, 'taxExempt is false');
182+
assert.strictEqual(updatedCheckout.taxesIncluded, false, 'taxesIncluded is false');
183+
154184
const expectedRootDiscountApplications = [
155185
{
156186
__typename: 'DiscountCodeApplication',
@@ -506,6 +536,35 @@ suite('client-checkout-discounts-integration-test', () => {
506536

507537
assert.strictEqual(updatedCheckout.discountApplications.length, 1);
508538
assertActualDiscountApplicationIsExpected(updatedCheckout.discountApplications[0], expectedRootDiscountApplications[0]);
539+
540+
assert.ok(updatedCheckout.lineItemsSubtotalPrice, 'lineItemsSubtotalPrice exists');
541+
assert.ok(updatedCheckout.lineItemsSubtotalPrice.amount, 'lineItemsSubtotalPrice amount exists');
542+
assert.ok(updatedCheckout.lineItemsSubtotalPrice.currencyCode, 'lineItemsSubtotalPrice currencyCode exists');
543+
544+
assert.ok(updatedCheckout.subtotalPrice, 'subtotalPrice exists');
545+
assert.ok(updatedCheckout.subtotalPriceV2, 'subtotalPriceV2 exists');
546+
assert.strictEqual(updatedCheckout.subtotalPrice.amount, updatedCheckout.subtotalPriceV2.amount, 'subtotalPrice amount matches V2');
547+
548+
assert.ok(updatedCheckout.totalPrice, 'totalPrice exists');
549+
assert.ok(updatedCheckout.totalPriceV2, 'totalPriceV2 exists');
550+
// Total price should be less than original due to discount
551+
const totalPriceNum = parseFloat(updatedCheckout.totalPrice.amount);
552+
const lineItemPrice = parseFloat(updatedCheckout.lineItems[0].variant.price.amount);
553+
554+
assert.ok(totalPriceNum < lineItemPrice, 'totalPrice reflects discount');
555+
556+
assert.ok(updatedCheckout.totalTax, 'totalTax exists');
557+
assert.ok(updatedCheckout.totalTaxV2, 'totalTaxV2 exists');
558+
559+
// Verify UNSUPPORTED_FIELDS maintain expected values with discounts applied
560+
assert.strictEqual(updatedCheckout.completedAt, null, 'completedAt is null');
561+
assert.strictEqual(updatedCheckout.order, null, 'order is null');
562+
assert.strictEqual(updatedCheckout.orderStatusUrl, null, 'orderStatusUrl is null');
563+
assert.strictEqual(updatedCheckout.ready, false, 'ready is false');
564+
assert.strictEqual(updatedCheckout.requiresShipping, true, 'requiresShipping is true');
565+
assert.strictEqual(updatedCheckout.shippingLine, null, 'shippingLine is null');
566+
assert.strictEqual(updatedCheckout.taxExempt, false, 'taxExempt is false');
567+
assert.strictEqual(updatedCheckout.taxesIncluded, false, 'taxesIncluded is false');
509568
});
510569
});
511570
});

test/client-checkout-giftcards-integration-test.js

+61
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,41 @@ suite('client-checkout-giftcards-integration-test', () => {
121121
implementsNode: false
122122
}
123123
});
124+
125+
assert.ok(updatedCheckout.lineItemsSubtotalPrice, 'lineItemsSubtotalPrice exists');
126+
assert.strictEqual(updatedCheckout.lineItemsSubtotalPrice.amount, '70.0', 'lineItemsSubtotalPrice amount is correct');
127+
assert.strictEqual(updatedCheckout.lineItemsSubtotalPrice.currencyCode, 'CAD', 'lineItemsSubtotalPrice currencyCode is correct');
128+
129+
assert.ok(updatedCheckout.subtotalPrice, 'subtotalPrice exists');
130+
assert.ok(updatedCheckout.subtotalPriceV2, 'subtotalPriceV2 exists');
131+
assert.strictEqual(updatedCheckout.subtotalPrice, updatedCheckout.subtotalPriceV2);
132+
assert.strictEqual(updatedCheckout.subtotalPrice.amount, '70.0', 'subtotalPrice does not include gift cards');
133+
assert.strictEqual(updatedCheckout.subtotalPrice.currencyCode, 'CAD', 'subtotalPrice currency is correct');
134+
135+
assert.ok(updatedCheckout.totalPrice, 'totalPrice exists');
136+
assert.ok(updatedCheckout.totalPriceV2, 'totalPriceV2 exists');
137+
assert.strictEqual(updatedCheckout.totalPrice, updatedCheckout.totalPriceV2);
138+
assert.strictEqual(updatedCheckout.totalPrice.amount, '70.0', 'totalPrice does not include gift cards');
139+
assert.strictEqual(updatedCheckout.totalPrice.currencyCode, 'CAD', 'totalPrice currency is correct');
140+
141+
assert.ok(updatedCheckout.totalTax, 'totalTax exists');
142+
assert.ok(updatedCheckout.totalTaxV2, 'totalTaxV2 exists');
143+
144+
assert.ok(updatedCheckout.paymentDue, 'paymentDue exists');
145+
assert.ok(updatedCheckout.paymentDueV2, 'paymentDueV2 exists');
146+
assert.strictEqual(updatedCheckout.paymentDue, updatedCheckout.paymentDueV2);
147+
assert.strictEqual(updatedCheckout.paymentDue.amount, '0.0', 'paymentDue amount includes gift cards');
148+
assert.strictEqual(updatedCheckout.paymentDue.currencyCode, 'CAD', 'paymentDue currencyCode is correct');
149+
150+
// Verify UNSUPPORTED_FIELDS maintain expected values
151+
assert.strictEqual(updatedCheckout.completedAt, null, 'completedAt is null');
152+
assert.strictEqual(updatedCheckout.order, null, 'order is null');
153+
assert.strictEqual(updatedCheckout.orderStatusUrl, null, 'orderStatusUrl is null');
154+
assert.strictEqual(updatedCheckout.ready, false, 'ready is false');
155+
assert.strictEqual(updatedCheckout.requiresShipping, true, 'requiresShipping is true');
156+
assert.strictEqual(updatedCheckout.shippingLine, null, 'shippingLine is null');
157+
assert.strictEqual(updatedCheckout.taxExempt, false, 'taxExempt is false');
158+
assert.strictEqual(updatedCheckout.taxesIncluded, false, 'taxesIncluded is false');
124159
});
125160
});
126161
});
@@ -165,6 +200,32 @@ suite('client-checkout-giftcards-integration-test', () => {
165200
assert.ok(updatedCheckout.appliedGiftCards[1].id);
166201
assert.strictEqual(updatedCheckout.appliedGiftCards[1].lastCharacters, 'card');
167202
assert.ok(updatedCheckout.appliedGiftCards[1].type);
203+
204+
205+
assert.ok(updatedCheckout.lineItemsSubtotalPrice, 'lineItemsSubtotalPrice exists');
206+
assert.strictEqual(updatedCheckout.lineItemsSubtotalPrice.amount, '270.0', 'lineItemsSubtotalPrice does not include gift cards');
207+
assert.strictEqual(updatedCheckout.lineItemsSubtotalPrice.currencyCode, 'CAD', 'lineItemsSubtotalPrice currencyCode is correct');
208+
209+
assert.ok(updatedCheckout.subtotalPrice, 'subtotalPrice exists');
210+
assert.ok(updatedCheckout.subtotalPriceV2, 'subtotalPriceV2 exists');
211+
assert.strictEqual(updatedCheckout.subtotalPrice, updatedCheckout.subtotalPriceV2);
212+
assert.strictEqual(updatedCheckout.subtotalPrice.amount, '270.0', 'subtotalPrice does not include gift cards');
213+
assert.strictEqual(updatedCheckout.subtotalPrice.currencyCode, 'CAD', 'subtotalPrice currency is correct');
214+
215+
assert.ok(updatedCheckout.totalPrice, 'totalPrice exists');
216+
assert.ok(updatedCheckout.totalPriceV2, 'totalPriceV2 exists');
217+
assert.strictEqual(updatedCheckout.totalPrice, updatedCheckout.totalPriceV2);
218+
assert.strictEqual(updatedCheckout.totalPrice.amount, '270.0', 'totalPrice does not include gift cards');
219+
assert.strictEqual(updatedCheckout.totalPrice.currencyCode, 'CAD', 'totalPrice currency is correct');
220+
221+
assert.ok(updatedCheckout.paymentDue, 'paymentDue exists');
222+
assert.ok(updatedCheckout.paymentDueV2, 'paymentDueV2 exists');
223+
assert.strictEqual(updatedCheckout.paymentDue, updatedCheckout.paymentDueV2);
224+
assert.strictEqual(updatedCheckout.paymentDue.amount, '120.0', 'paymentDue amount includes gift cards');
225+
assert.strictEqual(updatedCheckout.paymentDue.currencyCode, 'CAD', 'paymentDue currencyCode is correct');
226+
227+
assert.ok(updatedCheckout.totalTax, 'totalTax exists');
228+
assert.ok(updatedCheckout.totalTaxV2, 'totalTaxV2 exists');
168229
});
169230
});
170231
});

test/client-checkout-integration-test.js

+44
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,50 @@ suite('client-checkout-integration-test', () => {
189189
});
190190
});
191191

192+
suite('payload fields verification', () => {
193+
test('it returns all expected fields including price fields in a checkout with items', () => {
194+
const input = {
195+
lineItems: [
196+
{
197+
variantId: 'gid://shopify/ProductVariant/50850334310456',
198+
quantity: 1
199+
}
200+
]
201+
};
202+
203+
return client.checkout.create(input).then((checkout) => {
204+
assert.ok(checkout.lineItemsSubtotalPrice, 'lineItemsSubtotalPrice exists');
205+
assert.ok(checkout.lineItemsSubtotalPrice.amount, 'lineItemsSubtotalPrice.amount exists');
206+
assert.ok(checkout.lineItemsSubtotalPrice.currencyCode, 'lineItemsSubtotalPrice.currencyCode exists');
207+
208+
assert.ok(checkout.subtotalPrice, 'subtotalPrice exists');
209+
assert.ok(checkout.subtotalPriceV2, 'subtotalPriceV2 exists');
210+
assert.strictEqual(checkout.subtotalPrice.amount, checkout.subtotalPriceV2.amount, 'subtotalPrice amount matches V2');
211+
assert.strictEqual(checkout.subtotalPrice.currencyCode, checkout.subtotalPriceV2.currencyCode, 'subtotalPrice currency matches V2');
212+
213+
assert.ok(checkout.totalPrice, 'totalPrice exists');
214+
assert.ok(checkout.totalPriceV2, 'totalPriceV2 exists');
215+
assert.strictEqual(checkout.totalPrice.amount, checkout.totalPriceV2.amount, 'totalPrice amount matches V2');
216+
assert.strictEqual(checkout.totalPrice.currencyCode, checkout.totalPriceV2.currencyCode, 'totalPrice currency matches V2');
217+
218+
assert.ok(checkout.totalTax, 'totalTax exists');
219+
assert.ok(checkout.totalTaxV2, 'totalTaxV2 exists');
220+
assert.strictEqual(checkout.totalTax.amount, checkout.totalTaxV2.amount, 'totalTax amount matches V2');
221+
assert.strictEqual(checkout.totalTax.currencyCode, checkout.totalTaxV2.currencyCode, 'totalTax currency matches V2');
222+
223+
// Verify the UNSUPPORTED_FIELDS have expected values
224+
assert.strictEqual(checkout.completedAt, null, 'completedAt is null');
225+
assert.strictEqual(checkout.order, null, 'order is null');
226+
assert.strictEqual(checkout.orderStatusUrl, null, 'orderStatusUrl is null');
227+
assert.strictEqual(checkout.ready, false, 'ready is false');
228+
assert.strictEqual(checkout.requiresShipping, true, 'requiresShipping is true');
229+
assert.strictEqual(checkout.shippingLine, null, 'shippingLine is null');
230+
assert.strictEqual(checkout.taxExempt, false, 'taxExempt is false');
231+
assert.strictEqual(checkout.taxesIncluded, false, 'taxesIncluded is false');
232+
});
233+
});
234+
});
235+
192236
suite('updateAttributes', () => {
193237
test('it updates a checkout note via updateAttributes', () => {
194238
const input = {

0 commit comments

Comments
 (0)