170 lines
5.5 KiB
TypeScript
170 lines
5.5 KiB
TypeScript
/**
|
|
* CouponForm component (src/components/CouponForm.tsx) — Formik validation,
|
|
* discount-type switching, coupon-code entry (incl. bulk generation) and the
|
|
* applicable-users lookup dialog.
|
|
*
|
|
* NOTE: The default form starts with an empty coupon-code slot, and Formik
|
|
* normalises `['']` to `[undefined]` before Yup runs, which makes the
|
|
* `codes-unique` test call `.trim()` on `undefined` (see the reported bug).
|
|
* Every interaction that triggers validation is therefore exercised from a
|
|
* valid `initialValues` seed; the code-entry flow is covered by typing a code
|
|
* first.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import CouponForm from '@/src/components/CouponForm'
|
|
import { setupApiMocks } from '../mocks/api'
|
|
import { lastProcedureInput } from '../mocks/procedures'
|
|
import { resetStaffAuthMock } from '../mocks/staff-auth'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const onSubmit = jest.fn()
|
|
|
|
const validInitialValues = {
|
|
couponCodes: ['SAVE10'],
|
|
isUserBased: false,
|
|
isApplyForAll: false,
|
|
targetUsers: [],
|
|
discountPercent: 10,
|
|
applicableUsers: [],
|
|
applicableProducts: [],
|
|
exclusiveApply: false,
|
|
isFirstOrderOnly: false,
|
|
isReservedCoupon: false,
|
|
}
|
|
|
|
const usersProcedure = {
|
|
users: [
|
|
{ id: 11, name: 'Alice', mobile: '9000000001' },
|
|
{ id: 12, name: 'Bob', mobile: '9000000002' },
|
|
],
|
|
}
|
|
|
|
describe('CouponForm', () => {
|
|
beforeEach(() => {
|
|
resetStaffAuthMock()
|
|
onSubmit.mockClear()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
setupApiMocks({
|
|
procedures: { 'admin.coupon.getUsersMiniInfo': usersProcedure },
|
|
})
|
|
})
|
|
|
|
it('renders the default form', () => {
|
|
renderWithProviders(<CouponForm onSubmit={onSubmit} isLoading={false} />)
|
|
|
|
expect(screen.getByText('Coupon Codes')).toBeVisible()
|
|
expect(screen.getByText('Discount Type *')).toBeVisible()
|
|
expect(screen.getByText('Is Reserved Coupon')).toBeVisible()
|
|
expect(screen.getByText('Create Coupon')).toBeVisible()
|
|
})
|
|
|
|
it('validates the form before calling onSubmit', async () => {
|
|
renderWithProviders(<CouponForm onSubmit={onSubmit} isLoading={false} />)
|
|
|
|
fireEvent.press(screen.getByText('Create Coupon'))
|
|
|
|
await waitFor(() => expect(screen.getByText('Create Coupon')).toBeVisible())
|
|
expect(onSubmit).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('uppercases the coupon code and submits the entered values', async () => {
|
|
renderWithProviders(<CouponForm onSubmit={onSubmit} isLoading={false} />)
|
|
|
|
fireEvent.changeText(screen.getByPlaceholderText('e.g., SECRET10'), 'save10')
|
|
fireEvent.press(screen.getByText('Percentage'))
|
|
fireEvent.changeText(screen.getByPlaceholderText('e.g., 10'), '10')
|
|
fireEvent.press(screen.getByText('Create Coupon'))
|
|
|
|
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1))
|
|
expect(onSubmit.mock.calls[0][0]).toMatchObject({
|
|
couponCodes: ['SAVE10'],
|
|
discountPercent: 10,
|
|
isUserBased: false,
|
|
isApplyForAll: false,
|
|
isReservedCoupon: false,
|
|
})
|
|
})
|
|
|
|
it('switches between percentage and flat discount fields', () => {
|
|
renderWithProviders(
|
|
<CouponForm
|
|
onSubmit={onSubmit}
|
|
isLoading={false}
|
|
initialValues={validInitialValues}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByPlaceholderText('e.g., 10')).toBeVisible()
|
|
expect(screen.queryByPlaceholderText('e.g., 50')).toBeNull()
|
|
|
|
fireEvent.press(screen.getByText('Flat Amount'))
|
|
|
|
expect(screen.getByPlaceholderText('e.g., 50')).toBeVisible()
|
|
expect(screen.queryByPlaceholderText('e.g., 10')).toBeNull()
|
|
})
|
|
|
|
it('disables target audience for reserved coupons', () => {
|
|
renderWithProviders(
|
|
<CouponForm
|
|
onSubmit={onSubmit}
|
|
isLoading={false}
|
|
initialValues={validInitialValues}
|
|
/>
|
|
)
|
|
|
|
expect(
|
|
screen.queryByText('Target Audience (Disabled for Reserved Coupons)')
|
|
).toBeNull()
|
|
|
|
fireEvent.press(screen.getByRole('checkbox'))
|
|
|
|
expect(
|
|
screen.getByText('Target Audience (Disabled for Reserved Coupons)')
|
|
).toBeVisible()
|
|
})
|
|
|
|
it('generates sequential codes from a pattern code', async () => {
|
|
renderWithProviders(<CouponForm onSubmit={onSubmit} isLoading={false} />)
|
|
|
|
fireEvent.changeText(screen.getByPlaceholderText('e.g., SECRET10'), 'ABC1')
|
|
fireEvent.press(screen.getByText('Add Multiple'))
|
|
|
|
expect(screen.getByText('Add Multiple Coupons')).toBeVisible()
|
|
fireEvent.changeText(screen.getByPlaceholderText('e.g., 10'), '2')
|
|
fireEvent.press(screen.getByText('Generate'))
|
|
|
|
await waitFor(() => expect(screen.getByDisplayValue('ABC2')).toBeVisible())
|
|
expect(screen.getByDisplayValue('ABC3')).toBeVisible()
|
|
})
|
|
|
|
it('loads and selects applicable users', async () => {
|
|
const { fetchMock } = setupApiMocks({
|
|
procedures: { 'admin.coupon.getUsersMiniInfo': usersProcedure },
|
|
})
|
|
renderWithProviders(
|
|
<CouponForm
|
|
onSubmit={onSubmit}
|
|
isLoading={false}
|
|
initialValues={validInitialValues}
|
|
/>
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Select users...'))
|
|
|
|
expect(await screen.findByText('9000000001 - Alice')).toBeVisible()
|
|
expect(lastProcedureInput(fetchMock, 'admin.coupon.getUsersMiniInfo')).toMatchObject({
|
|
search: '',
|
|
limit: 10,
|
|
offset: 0,
|
|
})
|
|
|
|
fireEvent.press(screen.getByText('9000000001 - Alice'))
|
|
fireEvent.press(screen.getByText('Done'))
|
|
|
|
await waitFor(() => expect(screen.getByText('1 users selected')).toBeVisible())
|
|
})
|
|
})
|