Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pwa 2172: Validate Shopping Cart Promotions are displayed correctly #3609

Merged
merged 26 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
22ab6b1
PWA-2172: Validate Shopping Cart Promotions are displayed correctly
Dec 9, 2021
13c5d69
PWA-2172: Validate Shopping Cart Promotions are displayed correctly
Dec 9, 2021
3ca4e47
Merge branch 'PWA-2172' of github.com:magento/pwa-studio into PWA-2172
Dec 9, 2021
da56f74
PWA-2172: Validate Shopping Cart Promotions are displayed correctly
Dec 10, 2021
81e56fe
Merge branch 'develop' into PWA-2172
michaelyu0123 Dec 10, 2021
035e5f4
Merge remote-tracking branch 'upstream/develop' into PWA-2172
Dec 13, 2021
a782b7a
Merge branch 'PWA-2172' of github.com:magento/pwa-studio into PWA-2172
Dec 13, 2021
7e8f24c
Fix formatting for fixture
jcalcaben Dec 13, 2021
73ce065
Refactor talon out of discount summary component
jcalcaben Dec 14, 2021
749b8c5
Create jest test for talon
jcalcaben Dec 14, 2021
6b6ab79
Refactor style to use Tailwind classes
jcalcaben Dec 15, 2021
3ada84b
Remove extra imports
jcalcaben Dec 16, 2021
35191e8
Fix linting issue
jcalcaben Dec 16, 2021
497e754
Update snapshots
jcalcaben Dec 16, 2021
bf20f90
Remove bullets and increase spacing between price summary entries
jcalcaben Jan 5, 2022
11bb385
Update tests and snapshots
jcalcaben Jan 5, 2022
8e409ab
Merge branch 'develop' into PWA-2172
jcalcaben Jan 6, 2022
93fe0b4
Small change to trigger rebuild
jcalcaben Jan 6, 2022
0b9ceb7
Roll back small change
jcalcaben Jan 6, 2022
14fa162
Merge remote-tracking branch 'upstream/develop' into PWA-2172
Feb 24, 2022
ab17c3d
PWA-2172: Validate Shopping Cart Promotions are displayed correctly
Feb 24, 2022
cec163e
Merge remote-tracking branch 'upstream/develop' into PWA-2172
Feb 24, 2022
721f375
PWA-2172: Validate Shopping Cart Promotions are displayed correctly
Feb 24, 2022
608fe42
Merge branch 'develop' into PWA-2172
Feb 25, 2022
22118d7
Merge branch 'develop' into PWA-2172
dpatil-magento Feb 25, 2022
135b399
PWA-2171 Fix media files path
Feb 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@
"priceAdjustments.giftOptions": "Voir les options de cadeaux",
"priceAdjustments.shippingMethod": "Estimez votre expédition",
"priceSummary.checkoutButton": "Passer à la caisse",
"priceSummary.discountSummary.hideDiscounts": "Cacher les rabais individuels.",
"priceSummary.discountSummary.showDiscounts": "Afficher les rabais individuels.",
"priceSummary.errorText": "Un problème est survenu. Veuillez actualiser et réessayer.",
"priceSummary.estimatedTotal": "Total estimé",
"priceSummary.lineItemLabel": "Sous-total",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import React from 'react';
import { createTestInstance } from '@magento/peregrine';

import { useDiscountSummary } from '../useDiscountSummary';
import { act } from 'react-test-renderer';

const Component = props => {
const talonProps = useDiscountSummary(props);

return <i {...talonProps} />;
};

const mockDiscountData = [
{
amount: {
currency: 'USD',
value: 10
}
},
{
amount: {
currency: 'USD',
value: 20
}
},
{
amount: {
currency: 'USD',
value: 30
}
}
];

it('returns the proper shape', () => {
const props = {
data: mockDiscountData
};

const rendered = createTestInstance(<Component {...props} />);

const talonProps = rendered.root.findByType('i').props;

expect(talonProps).toMatchInlineSnapshot(`
Object {
"discountData": Array [
Object {
"amount": Object {
"currency": "USD",
"value": 10,
},
},
Object {
"amount": Object {
"currency": "USD",
"value": 20,
},
},
Object {
"amount": Object {
"currency": "USD",
"value": 30,
},
},
],
"expanded": false,
"handleClick": [Function],
"totalDiscount": Object {
"currency": "USD",
"value": 60,
},
}
`);
});

it('toggles the expanded state when handleClick() is called', () => {
const props = {
data: mockDiscountData
};

const rendered = createTestInstance(<Component {...props} />);

let talonProps = rendered.root.findByType('i').props;

act(() => {
talonProps.handleClick();
});

talonProps = rendered.root.findByType('i').props;

expect(talonProps.expanded).toBeTruthy();

act(() => {
talonProps.handleClick();
});
talonProps = rendered.root.findByType('i').props;

expect(talonProps.expanded).toBeFalsy();
});

describe('returns a default amount when', () => {
test('there is no discount data', () => {
const props = {};

const rendered = createTestInstance(<Component {...props} />);

const talonProps = rendered.root.findByType('i').props;

expect(talonProps).toMatchInlineSnapshot(`
Object {
"discountData": undefined,
"expanded": false,
"handleClick": [Function],
"totalDiscount": Object {
"currency": "USD",
"value": 0,
},
}
`);
});

test('discount data is an empty array', () => {
const props = {
data: []
};

const rendered = createTestInstance(<Component {...props} />);

const talonProps = rendered.root.findByType('i').props;

expect(talonProps).toMatchInlineSnapshot(`
Object {
"discountData": Array [],
"expanded": false,
"handleClick": [Function],
"totalDiscount": Object {
"currency": "USD",
"value": 0,
},
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useCallback, useState } from 'react';

const DEFAULT_AMOUNT = {
currency: 'USD',
value: 0
};

/**
* Reduces discounts array into a single amount.
*
* @param {Array} discounts
*/
const getTotalDiscount = (discounts = []) => {
// discounts from data can be null
if (!discounts || !discounts.length) {
return DEFAULT_AMOUNT;
} else {
return {
currency: discounts[0].amount.currency,
value: discounts.reduce(
(acc, discount) => acc + discount.amount.value,
0
)
};
}
};

/**
* This talon contains the logic for the discount summary component.
*
* @param {Object} props
* @param {Array} props.data Discount data
* @returns {DiscountSummaryProps}
*/
export const useDiscountSummary = props => {
const { data: discountData } = props;

const totalDiscount = getTotalDiscount(discountData);

const [expanded, setExpanded] = useState(false);

const handleClick = useCallback(() => {
setExpanded(value => !value);
}, [setExpanded]);

return {
totalDiscount,
discountData,
expanded,
handleClick
};
};

/** JSDocs type definitions */

/**
* @typedef {Object} DiscountSummaryProps
*
* @property {DiscountObject} discount Object containing discount information
* @property {Array} discountData Array of discounts
* @property {Boolean} expanded
* @property {Function} handleClick Click handler for toggling the expanded state
*
*/

/**
* @typedef {Object} DiscountObject
*
* @property {String} currency Currency code
* @property {Number} value Discount amount
*/
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const usePriceSummary = (props = {}) => {
*
* @property {String} subtotal Cart subtotal (excluding tax)
* @property {String} total Cart grand total
* @property {Array<Object>} discounts Discounts applied to the cart
* @property {Array<Object>} discounts Applied discounts to the cart
* @property {Array<Object>} giftCards Gift cards applied to the cart
* @property {Array<Object>} giftOptions Gift Options applied to the cart
* @property {Array<Object>} taxes Taxes applied to the cart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ test('exposes all hooks and targets', async () => {
talons.CartPage.PriceAdjustments.ShippingMethods.useShippingMethods.wrapWith() wraps export "useShippingMethods" from "CartPage/PriceAdjustments/ShippingMethods/useShippingMethods.js"
talons.CartPage.PriceAdjustments.ShippingMethods.useShippingRadios.wrapWith() wraps export "useShippingRadios" from "CartPage/PriceAdjustments/ShippingMethods/useShippingRadios.js"
talons.CartPage.PriceAdjustments.useGiftOptionsSection.wrapWith() wraps export "useGiftOptionsSection" from "CartPage/PriceAdjustments/useGiftOptionsSection.js"
talons.CartPage.PriceSummary.useDiscountSummary.wrapWith() wraps export "useDiscountSummary" from "CartPage/PriceSummary/useDiscountSummary.js"
talons.CartPage.PriceSummary.usePriceSummary.wrapWith() wraps export "usePriceSummary" from "CartPage/PriceSummary/usePriceSummary.js"
talons.CartPage.ProductListing.EditModal.useEditModal.wrapWith() wraps export "useEditModal" from "CartPage/ProductListing/EditModal/useEditModal.js"
talons.CartPage.ProductListing.EditModal.useProductForm.wrapWith() wraps export "useProductForm" from "CartPage/ProductListing/EditModal/useProductForm.js"
Expand Down
4 changes: 3 additions & 1 deletion packages/venia-ui/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"customerForm.defaultShipping": "Make this my default address",
"customerForm.formMessage": "The shipping address you enter will be saved to your address book and set as your default for future purchases.",
"customerForm.loading": "Fetching Customer Details...",
"discountSummary.lineItemLabel": "Discounts applied",
"discountSummary.lineItemLabel": "Applied discounts",
"editModal.headerText": "Edit Item",
"Email Signup": "Email Signup",
"errorView.header": "Oops!",
Expand Down Expand Up @@ -329,6 +329,8 @@
"priceAdjustments.giftOptions": "See Gift Options",
"priceAdjustments.shippingMethod": "Estimate your Shipping",
"priceSummary.checkoutButton": "Proceed to Checkout",
"priceSummary.discountSummary.hideDiscounts": "Hide individual discounts.",
"priceSummary.discountSummary.showDiscounts": "Show individual discounts.",
"priceSummary.errorText": "Something went wrong. Please refresh and try again.",
"priceSummary.estimatedTotal": "Estimated Total",
"priceSummary.lineItemLabel": "Subtotal",
Expand Down
Loading