Skip to content

Commit a119fc9

Browse files
authored
CIF-1333 - The Minicart shows an incorrect number of items (#239)
* Update the GraphQL query to include the total number of items. * Update the footer component to read the total number of items from the backend instead of calculating it in the component. * Put back the dev dependency to @magento/peregrine * Fix the cart quantity display in the cart trigger as well * Update unit tests
1 parent b613cd9 commit a119fc9

File tree

9 files changed

+131
-28
lines changed

9 files changed

+131
-28
lines changed

react-components/__mocks__/i18nForTests.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ import common from '../i18n/en/common.json';
2222
i18n.use(initReactI18next).init({
2323
fallbackLng: 'en',
2424
debug: false,
25+
lng: 'en',
2526

2627
interpolation: {
2728
escapeValue: false,
2829
format: (value, format, lng) => {
2930
if (format === 'price') {
30-
return new Intl.NumberFormat(lng, { style: 'currency', currency: value.currency }).format(
31-
value.value
32-
);
31+
return new Intl.NumberFormat(lng, { style: 'currency', currency: value.currency }).format(value.value);
3332
}
3433
return value;
3534
}

react-components/package-lock.json

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-components/src/components/CartTrigger/__test__/__snapshots__/cartTrigger.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ exports[`<CartTrigger> renders the icon 1`] = `
3939
class="root"
4040
data-testid="cart-counter"
4141
>
42-
2
42+
3
4343
</span>
4444
</button>
4545
</DocumentFragment>

react-components/src/components/CartTrigger/__test__/cartTrigger.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('<CartTrigger>', () => {
2828
ReactDOM.createPortal.mockClear();
2929
});
3030

31-
const stateWithTwoItems = { cart: { items: [{}, {}] } };
31+
const stateWithTwoItems = { cart: { items: [{}, {}], total_quantity: 3 } };
3232

3333
it('renders the icon', () => {
3434
const { asFragment } = render(
@@ -40,7 +40,7 @@ describe('<CartTrigger>', () => {
4040
});
4141

4242
it('renders the quantity', () => {
43-
const expectedQuantity = '2';
43+
const expectedQuantity = '3';
4444
const { getByTestId } = render(
4545
<CartProvider initialState={stateWithTwoItems} reducerFactory={() => state => state}>
4646
<CartTrigger />

react-components/src/components/CartTrigger/cartTrigger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const parentEl = document.querySelector('.header__cartTrigger');
2626

2727
const Trigger = () => {
2828
const [{ cart }, dispatch] = useCartState();
29-
let cartQuantity = cart && Object.entries(cart).length > 0 ? cart.items.length : 0;
29+
let cartQuantity = cart && Object.entries(cart).length > 0 ? cart.total_quantity : 0;
3030

3131
const iconColor = 'rgb(var(--venia-text))';
3232
const svgAttributes = {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<Footer> renders the component 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="root"
7+
>
8+
<div
9+
class="root"
10+
>
11+
<dl
12+
class="totals"
13+
>
14+
<dt
15+
class="subtotalLabel"
16+
>
17+
<span>
18+
Cart Total:
19+
<span
20+
class="discounted"
21+
>
22+
$60.00
23+
</span>
24+
</span>
25+
</dt>
26+
<dd
27+
class="subtotalValue"
28+
>
29+
3 items
30+
</dd>
31+
</dl>
32+
<dl
33+
class="totalsDiscount"
34+
>
35+
<dt
36+
class="subtotalLabel"
37+
>
38+
<span>
39+
New Cart Total:
40+
<span
41+
class=""
42+
>
43+
$34.00
44+
</span>
45+
</span>
46+
</dt>
47+
<dd
48+
class="subtotalValue"
49+
>
50+
3 items
51+
</dd>
52+
</dl>
53+
</div>
54+
</div>
55+
</DocumentFragment>
56+
`;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************************
2+
*
3+
* Copyright 2019 Adobe. All rights reserved.
4+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License. You may obtain a copy
6+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed under
9+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10+
* OF ANY KIND, either express or implied. See the License for the specific language
11+
* governing permissions and limitations under the License.
12+
*
13+
******************************************************************************/
14+
import React from 'react';
15+
import { render } from '@testing-library/react';
16+
import { I18nextProvider } from 'react-i18next';
17+
18+
import { CartProvider } from '../cartContext';
19+
import Footer from '../footer';
20+
import i18n from '../../../../__mocks__/i18nForTests';
21+
jest.mock('../../Checkout', () => {
22+
return () => {
23+
return null;
24+
};
25+
});
26+
describe('<Footer>', () => {
27+
it('renders the component', () => {
28+
const { asFragment } = render(
29+
<I18nextProvider i18n={i18n}>
30+
<CartProvider
31+
initialState={{
32+
cart: {
33+
prices: {
34+
subtotal_excluding_tax: { currency: 'USD', value: 60 },
35+
subtotal_with_discount_excluding_tax: { currency: 'USD', value: 34 }
36+
},
37+
total_quantity: 3
38+
}
39+
}}
40+
reducerFactory={() => state => state}>
41+
<Footer />
42+
</CartProvider>
43+
</I18nextProvider>
44+
);
45+
46+
expect(asFragment()).toMatchSnapshot();
47+
});
48+
});

react-components/src/components/Minicart/footer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const Footer = () => {
2828
<TotalsSummary
2929
subtotal={subtotal_excluding_tax}
3030
subtotalDiscount={subtotal_with_discount_excluding_tax}
31-
numItems={cart.items.length}
31+
numItems={cart.total_quantity}
3232
/>
3333
<Checkout />
3434
</div>

react-components/src/queries/query_cart_details.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ query cartDetails($cartId: String!) {
7474
applied_coupon {
7575
code
7676
}
77-
77+
total_quantity
7878
items {
7979
id
8080
quantity

0 commit comments

Comments
 (0)