-
Notifications
You must be signed in to change notification settings - Fork 683
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-240] [feature]: Cart Price Summary #2092
Merged
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2ae4c08
feature: price summary
sirugh 1ed3946
feature: dynamically available gift cards
sirugh bbcd286
dep: adds fraql to allow composition of unnamed fragments
sirugh ee504c5
discount summary component
sirugh 9c89db6
Break summary into components
sirugh 1021135
css update
sirugh 6a4e1eb
Pull fragment data off response and pass to components
sirugh 2b01c27
fix css
sirugh 01692a4
Use toggle for to no-op gift card component
sirugh 5e83196
Pass specific classes to line item components
sirugh 637f1dc
Export static summary query for use by refetch
sirugh 29a3bd2
Feedback. Also do not render tax until available.
sirugh 9da93c4
Update packages/venia-ui/lib/components/CartPage/PriceSummary/priceSu…
sirugh 5abd7a3
Merge branch 'develop' into rugh/price-summary
revanth0212 e7d28a0
add tests
sirugh af17370
Merge branch 'rugh/price-summary' of https://github.com/magento/pwa-s…
sirugh e84ed2d
Merge branch 'develop' into rugh/price-summary
dpatil-magento 2499226
fix import in test
sirugh f7f0e2b
Revert to graphql-tag and named exports
sirugh c1cd366
oops
sirugh 7b27b29
Use lazy query to avoid fetching until/unless a cart exists
sirugh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/peregrine/lib/talons/CartPage/PriceSummary/usePriceSummary.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
import { useQuery } from '@apollo/react-hooks'; | ||
import { useCartContext } from '@magento/peregrine/lib/context/cart'; | ||
|
||
/** | ||
* Flattens query data into a simple object. We create this here rather than | ||
* having each line summary line component destructure its own data because | ||
* only the parent "price summary" component knows the data structure. | ||
* | ||
* @param {Object} data query data | ||
*/ | ||
const flattenData = data => { | ||
if (!data) return {}; | ||
return { | ||
subtotal: data.cart.prices.subtotal_excluding_tax, | ||
total: data.cart.prices.grand_total, | ||
discounts: data.cart.prices.discounts, | ||
giftCards: data.cart.applied_gift_cards, | ||
taxes: data.cart.prices.applied_taxes, | ||
shipping: data.cart.shipping_addresses | ||
}; | ||
}; | ||
|
||
export const usePriceSummary = props => { | ||
const [{ cartId }] = useCartContext(); | ||
const { error, loading, data } = useQuery(props.query, { | ||
variables: { | ||
cartId | ||
}, | ||
fetchPolicy: 'no-cache' | ||
}); | ||
|
||
const handleProceedToCheckout = useCallback(() => { | ||
// TODO: Navigate to checkout view | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (error) { | ||
console.error('GraphQL Error:', error); | ||
} | ||
}, [error]); | ||
|
||
return { | ||
handleProceedToCheckout, | ||
hasError: !!error, | ||
hasItems: data && !!data.cart.items.length, | ||
isLoading: !!loading, | ||
flatData: flattenData(data) | ||
}; | ||
}; |
43 changes: 43 additions & 0 deletions
43
...lib/components/CartPage/PriceSummary/__tests__/__snapshots__/discountSummary.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders accumulated discount value 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Discounts applied | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
(- | ||
<span> | ||
$2 | ||
</span> | ||
) | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`renders discount summary line item correctly 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Discounts applied | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
(- | ||
<span> | ||
$10 | ||
</span> | ||
) | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`renders nothing if discount data is empty 1`] = `null`; | ||
|
||
exports[`renders nothing if discount value is "0" 1`] = `null`; |
9 changes: 9 additions & 0 deletions
9
...lib/components/CartPage/PriceSummary/__tests__/__snapshots__/giftCardSummary.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders accumulated gift card value 1`] = `null`; | ||
|
||
exports[`renders gift card summary line item correctly 1`] = `null`; | ||
|
||
exports[`renders nothing if gift card data is empty 1`] = `null`; | ||
|
||
exports[`renders nothing if gift card value is "0" 1`] = `null`; |
90 changes: 90 additions & 0 deletions
90
...ui/lib/components/CartPage/PriceSummary/__tests__/__snapshots__/priceSummary.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders PriceSummary correctly 1`] = ` | ||
<div | ||
className="root" | ||
> | ||
<div | ||
className="lineItems" | ||
> | ||
<span> | ||
Subtotal | ||
</span> | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
$11 | ||
</span> | ||
</span> | ||
<span> | ||
Discounts applied | ||
</span> | ||
<span | ||
className="price" | ||
> | ||
(- | ||
<span> | ||
$1 | ||
</span> | ||
) | ||
</span> | ||
<span> | ||
Estimated Tax | ||
</span> | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
$0 | ||
</span> | ||
</span> | ||
<span> | ||
Estimated Shipping | ||
</span> | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
FREE | ||
</span> | ||
</span> | ||
<span | ||
className="totalLabel" | ||
> | ||
Estimated Total | ||
</span> | ||
<span | ||
className="totalPrice" | ||
> | ||
<span> | ||
$10 | ||
</span> | ||
</span> | ||
</div> | ||
<div | ||
className="checkoutButton_container" | ||
> | ||
<button | ||
onClick={[Function]} | ||
type="button" | ||
> | ||
<span> | ||
PROCEED TO CHECKOUT | ||
</span> | ||
</button> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`renders an error state if query fails 1`] = ` | ||
<div | ||
className="root" | ||
> | ||
An error occurred. Please refresh the page. | ||
</div> | ||
`; | ||
|
||
exports[`renders nothing if query is loading 1`] = `null`; | ||
|
||
exports[`renders nothing if query returns no items 1`] = `null`; |
39 changes: 39 additions & 0 deletions
39
...lib/components/CartPage/PriceSummary/__tests__/__snapshots__/shippingSummary.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders "FREE"" if shipping value is "0" 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Estimated Shipping | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
FREE | ||
</span> | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`renders nothing if shipping data is empty 1`] = `null`; | ||
|
||
exports[`renders nothing if there is no selected shipping method 1`] = `null`; | ||
|
||
exports[`renders shipping summary line item correctly 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Estimated Shipping | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
$10 | ||
</span> | ||
</span>, | ||
] | ||
`; |
54 changes: 54 additions & 0 deletions
54
...a-ui/lib/components/CartPage/PriceSummary/__tests__/__snapshots__/taxSummary.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders accumulated tax value 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Estimated Tax | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
$2 | ||
</span> | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`renders nothing if tax data is empty 1`] = `null`; | ||
|
||
exports[`renders tax summary line item correctly 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Estimated Tax | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
$10 | ||
</span> | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`renders tax summary line item correctly if tax value is "0" 1`] = ` | ||
Array [ | ||
<span | ||
className="lineItemLabel" | ||
> | ||
Estimated Tax | ||
</span>, | ||
<span | ||
className="price" | ||
> | ||
<span> | ||
$0 | ||
</span> | ||
</span>, | ||
] | ||
`; |
93 changes: 93 additions & 0 deletions
93
packages/venia-ui/lib/components/CartPage/PriceSummary/__tests__/discountSummary.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React from 'react'; | ||
import { createTestInstance } from '@magento/peregrine'; | ||
|
||
import DiscountSummary from '../discountSummary'; | ||
|
||
jest.mock('../../../../classify'); | ||
|
||
jest.mock('@magento/peregrine', () => { | ||
const Price = props => <span>{`$${props.value}`}</span>; | ||
|
||
return { | ||
...jest.requireActual('@magento/peregrine'), | ||
Price | ||
}; | ||
}); | ||
|
||
const defaultProps = { | ||
classes: { | ||
lineItemLabel: 'lineItemLabel', | ||
price: 'price' | ||
}, | ||
data: [ | ||
{ | ||
amount: { | ||
value: 10, | ||
currency: 'USD' | ||
} | ||
} | ||
] | ||
}; | ||
|
||
test('renders discount summary line item correctly', () => { | ||
const tree = createTestInstance(<DiscountSummary {...defaultProps} />); | ||
|
||
expect(tree.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders accumulated discount value', () => { | ||
const props = { | ||
...defaultProps, | ||
data: [ | ||
{ | ||
amount: { | ||
value: 0, | ||
currency: 'USD' | ||
} | ||
}, | ||
{ | ||
amount: { | ||
value: 1, | ||
currency: 'USD' | ||
} | ||
}, | ||
{ | ||
amount: { | ||
value: 1, | ||
currency: 'USD' | ||
} | ||
} | ||
] | ||
}; | ||
|
||
const tree = createTestInstance(<DiscountSummary {...props} />); | ||
|
||
expect(tree.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders nothing if discount data is empty', () => { | ||
const props = { | ||
...defaultProps, | ||
data: [] | ||
}; | ||
const tree = createTestInstance(<DiscountSummary {...props} />); | ||
|
||
expect(tree.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders nothing if discount value is "0"', () => { | ||
const props = { | ||
...defaultProps, | ||
data: [ | ||
{ | ||
amount: { | ||
value: 0, | ||
currency: 'USD' | ||
} | ||
} | ||
] | ||
}; | ||
const tree = createTestInstance(<DiscountSummary {...props} />); | ||
|
||
expect(tree.toJSON()).toMatchSnapshot(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should verify that actions that modify the price summary values such as shipping method or adding a shipping address elsewhere also update the price summary even if
no-cache
is used here.