-
Notifications
You must be signed in to change notification settings - Fork 142
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
Testing product sets - @W-12414116@ #1003
Changes from all commits
7cb7bc3
eab9fad
7aa5147
b0b94a4
ee66061
d1e15d2
aab2990
40a45fa
c649c19
5496607
f46f6be
f89299f
560825f
0fd3c3f
5f8d763
de72da8
c8b26a0
169e5bd
ea2a73a
ee599f4
6fd0759
429a269
bfd4a79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,13 +62,18 @@ export const AddToCartModal = () => { | |
const intl = useIntl() | ||
const basket = useBasket() | ||
const size = useBreakpointValue({base: 'full', lg: '2xl', xl: '4xl'}) | ||
const {currency, productItems, productSubTotal, itemAccumulatedCount} = basket | ||
const {currency, productItems, productSubTotal} = basket | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const totalQuantity = itemsAdded.reduce((acc, {quantity}) => acc + quantity, 0) | ||
|
||
return ( | ||
<Modal size={size} isOpen={isOpen} onClose={onClose} scrollBehavior="inside" isCentered> | ||
<ModalOverlay /> | ||
<ModalContent margin="0" borderRadius={{base: 'none', md: 'base'}} bgColor="gray.50"> | ||
<ModalContent | ||
margin="0" | ||
borderRadius={{base: 'none', md: 'base'}} | ||
bgColor="gray.50" | ||
containerProps={{'data-testid': 'add-to-cart-modal'}} | ||
> | ||
<ModalHeader paddingY="8" bgColor="white" fontSize="2xl" fontWeight="700"> | ||
{intl.formatMessage( | ||
{ | ||
|
@@ -118,6 +123,7 @@ export const AddToCartModal = () => { | |
borderBottomWidth={{base: '1px', lg: '0px'}} | ||
borderColor="gray.200" | ||
borderStyle="solid" | ||
data-testid="product-added" | ||
> | ||
<Flex gridGap="4"> | ||
<Box w="24" flex="none"> | ||
|
@@ -180,7 +186,7 @@ export const AddToCartModal = () => { | |
'Cart Subtotal ({itemAccumulatedCount} item)', | ||
id: 'add_to_cart_modal.label.cart_subtotal' | ||
}, | ||
{itemAccumulatedCount} | ||
{itemAccumulatedCount: totalQuantity} | ||
)} | ||
</Text> | ||
<Text alignSelf="flex-end" fontWeight="600"> | ||
|
@@ -245,7 +251,7 @@ export const AddToCartModal = () => { | |
defaultMessage: 'Cart Subtotal ({itemAccumulatedCount} item)', | ||
id: 'add_to_cart_modal.label.cart_subtotal' | ||
}, | ||
{itemAccumulatedCount} | ||
{itemAccumulatedCount: totalQuantity} | ||
)} | ||
</Text> | ||
<Text alignSelf="flex-end" fontWeight="600"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import React from 'react' | ||
import {AddToCartModal, AddToCartModalContext} from './use-add-to-cart-modal' | ||
import {renderWithProviders} from '../utils/test-utils' | ||
import {screen} from '@testing-library/react' | ||
|
||
const MOCK_PRODUCT = { | ||
currency: 'USD', | ||
|
@@ -560,18 +561,24 @@ const MOCK_PRODUCT = { | |
c_width: 'Z' | ||
} | ||
|
||
test('Renders AddToCartModal', () => { | ||
test('Renders AddToCartModal with multiple products', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tend to make all tests use at least some amount of |
||
const MOCK_DATA = { | ||
product: MOCK_PRODUCT, | ||
itemsAdded: [ | ||
{ | ||
product: MOCK_PRODUCT, | ||
variant: MOCK_PRODUCT.variants[0], | ||
quantity: 22 | ||
}, | ||
{ | ||
product: MOCK_PRODUCT, | ||
variant: MOCK_PRODUCT.variants[0], | ||
quantity: 1 | ||
} | ||
] | ||
} | ||
const {getByText} = renderWithProviders( | ||
|
||
renderWithProviders( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<AddToCartModalContext.Provider | ||
value={{ | ||
isOpen: true, | ||
|
@@ -582,7 +589,11 @@ test('Renders AddToCartModal', () => { | |
</AddToCartModalContext.Provider> | ||
) | ||
|
||
expect(getByText(MOCK_PRODUCT.name)).toBeInTheDocument() | ||
expect(screen.getAllByText(MOCK_PRODUCT.name)[0]).toBeInTheDocument() | ||
expect(screen.getByRole('dialog', {name: /23 items added to cart/i})).toBeInTheDocument() | ||
|
||
const numOfRowsRendered = screen.getAllByTestId('product-added').length | ||
expect(numOfRowsRendered).toEqual(MOCK_DATA.itemsAdded.length) | ||
Comment on lines
+592
to
+596
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember you saying something about "awaiting too many things" which I'm still a bit confused about. If you want all of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I won't use the After the component is rendered, I expect all those |
||
}) | ||
|
||
test('Do not render when isOpen is false', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2023, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import React from 'react' | ||
import {screen} from '@testing-library/react' | ||
import PropTypes from 'prop-types' | ||
import {usePDPSearchParams} from './use-pdp-search-params' | ||
import {renderWithProviders} from '../utils/test-utils' | ||
|
||
const MockComponent = ({productId} = {}) => { | ||
const [allParams, productParams] = usePDPSearchParams(productId) | ||
|
||
return ( | ||
<> | ||
<div data-testid="all-params">{allParams.toString()}</div> | ||
<div data-testid="product-params">{productParams.toString()}</div>{' '} | ||
</> | ||
) | ||
} | ||
MockComponent.propTypes = { | ||
productId: PropTypes.string | ||
} | ||
|
||
test('product set', () => { | ||
const url = | ||
// The parent's id is `winter-lookM`, while the children's are `25518447M` and `25518704M` | ||
'/global/en-GB/product/winter-lookM?25518447M=color%3DJJ5FUXX%26size%3D9XL&25518704M=color%3DJJ2XNXX%26size%3D9MD' | ||
window.history.pushState({}, '', url) | ||
|
||
renderWithProviders(<MockComponent productId="25518704M" />) | ||
|
||
expect(screen.getByTestId('all-params')).toHaveTextContent( | ||
/^25518447M=color%3DJJ5FUXX%26size%3D9XL&25518704M=color%3DJJ2XNXX%26size%3D9MD$/ | ||
) | ||
expect(screen.getByTestId('product-params')).toHaveTextContent(/^color=JJ2XNXX&size=9MD$/) | ||
}) | ||
|
||
test('regular product with variant', () => { | ||
const url = '/global/en-GB/product/25502228M?color=JJ0NLD0&size=9MD&pid=701642889830M' | ||
window.history.pushState({}, '', url) | ||
|
||
renderWithProviders(<MockComponent />) | ||
|
||
expect(screen.getByTestId('all-params')).toHaveTextContent( | ||
/^color=JJ0NLD0&size=9MD&pid=701642889830M$/ | ||
) | ||
expect(screen.getByTestId('product-params')).toHaveTextContent(/^$/) | ||
}) |
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.
Unlike other Chakra components,
Modal
does not accept any props you pass in. So to set the test id, usingcontainerProps
like this is one way to do it.