Skip to content

Commit

Permalink
Pwa 2172: Validate Shopping Cart Promotions are displayed correctly (#…
Browse files Browse the repository at this point in the history
…3609)

* PWA-2172: Validate Shopping Cart Promotions are displayed correctly

- Updated discountSummary and price summary allows individual discount dropdown
- Added test coverage for feature

* PWA-2172: Validate Shopping Cart Promotions are displayed correctly

- Updated discountSummary and price summary allows individual discount dropdown
- Added test coverage for feature

* PWA-2172: Validate Shopping Cart Promotions are displayed correctly

- Added dropdown animation
- Updated tests
- Updated discount summary styles

* Fix formatting for fixture

* Refactor talon out of discount summary component

* Create jest test for talon

* Refactor style to use Tailwind classes

* Remove extra imports

* Fix linting issue

* Update snapshots

* Remove bullets and increase spacing between price summary entries

* Update tests and snapshots

* Small change to trigger rebuild

* Roll back small change

* PWA-2172: Validate Shopping Cart Promotions are displayed correctly

- Resolved cypress test failure

* PWA-2172: Validate Shopping Cart Promotions are displayed correctly

- Resolved cypress test failure

* PWA-2171 Fix media files path

Co-authored-by: James Calcaben <calcaben@adobe.com>
Co-authored-by: James Calcaben <jcalcaben@users.noreply.github.com>
Co-authored-by: Mikhaël Bois <mbois@absolunet.com>
Co-authored-by: Devagouda <40405790+dpatil-magento@users.noreply.github.com>
  • Loading branch information
5 people authored Feb 25, 2022
1 parent e3f5fd5 commit 5b87fec
Show file tree
Hide file tree
Showing 23 changed files with 1,751 additions and 342 deletions.
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

0 comments on commit 5b87fec

Please sign in to comment.