186 lines
6.3 KiB
TypeScript
186 lines
6.3 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 8.2 — Coupon edit (`tests/pages/coupon-edit.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent, waitFor } from '@testing-library/react-native'
|
|
import { MyButton } from 'common-ui'
|
|
|
|
import EditCoupon from '@/app/(drawer)/dashboard/coupons/edit/[id]'
|
|
import { renderWithProviders, renderAndFlush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
setProcedure,
|
|
lastMutationInput,
|
|
procedureInvalidations,
|
|
} from '../mocks/trpc-client'
|
|
import { router, __setParams } from '../mocks/expo-router-mock'
|
|
import { COUPON_1, COUPON_GLOBAL } from '../fixtures'
|
|
|
|
type View = Awaited<ReturnType<typeof renderEdit>>['view']
|
|
|
|
async function renderEdit() {
|
|
__setParams({ id: '21' })
|
|
const view = await renderAndFlush(<EditCoupon />, { auth: false })
|
|
return { view }
|
|
}
|
|
|
|
function codeInput(view: View) {
|
|
return view.getByPlaceholderText('e.g., SECRET10')
|
|
}
|
|
|
|
// getById returns the coupon object itself (the page reads `data` directly).
|
|
function couponResponse(coupon: any) {
|
|
return coupon
|
|
}
|
|
|
|
async function submit(view: View) {
|
|
fireEvent.press(view.getByText('Create Coupon'))
|
|
}
|
|
|
|
describe('Coupon edit (ACOUPE-01..10)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ACOUPE-01: shows the loading state', () => {
|
|
setProcedure('admin.coupon.getById', { mode: 'pending' })
|
|
__setParams({ id: '21' })
|
|
const view = renderWithProviders(<EditCoupon />, { auth: false })
|
|
|
|
expect(view.getByText('Loading...')).toBeTruthy()
|
|
})
|
|
|
|
it('ACOUPE-02: shows Coupon not found when the coupon is missing', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(null) })
|
|
const { view } = await renderEdit()
|
|
|
|
expect(view.getByText('Coupon not found')).toBeTruthy()
|
|
})
|
|
|
|
it('ACOUPE-03: prefills code, discount and product targeting', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_1) })
|
|
const { view } = await renderEdit()
|
|
|
|
expect(codeInput(view).props.value).toBe('SAVE10')
|
|
expect(view.getByPlaceholderText('e.g., 10').props.value).toBe('10')
|
|
expect(
|
|
view.getByText('No products selected - the coupon applies to the whole cart.')
|
|
).toBeTruthy()
|
|
})
|
|
|
|
it('ACOUPE-04: saving calls update with { id, updates } and refreshes the list', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_1) })
|
|
const { view } = await renderEdit()
|
|
|
|
fireEvent.changeText(codeInput(view), 'SAVE20')
|
|
await submit(view)
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.coupon.update')).toBeTruthy()
|
|
)
|
|
|
|
const input = lastMutationInput('admin.coupon.update')
|
|
expect(input.id).toBe(21)
|
|
expect(input.updates.couponCodes).toEqual(['SAVE20'])
|
|
await waitFor(() =>
|
|
expect(procedureInvalidations('admin.coupon.getAll').length).toBeGreaterThan(0)
|
|
)
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Success',
|
|
'Coupon updated successfully',
|
|
expect.any(Array)
|
|
)
|
|
)
|
|
})
|
|
|
|
it('ACOUPE-05: a failed update shows the server message', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_1) })
|
|
setProcedure('admin.coupon.update', { mode: 'error', error: new Error('Boom') })
|
|
const { view } = await renderEdit()
|
|
|
|
await submit(view)
|
|
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'Boom'))
|
|
})
|
|
|
|
it('ACOUPE-06: flat discounts copy maxValue into the payload', async () => {
|
|
setProcedure('admin.coupon.getById', {
|
|
data: couponResponse({ ...COUPON_1, discountPercent: null, flatDiscount: '50' }),
|
|
})
|
|
const { view } = await renderEdit()
|
|
|
|
expect(view.getByPlaceholderText('e.g., 50').props.value).toBe('50')
|
|
await submit(view)
|
|
await waitFor(() => expect(lastMutationInput('admin.coupon.update')).toBeTruthy())
|
|
|
|
const updates = lastMutationInput('admin.coupon.update').updates
|
|
expect(updates.flatDiscount).toBe(50)
|
|
expect(updates.maxValue).toBe(50)
|
|
})
|
|
|
|
it('ACOUPE-07: an existing targetUser is transformed back to targetUser', async () => {
|
|
setProcedure('admin.coupon.getById', {
|
|
data: couponResponse({ ...COUPON_1, targetUser: { id: 5 } }),
|
|
})
|
|
const { view } = await renderEdit()
|
|
|
|
// The selector renders applicableUsers, so the restored targetUser is not
|
|
// shown there; the payload still carries it.
|
|
expect(view.getByText('Select users...')).toBeTruthy()
|
|
await submit(view)
|
|
await waitFor(() => expect(lastMutationInput('admin.coupon.update')).toBeTruthy())
|
|
|
|
const updates = lastMutationInput('admin.coupon.update').updates
|
|
expect(updates.targetUser).toBe(5)
|
|
expect(updates.targetUsers).toBeUndefined()
|
|
})
|
|
|
|
it('ACOUPE-08: a pending update shows the loading button', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_1) })
|
|
setProcedure('admin.coupon.update', { mode: 'pending' })
|
|
const { view } = await renderEdit()
|
|
|
|
await submit(view)
|
|
|
|
await waitFor(() => expect(lastMutationInput('admin.coupon.update')).toBeTruthy())
|
|
await waitFor(() =>
|
|
expect(view.UNSAFE_getAllByType(MyButton)[0].props.loading).toBe(true)
|
|
)
|
|
})
|
|
|
|
it('ACOUPE-09: OK on the success alert goes back', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_1) })
|
|
const { view } = await renderEdit()
|
|
|
|
await submit(view)
|
|
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Success',
|
|
'Coupon updated successfully',
|
|
expect.any(Array)
|
|
)
|
|
)
|
|
const buttons = alertSpy.mock.calls.find((call) => call[0] === 'Success')[2]
|
|
act(() => {
|
|
buttons[0].onPress()
|
|
})
|
|
expect(router.back).toHaveBeenCalled()
|
|
})
|
|
|
|
it('ACOUPE-10: a global coupon is not guarded on this direct route', async () => {
|
|
setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_GLOBAL) })
|
|
const { view } = await renderEdit()
|
|
|
|
// The route has no global-coupon guard; it renders the editable form.
|
|
expect(codeInput(view).props.value).toBe('GLOBAL10')
|
|
expect(view.getByText('Create Coupon')).toBeTruthy()
|
|
})
|
|
})
|