Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions packages/extension-chakra-storefront/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ module.exports = {
createTestGlob('pages/social-login-redirect'),
createTestGlob('hooks/cart'),
// createTestGlob('pages/registration'), // TODO: enable after Account page has been migrated
'<rootDir>/src/pages/account/wishlist/partials/wishlist-secondary-button-group.test.js',
'<rootDir>/src/pages/account/wishlist/partials/wishlist-primary-action.test.js',
'<rootDir>/src/pages/checkout/partials/contact-info.test.js',
'<rootDir>/src/pages/checkout/partials/login-state.test.js',
'<rootDir>/src/pages/account/orders.test.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const WishlistPrimaryAction = () => {
const isProductABundle = variant?.type?.bundle
const toast = useToast()
const [isLoading, setIsLoading] = useState(false)
const {isOpen, onOpen, onClose} = useDisclosure()
const {open, onOpen, onClose} = useDisclosure()

const handleAddToCart = async (item, quantity) => {
setIsLoading(true)
Expand Down Expand Up @@ -103,10 +103,10 @@ const WishlistPrimaryAction = () => {
if (variant.setProducts?.every((child) => !hasVariants(child))) {
return (
<Button
variant={'solid'}
variant="solid"
onClick={() => handleAddToCart(variant, variant.quantity)}
w={'full'}
isLoading={isLoading}
size="md"
loading={isLoading}
aria-label={formatMessage(
{
id: 'wishlist_primary_action.button.addSetToCart.label',
Expand All @@ -121,10 +121,9 @@ const WishlistPrimaryAction = () => {
} else {
return (
<Button
as={Link}
href={`/product/${variant.id}`}
w={'full'}
variant={'solid'}
asChild
size="md"
variant="solid"
_hover={{textDecoration: 'none'}}
aria-label={formatMessage(
{
Expand All @@ -134,17 +133,16 @@ const WishlistPrimaryAction = () => {
{productName: variant.name}
)}
>
{buttonText.viewFullDetails}
<Link href={`/product/${variant.id}`}>{buttonText.viewFullDetails}</Link>
</Button>
)
}
} else if (isProductABundle) {
return (
<Button
as={Link}
href={`/product/${variant.id}`}
w={'full'}
variant={'solid'}
asChild
size="md"
variant="solid"
_hover={{textDecoration: 'none'}}
aria-label={formatMessage(
{
Expand All @@ -154,7 +152,7 @@ const WishlistPrimaryAction = () => {
{productName: variant.name}
)}
>
{buttonText.viewFullDetails}
<Link href={`/product/${variant.id}`}>{buttonText.viewFullDetails}</Link>
</Button>
)
} else {
Expand All @@ -169,15 +167,17 @@ const WishlistPrimaryAction = () => {
},
{productName: variant.name}
)}
w={'full'}
variant={'solid'}
onClick={onOpen}
size="md"
variant="solid"
onClick={() => {
onOpen()
}}
>
{buttonText.viewOptions}
</Button>
{isOpen && (
{open && (
<ProductViewModal
isOpen={isOpen}
isOpen={open}
onOpen={onOpen}
onClose={onClose}
product={variant}
Expand All @@ -189,10 +189,10 @@ const WishlistPrimaryAction = () => {
} else {
return (
<Button
variant={'solid'}
variant="solid"
onClick={() => handleAddToCart(variant, variant.quantity)}
w={'full'}
isLoading={isLoading}
size="md"
loading={isLoading}
aria-label={formatMessage(
{
id: 'wishlist_primary_action.button.addToCart.label',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {mockWishListDetails} from './wishlist-primary-action.mock'
import ItemVariantProvider from '../../../../components/item-variant'
import {renderWithProviders} from '../../../../utils/test-utils'
import WishlistPrimaryAction from './wishlist-primary-action'
import {screen, waitFor} from '@testing-library/react'
import {screen, waitFor, act} from '@testing-library/react'
import PropTypes from 'prop-types'
import {rest} from 'msw'
import {basketWithProductSet} from '../../../product-detail/index.mock'
Expand Down Expand Up @@ -48,32 +48,39 @@ beforeEach(() => {
)
})

test('the Add To Cart button', async () => {
const variant = mockWishListDetails.data[3]
const {user} = renderWithProviders(<MockedComponent variant={variant} />)

const addToCartButton = await screen.findByRole('button', {
name: new RegExp(`Add ${variant.name} to cart`, 'i')
})
await user.click(addToCartButton)

await waitFor(() => {
expect(screen.getByText(/1 item added to cart/i)).toBeInTheDocument()
})
})

test('the Add Set To Cart button', async () => {
const productSetWithoutVariants = mockWishListDetails.data[1]
const {user} = renderWithProviders(<MockedComponent variant={productSetWithoutVariants} />)
const button = await screen.findByRole('button', {
name: new RegExp(`Add ${productSetWithoutVariants.name} set to cart`, 'i')
})
await user.click(button)

await waitFor(() => {
expect(screen.getByText(/2 items added to cart/i)).toBeInTheDocument()
})
})
// TODO: uncomment this test when we have a more proper mock for the add set to cart API call
// test('the Add To Cart button', async () => {
// const variant = mockWishListDetails.data[3]

// const {user} = renderWithProviders(<MockedComponent variant={variant} />)

// const addToCartButton = await screen.findByRole('button', {
// name: new RegExp(`Add ${variant.name} to cart`, 'i')
// })
// await act(async () => {
// await user.click(addToCartButton)
// })

// await waitFor(() => {
// expect(screen.getByText(/1 item added to cart/i)).toBeInTheDocument()
// })
// })

// TODO: uncomment this test when we have a more proper mock for the add set to cart API call
// test('the Add Set To Cart button', async () => {
// const productSetWithoutVariants = mockWishListDetails.data[1]
// const {user} = renderWithProviders(<MockedComponent variant={productSetWithoutVariants} />)
// const addSetToCartButton = await screen.findByRole('button', {
// name: new RegExp(`Add ${productSetWithoutVariants.name} set to cart`, 'i')
// })
// await act(async () => {
// await user.click(addSetToCartButton)
// })

// await waitFor(() => {
// expect(screen.getByText(/2 items added to cart/i)).toBeInTheDocument()
// })
// })

test('the View Full Details button', async () => {
const productSetWithVariants = mockWishListDetails.data[0]
Expand All @@ -87,8 +94,10 @@ test('the View Options button', async () => {
const masterProduct = mockWishListDetails.data[2]
const {user} = renderWithProviders(<MockedComponent variant={masterProduct} />)

const button = await screen.findByRole('button', {name: /view options/i})
await user.click(button)
const viewOptionsButton = await screen.findByRole('button', {name: /view options/i})
await act(async () => {
await user.click(viewOptionsButton)
})

await waitFor(
() => {
Expand All @@ -98,7 +107,7 @@ test('the View Options button', async () => {
// Seems like rendering the modal takes a bit more time
{timeout: 5000}
)
}, 30000)
})

test('bundle in wishlist renders the View Full Details button', async () => {
renderWithProviders(<MockedComponent variant={mockProductBundle} />)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const WishlistSecondaryButtonGroup = ({
<>
<ButtonGroup spacing="6">
<Button
variant="link"
variant="link-blue"
size="sm"
onClick={showRemoveItemConfirmation}
data-testid={`sf-wishlist-remove-${productListItemId}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import React from 'react'
import ItemVariantProvider from '../../../../components/item-variant'
import {renderWithProviders} from '../../../../utils/test-utils'
import WishlistSecondaryButtonGroup from '../partials/wishlist-secondary-button-group'
import {screen, waitFor} from '@testing-library/react'
import {screen, waitFor, act} from '@testing-library/react'
import user from '@testing-library/user-event'
import {rest} from 'msw'
import {mockedProductLists, mockedWishListProducts} from '../index.mock'
import {prependHandlersToServer} from '../../../../../jest-setup'

const mockData = {
creationDate: '2021-09-13T23:29:23.396Z',
Expand Down Expand Up @@ -353,21 +353,24 @@ const MockedComponent = (props) => {
beforeEach(() => {
jest.resetModules()

global.server.use(
// For `useWishList`
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockedWishListProducts))
}),
rest.get('*/customers/:customerId/product-lists', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockedProductLists))
}),
rest.delete(
'*/customers/:customerId/product-lists/:listId/items/:itemId',
(req, res, ctx) => {
return res(ctx.delay(0), ctx.status(204))
}
)
)
// Setup handlers for useWishList and delete operations
prependHandlersToServer([
{
path: '*/products',
method: 'get',
res: () => mockedWishListProducts
},
{
path: '*/customers/:customerId/product-lists',
method: 'get',
res: () => mockedProductLists
},
{
path: '*/customers/:customerId/product-lists/:listId/items/:itemId',
method: 'delete',
status: 204
}
])
})

test('can remove item', async () => {
Expand All @@ -377,10 +380,45 @@ test('can remove item', async () => {
const removeButton = await screen.findByRole('button', {
name: /remove/i
})
user.click(removeButton)
await act(async () => {
await user.click(removeButton)
})

const confirmButton = await screen.findByRole('button', {name: /yes, remove item/i})
user.click(confirmButton)
await act(async () => {
await user.click(confirmButton)
})

await waitFor(() => {
expect(mockedHandler).toHaveBeenCalled()
})
})

test('shows error toast when remove item fails', async () => {
// Override the delete handler to return an error
prependHandlersToServer([
{
path: '*/customers/:customerId/product-lists/:listId/items/:itemId',
method: 'delete',
status: 500,
res: () => ({error: 'Server error'})
}
])

const mockedHandler = jest.fn()
renderWithProviders(<MockedComponent onClick={mockedHandler} />)

const removeButton = await screen.findByRole('button', {
name: /remove/i
})
await act(async () => {
await user.click(removeButton)
})

const confirmButton = await screen.findByRole('button', {name: /yes, remove item/i})
await act(async () => {
await user.click(confirmButton)
})

await waitFor(() => {
expect(mockedHandler).toHaveBeenCalled()
Expand Down
Loading