214 lines
7.4 KiB
TypeScript
214 lines
7.4 KiB
TypeScript
/**
|
|
* PaymentAndOrderComponent — the bill rows and the placeOrder payload.
|
|
*
|
|
* Extends tests/components/payment-and-order.test.tsx with the parts that carry
|
|
* money: the discount row, the coupon and flash fields of the mutation, the
|
|
* COD-only payment method and the out-of-stock gating.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
import { StorageServiceCasual } from 'common-ui/src/services/StorageServiceCasual'
|
|
|
|
import PaymentAndOrderComponent from '@/components/PaymentAndOrderComponent'
|
|
import { useCentralProductStore } from '@/src/store/centralProductStore'
|
|
import { useCentralSlotStore } from '@/src/store/centralSlotStore'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import { lastProcedureInput, expectProcedureCalled } from '../mocks/procedures'
|
|
import { routerMock } from '../mocks/router'
|
|
import { Providers, renderWithProviders } from '../helpers/render'
|
|
|
|
const PLACE_ORDER_OK = { data: [{ id: 99, isCod: true }] }
|
|
|
|
const baseProps = {
|
|
selectedAddress: 1,
|
|
selectedSlots: { 1: 1849 },
|
|
selectedCouponId: null,
|
|
cartItems: [{ id: 1, skuId: 2, quantity: 1, slotId: 1849 }],
|
|
totalPrice: 215,
|
|
totalSavings: 0,
|
|
discountAmount: 0,
|
|
finalTotal: 215,
|
|
finalTotalWithDelivery: 230,
|
|
deliveryCharge: 15,
|
|
constsData: { freeDeliveryThreshold: 249, deliveryCharge: 15 },
|
|
selectedCoupons: [],
|
|
}
|
|
|
|
const readCart = async (key: string) => {
|
|
const raw = await StorageServiceCasual.getItem(key)
|
|
return raw ? JSON.parse(raw) : []
|
|
}
|
|
|
|
describe('PaymentAndOrderComponent money', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
useCentralProductStore.setState({ products: [], productsById: {} })
|
|
useCentralSlotStore.setState({ slots: [], productSlotsMap: {}, isSlotsLoaded: false })
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
it('renders the Product Discount row only when there is a discount', () => {
|
|
setupApiMocks({ procedures: { 'user.order.placeOrder': PLACE_ORDER_OK } })
|
|
|
|
const first = renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
expect(screen.queryByText('Product Discount')).toBeNull()
|
|
first.unmount()
|
|
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent
|
|
{...baseProps}
|
|
discountAmount={20}
|
|
finalTotal={195}
|
|
finalTotalWithDelivery={210}
|
|
selectedCouponId={77}
|
|
/>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
expect(screen.getByText('Product Discount')).toBeVisible()
|
|
expect(screen.getByText('-₹20')).toBeVisible()
|
|
})
|
|
|
|
it('renders the savings banner from the discount and the waived fee', () => {
|
|
setupApiMocks({ procedures: { 'user.order.placeOrder': PLACE_ORDER_OK } })
|
|
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent
|
|
{...baseProps}
|
|
discountAmount={20}
|
|
deliveryCharge={0}
|
|
totalSavings={5}
|
|
finalTotal={195}
|
|
finalTotalWithDelivery={195}
|
|
constsData={{ freeDeliveryThreshold: 249, deliveryCharge: 15 }}
|
|
/>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
// 20 discount + 5 savings + 15 waived fee = 40.
|
|
expect(screen.getByText('You saved ₹40 on this order')).toBeVisible()
|
|
})
|
|
|
|
it('offers more spending when the order is under the free-delivery threshold', () => {
|
|
setupApiMocks({ procedures: { 'user.order.placeOrder': PLACE_ORDER_OK } })
|
|
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
// 249 - 215 = 34 more for free delivery.
|
|
expect(screen.getByText('Add products worth ₹34 for free delivery')).toBeVisible()
|
|
})
|
|
|
|
it('keeps Pay Online disabled so only Cash on Delivery can be chosen', () => {
|
|
setupApiMocks({ procedures: { 'user.order.placeOrder': PLACE_ORDER_OK } })
|
|
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
const online = screen.getByText('Pay Online (Coming Soon)')
|
|
expect(online).toBeVisible()
|
|
// The online row is wrapped in a disabled touchable.
|
|
expect(screen.getByText('Cash on Delivery')).toBeVisible()
|
|
})
|
|
|
|
it('sends the coupon id and the flash flag with the order', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: { 'user.order.placeOrder': PLACE_ORDER_OK },
|
|
})
|
|
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent {...baseProps} selectedCouponId={77} />,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('place-order-button'))
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.placeOrder'))
|
|
const input = lastProcedureInput(api.fetchMock, 'user.order.placeOrder') as Record<string, unknown>
|
|
expect(input.couponId).toBe(77)
|
|
expect(input.isFlashDelivery).toBe(false)
|
|
})
|
|
|
|
it('places a flash order with null slots and clears the flash cart only', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: { 'user.order.placeOrder': PLACE_ORDER_OK },
|
|
})
|
|
await StorageServiceCasual.setItem(
|
|
'cart_items',
|
|
JSON.stringify([{ id: 1, skuId: 2, quantity: 1, slotId: 1849, addedAt: '' }])
|
|
)
|
|
await StorageServiceCasual.setItem(
|
|
'flash_cart_items',
|
|
JSON.stringify([{ id: 1, skuId: 2, quantity: 1, slotId: 0, addedAt: '' }])
|
|
)
|
|
|
|
// flashEligibleProductIds is derived from the central stores.
|
|
useCentralProductStore.setState({
|
|
products: [{ id: 2, name: 'Chicken Breast 500 g', price: 215 }] as never,
|
|
productsById: { 2: { id: 2, name: 'Chicken Breast 500 g', price: 215 } } as never,
|
|
})
|
|
useCentralSlotStore.setState({
|
|
slots: [],
|
|
isSlotsLoaded: true,
|
|
productSlotsMap: {
|
|
2: { slots: [], isOutOfStock: false, isFlashAvailable: true, isSuspended: false },
|
|
} as never,
|
|
})
|
|
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent
|
|
{...baseProps}
|
|
isFlashDelivery
|
|
cartItems={[{ id: 1, skuId: 2, quantity: 1, slotId: 0 }]}
|
|
selectedSlots={{}}
|
|
/>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('place-order-button'))
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.placeOrder'))
|
|
const input = lastProcedureInput(api.fetchMock, 'user.order.placeOrder') as Record<string, unknown>
|
|
expect(input.isFlashDelivery).toBe(true)
|
|
expect(input.selectedItems).toEqual([{ skuId: 2, quantity: 1, slotId: null }])
|
|
|
|
await waitFor(() =>
|
|
expect(routerMock.replace).toHaveBeenCalledWith(
|
|
expect.stringContaining('/flash-delivery/order-success')
|
|
)
|
|
)
|
|
await waitFor(async () => expect(await readCart('flash_cart_items')).toHaveLength(0))
|
|
// The scheduled cart is untouched by a flash order.
|
|
expect(await readCart('cart_items')).toHaveLength(1)
|
|
})
|
|
|
|
it('refuses to order when every line is out of stock', async () => {
|
|
useCentralSlotStore.setState({
|
|
slots: [],
|
|
isSlotsLoaded: true,
|
|
productSlotsMap: {
|
|
2: { slots: [], isOutOfStock: true, isFlashAvailable: true, isSuspended: false },
|
|
} as never,
|
|
})
|
|
setupApiMocks({ procedures: { 'user.order.placeOrder': PLACE_ORDER_OK } })
|
|
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
fireEvent.press(screen.getByTestId('place-order-button'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('Error', 'No valid items to order')
|
|
)
|
|
expect(routerMock.replace).not.toHaveBeenCalled()
|
|
})
|
|
})
|