119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
/**
|
|
* AddToCartDialog behaviour reached from the home tag grid:
|
|
* - the flash-delivery option (only when flash is enabled AND the SKU is
|
|
* flash available) and its route,
|
|
* - the confirm button staying disabled until a delivery option is chosen,
|
|
* - reopening from the card stepper pre-loading the cart quantity.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import { fireEvent, screen, waitFor, within } from '@testing-library/react-native'
|
|
|
|
import {
|
|
FLASH_ENABLED_CONSTS,
|
|
outOfStockIds,
|
|
resetEssentialConsts,
|
|
resetStorage,
|
|
setupApiMocks,
|
|
} from '../mocks/api'
|
|
import { routerMock } from '../mocks/router'
|
|
import { useNavigationStore } from '@/src/store/navigationStore'
|
|
import {
|
|
addToCartFromTagGrid,
|
|
expandTag,
|
|
flashAvailableById,
|
|
productIdsForTag,
|
|
renderLoadedHome,
|
|
tags,
|
|
} from './render'
|
|
|
|
/** First in-stock product in a tag that is also flash available. */
|
|
const findFlashProduct = () => {
|
|
for (const tag of tags) {
|
|
for (const id of productIdsForTag(tag)) {
|
|
if (!outOfStockIds.has(id) && flashAvailableById.get(id) === true) {
|
|
return { tagId: tag.id, productId: id }
|
|
}
|
|
}
|
|
}
|
|
throw new Error('[tests] no in-stock flash product in any tag')
|
|
}
|
|
|
|
/** First in-stock product in any tag. */
|
|
const findInStockProduct = () => {
|
|
for (const tag of tags) {
|
|
for (const id of productIdsForTag(tag)) {
|
|
if (!outOfStockIds.has(id)) return { tagId: tag.id, productId: id }
|
|
}
|
|
}
|
|
throw new Error('[tests] no in-stock product in any tag')
|
|
}
|
|
|
|
const openDialog = ({ tagId, productId }: { tagId: number; productId: number }) => {
|
|
expandTag(tagId)
|
|
fireEvent.press(
|
|
within(screen.getByTestId(`tag-product-${productId}`)).getByTestId(
|
|
'add-to-cart-icon'
|
|
)
|
|
)
|
|
}
|
|
|
|
describe('Home add-to-cart dialog', () => {
|
|
beforeEach(async () => {
|
|
// A consts override is persisted by useGetEssentialConsts, so clear it
|
|
// between tests or the flash-enabled payload leaks into the next case.
|
|
await resetEssentialConsts()
|
|
await resetStorage()
|
|
useNavigationStore.setState({
|
|
isNavigatedFromHome: false,
|
|
selectedStoreId: null,
|
|
})
|
|
})
|
|
|
|
it('offers flash delivery and routes to the flash cart when enabled', async () => {
|
|
setupApiMocks({ consts: FLASH_ENABLED_CONSTS })
|
|
await renderLoadedHome()
|
|
|
|
openDialog(findFlashProduct())
|
|
|
|
expect(screen.getByText('1 hr Delivery')).toBeVisible()
|
|
fireEvent.press(screen.getByText('1 hr Delivery'))
|
|
fireEvent.press(screen.getByTestId('add-to-cart-confirm'))
|
|
|
|
expect(routerMock.push).toHaveBeenCalledWith(
|
|
'/(drawer)/(tabs)/flash-delivery/(products)'
|
|
)
|
|
})
|
|
|
|
it('keeps confirm disabled until a delivery option is chosen', async () => {
|
|
setupApiMocks()
|
|
await renderLoadedHome()
|
|
|
|
openDialog(findInStockProduct())
|
|
|
|
expect(screen.getByTestId('add-to-cart-confirm')).toBeDisabled()
|
|
})
|
|
|
|
it('pre-selects the cart quantity when reopened from the card stepper', async () => {
|
|
setupApiMocks()
|
|
await renderLoadedHome()
|
|
|
|
const target = findInStockProduct()
|
|
expandTag(target.tagId)
|
|
addToCartFromTagGrid(target.productId)
|
|
|
|
await waitFor(() => expect(screen.getByText(/1 Item/)).toBeVisible())
|
|
|
|
// The card increment opens the dialog in update mode, preloaded with the
|
|
// cart quantity (the pressed delta is discarded by ProductCard).
|
|
fireEvent.press(
|
|
within(screen.getByTestId(`tag-product-${target.productId}`)).getByTestId(
|
|
'mini-quantifier-increment'
|
|
)
|
|
)
|
|
|
|
expect(screen.getByText('Update Item')).toBeVisible()
|
|
expect(screen.getByTestId('quantifier-value')).toHaveTextContent('1')
|
|
})
|
|
})
|