freshyo/apps/admin-ui/tests/pages/coupon-detail.test.tsx
2026-10-23 23:02:26 +05:30

152 lines
5.1 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 8.2 — Coupon detail (`tests/pages/coupon-detail.test.tsx`).
*/
import React from 'react'
import { fireEvent } from '@testing-library/react-native'
import CouponDetail from '@/app/(drawer)/dashboard/coupons/detail/[id]'
import { renderAndFlush, renderWithProviders } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { setProcedure } from '../mocks/trpc-client'
import { __setParams, router } from '../mocks/expo-router-mock'
import { COUPON_1, COUPON_EXPIRED, COUPON_GLOBAL, COUPON_INVALIDATED } from '../fixtures'
function setCoupon(coupon: any) {
setProcedure('admin.coupon.getById', { data: coupon })
}
async function renderDetail() {
__setParams({ id: '21' })
return renderAndFlush(<CouponDetail />, { auth: false })
}
describe('Coupon detail (ACOUPD-01..11)', () => {
beforeEach(() => {
resetAllMocks()
__setParams({ id: '21' })
})
it('ACOUPD-01: shows the loading state', () => {
setProcedure('admin.coupon.getById', { mode: 'pending' })
const view = renderWithProviders(<CouponDetail />, { auth: false })
expect(view.getByText('Loading coupon...')).toBeTruthy()
})
it('ACOUPD-02: shows Coupon not found and goes back', async () => {
setCoupon(null)
const view = await renderDetail()
expect(view.getByText('Coupon not found')).toBeTruthy()
fireEvent.press(view.getByTestId('coupon-detail-back'))
expect(router.back).toHaveBeenCalled()
})
it('ACOUPD-03: renders the core coupon fields', async () => {
setCoupon(COUPON_1)
const view = await renderDetail()
expect(view.getByText('Coupon Details')).toBeTruthy()
expect(view.getByText('SAVE10')).toBeTruthy()
expect(view.getByText('Active')).toBeTruthy()
expect(view.getByText('10% off')).toBeTruthy()
expect(view.getByText('₹50')).toBeTruthy()
expect(view.getByText('All users')).toBeTruthy()
expect(view.getByText('Whole cart')).toBeTruthy()
expect(view.getByText('No expiry')).toBeTruthy()
expect(view.getByText('Admin One')).toBeTruthy()
expect(view.getByText('Region #1')).toBeTruthy()
})
it('ACOUPD-04: renders flat discounts, min order, usage limit and validity', async () => {
const validTill = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString()
setCoupon({
...COUPON_1,
discountPercent: null,
flatDiscount: '50',
minOrder: '500',
maxLimitForUser: 2,
validTill,
})
const view = await renderDetail()
expect(view.getByText('₹50 off')).toBeTruthy()
expect(view.getByText('₹500')).toBeTruthy()
expect(view.queryByText('Percentage')).toBeNull()
expect(view.getByText('Flat amount')).toBeTruthy()
expect(view.getByText('Uses / User')).toBeTruthy()
expect(view.getByText('2')).toBeTruthy()
expect(view.queryByText('No expiry')).toBeNull()
})
it('ACOUPD-05: shows Expired for a past validTill', async () => {
setCoupon(COUPON_EXPIRED)
const view = await renderDetail()
expect(view.getByText('Expired')).toBeTruthy()
})
it('ACOUPD-06: shows Inactive for an invalidated coupon', async () => {
setCoupon(COUPON_INVALIDATED)
const view = await renderDetail()
expect(view.getByText('Inactive')).toBeTruthy()
})
it('ACOUPD-07: a global coupon is read-only and explains why', async () => {
setCoupon(COUPON_GLOBAL)
const view = await renderDetail()
expect(view.getByText('Global')).toBeTruthy()
expect(view.getByText('All regions')).toBeTruthy()
expect(
view.getByText('Applies to all regions — created and managed by the superadmin.')
).toBeTruthy()
expect(view.queryByTestId('coupon-detail-edit')).toBeNull()
expect(view.queryByText('Edit')).toBeNull()
})
it('ACOUPD-08: lists the targeted users for a user-based coupon', async () => {
setCoupon({
...COUPON_1,
isApplyForAll: false,
applicableUsers: [
{ id: 5, name: 'Test User', mobile: '9876543210' },
{ id: 6, name: 'Second User', mobile: '9876543211' },
],
})
const view = await renderDetail()
expect(view.getByText('2 users')).toBeTruthy()
expect(view.getByText('Test User')).toBeTruthy()
expect(view.getByText('9876543210')).toBeTruthy()
expect(view.getByText('Second User')).toBeTruthy()
expect(view.getByText('9876543211')).toBeTruthy()
})
it('ACOUPD-09: shows the number of targeted products', async () => {
setCoupon({ ...COUPON_1, skuIds: [11, 12] })
const view = await renderDetail()
expect(view.getByText('2 selected')).toBeTruthy()
})
it('ACOUPD-10: Edit opens the coupon edit route', async () => {
setCoupon(COUPON_1)
const view = await renderDetail()
fireEvent.press(view.getByTestId('coupon-detail-edit'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/coupons/edit/21')
})
it('ACOUPD-11: the header back button goes back', async () => {
setCoupon(COUPON_1)
const view = await renderDetail()
fireEvent.press(view.getByTestId('coupon-detail-back'))
expect(router.back).toHaveBeenCalled()
})
})