195 lines
6.2 KiB
TypeScript
195 lines
6.2 KiB
TypeScript
/**
|
|
* PaymentAndOrderComponent — bill, address gating and placing the order.
|
|
* Asserts user.order.placeOrder, cart clearing and the success redirect.
|
|
*/
|
|
|
|
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 { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures'
|
|
import { routerMock } from '../mocks/router'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const baseProps = {
|
|
selectedAddress: 1,
|
|
selectedSlots: { 1: 1849 },
|
|
selectedCouponIds: [],
|
|
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 PLACE_ORDER_OK = { data: [{ id: 99, isCod: true }] }
|
|
|
|
const readCart = async (key: string) => {
|
|
const raw = await StorageServiceCasual.getItem(key)
|
|
return raw ? JSON.parse(raw) : []
|
|
}
|
|
|
|
describe('PaymentAndOrderComponent', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
it('renders the bill and an enabled Place Order button', () => {
|
|
setupApiMocks({ procedures: { 'user.order.placeOrder': PLACE_ORDER_OK } })
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
expect(screen.getByText('Bill Details')).toBeVisible()
|
|
expect(screen.getByText('To Pay')).toBeVisible()
|
|
expect(screen.getByText('₹230')).toBeVisible()
|
|
expect(screen.getByTestId('place-order-button')).toBeEnabled()
|
|
})
|
|
|
|
it('blocks ordering without an address', () => {
|
|
setupApiMocks()
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent {...baseProps} selectedAddress={null} />,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
expect(screen.getByTestId('select-address-error')).toBeVisible()
|
|
expect(screen.getByTestId('place-order-button')).toBeDisabled()
|
|
})
|
|
|
|
it('places a COD order and redirects to order success, clearing the cart', 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: '' }])
|
|
)
|
|
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
fireEvent.press(screen.getByTestId('place-order-button'))
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.placeOrder'))
|
|
expect(lastProcedureInput(api.fetchMock, 'user.order.placeOrder')).toEqual({
|
|
selectedItems: [{ skuId: 2, quantity: 1, slotId: 1849 }],
|
|
addressId: 1,
|
|
paymentMethod: 'cod',
|
|
couponIds: undefined,
|
|
userNotes: '',
|
|
isFlashDelivery: false,
|
|
})
|
|
|
|
await waitFor(() =>
|
|
expect(routerMock.replace).toHaveBeenCalledWith(
|
|
'./order-success?orderId=99&totalAmount=230'
|
|
)
|
|
)
|
|
await waitFor(async () => expect(await readCart('cart_items')).toHaveLength(0))
|
|
})
|
|
|
|
it('renders the product discount and the savings banner when a coupon is applied', () => {
|
|
setupApiMocks()
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent
|
|
{...baseProps}
|
|
selectedCouponIds={[77]}
|
|
selectedCoupons={[{ id: 77, code: 'SAVE10' }]}
|
|
totalPrice={430}
|
|
discountAmount={43}
|
|
finalTotal={387}
|
|
finalTotalWithDelivery={387}
|
|
deliveryCharge={0}
|
|
/>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
expect(screen.getByText('Product Discount')).toBeVisible()
|
|
expect(screen.getByText('-₹43')).toBeVisible()
|
|
expect(screen.getByText('₹387')).toBeVisible()
|
|
// 43 discount + the 15 delivery fee the discount earned back.
|
|
expect(screen.getByText('You saved ₹58 on this order')).toBeVisible()
|
|
})
|
|
|
|
it('sends the selected coupon id with the order', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: { 'user.order.placeOrder': PLACE_ORDER_OK },
|
|
})
|
|
|
|
renderWithProviders(
|
|
<PaymentAndOrderComponent
|
|
{...baseProps}
|
|
selectedCouponIds={[77]}
|
|
selectedCoupons={[{ id: 77, code: 'SAVE10' }]}
|
|
discountAmount={43}
|
|
finalTotal={387}
|
|
finalTotalWithDelivery={387}
|
|
/>,
|
|
{ centralStores: false }
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('place-order-button'))
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.placeOrder'))
|
|
expect(lastProcedureInput(api.fetchMock, 'user.order.placeOrder')).toMatchObject({
|
|
couponIds: [77],
|
|
})
|
|
})
|
|
|
|
it('shows an error when placing the order fails', async () => {
|
|
setupApiMocks({
|
|
procedures: {
|
|
'user.order.placeOrder': () => {
|
|
throw new Error('order failed')
|
|
},
|
|
},
|
|
})
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
fireEvent.press(screen.getByTestId('place-order-button'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('Error', expect.any(String))
|
|
)
|
|
})
|
|
|
|
it('ignores a second press while the order is in flight', async () => {
|
|
let resolveOrder: (value: unknown) => void = () => {}
|
|
const api = setupApiMocks({
|
|
procedures: {
|
|
'user.order.placeOrder': () =>
|
|
new Promise((resolve) => {
|
|
resolveOrder = resolve
|
|
}),
|
|
},
|
|
})
|
|
|
|
renderWithProviders(<PaymentAndOrderComponent {...baseProps} />, {
|
|
centralStores: false,
|
|
})
|
|
|
|
const button = screen.getByTestId('place-order-button')
|
|
fireEvent.press(button)
|
|
fireEvent.press(button)
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.placeOrder'))
|
|
// The double-tap must not submit twice.
|
|
expectProcedureCalled(api.fetchMock, 'user.order.placeOrder', { times: 1 })
|
|
|
|
resolveOrder(PLACE_ORDER_OK)
|
|
})
|
|
})
|