/* 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 } from '@testing-library/react-native' import { MyButton } from 'common-ui' import EditCoupon from '@/app/(drawer)/dashboard/coupons/edit/[id]' import { renderWithProviders, flush } 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 = ReturnType['view'] function renderEdit() { __setParams({ id: '21' }) const view = renderWithProviders(, { 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')) await flush(3) } 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' }) const { view } = renderEdit() expect(view.getByText('Loading...')).toBeTruthy() }) it('ACOUPE-02: shows Coupon not found when the coupon is missing', () => { setProcedure('admin.coupon.getById', { data: couponResponse(null) }) const { view } = renderEdit() expect(view.getByText('Coupon not found')).toBeTruthy() }) it('ACOUPE-03: prefills code, discount and product targeting', () => { setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_1) }) const { view } = 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 } = renderEdit() fireEvent.changeText(codeInput(view), 'SAVE20') await submit(view) const input = lastMutationInput('admin.coupon.update') expect(input.id).toBe(21) expect(input.updates.couponCodes).toEqual(['SAVE20']) expect(procedureInvalidations('admin.coupon.getAll').length).toBeGreaterThan(0) 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 } = renderEdit() await submit(view) 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 } = renderEdit() expect(view.getByPlaceholderText('e.g., 50').props.value).toBe('50') await submit(view) 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 } = 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) 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 } = renderEdit() await submit(view) expect(lastMutationInput('admin.coupon.update')).toBeTruthy() 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 } = renderEdit() await submit(view) 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', () => { setProcedure('admin.coupon.getById', { data: couponResponse(COUPON_GLOBAL) }) const { view } = 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() }) })