/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 4.6 — Shared product form (`src/components/ProductForm.tsx`). */ import React from 'react' import { Alert } from 'react-native' import { act, fireEvent } from '@testing-library/react-native' import * as ImagePicker from 'expo-image-picker' import { BottomDropdown } from 'common-ui' import ProductForm from '@/src/components/ProductForm' import { renderWithProviders, flush } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' const variant = (over: Record = {}) => ({ id: undefined as number | undefined, name: '', price: 100, marketPrice: null, isFlashAvailable: false, flashPrice: null, isOffer: false, isComboOnly: false, isDeleted: false, isSuspendedInRegion: false, features: [{ featureName: 'quantity', featureValue: '1kg' }], comboItems: [], ...over, }) const initialValues = (over: Record = {}) => ({ name: 'Test Product', shortDescription: '', longDescription: '', storeId: 1, productType: 'item' as 'item' | 'combo', variants: [variant()], ...over, }) function renderForm( over: Record = {}, props: Record = {} ) { const onSubmit = jest.fn() const view = renderWithProviders( , { auth: false } ) return { view, onSubmit } } async function submit(view: ReturnType['view']) { fireEvent.press(view.getByTestId('create-product-button')) await flush() } describe('ProductForm (APFORM-01..22)', () => { let alertSpy: jest.SpyInstance beforeEach(() => { resetAllMocks() alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) }) afterEach(() => alertSpy?.mockRestore()) it('APFORM-01: auto-adds the quantity feature to the default variant', () => { const { view } = renderForm({ variants: [variant({ features: [] })] }) expect(view.getByTestId('variant-0-attr-0-name').props.value).toBe('quantity') }) it('APFORM-02: prepends quantity when an initial variant has other features', () => { const { view } = renderForm({ variants: [variant({ features: [{ featureName: 'size', featureValue: 'L' }] })], }) expect(view.getByTestId('variant-0-attr-0-name').props.value).toBe('quantity') expect(view.getByTestId('variant-0-attr-1-name').props.value).toBe('size') }) it('APFORM-03: existing "Quantity" case variants are normalized to lowercase on submit', async () => { const { view, onSubmit } = renderForm({ variants: [variant({ features: [{ featureName: 'Quantity', featureValue: '1kg' }] })], }) await submit(view) const [values] = onSubmit.mock.calls[0] expect(values.variants[0].features[0].featureName).toBe('quantity') }) it('APFORM-04: adding/removing a variant keeps the image arrays aligned', async () => { const { view, onSubmit } = renderForm() fireEvent.press(view.getByTestId('add-variant-button')) expect(view.getByText('Variant 2')).toBeTruthy() fireEvent.press(view.getByTestId('variant-1-remove')) expect(view.queryByText('Variant 2')).toBeNull() await submit(view) const [, images] = onSubmit.mock.calls[0] expect(images).toHaveLength(1) }) it('APFORM-05: the only new variant cannot be removed and validation still runs', async () => { const { view } = renderForm({ name: '' }) expect(view.queryByTestId('variant-0-remove')).toBeNull() await submit(view) expect(alertSpy).toHaveBeenCalledWith('Check your form', 'Product name is required') }) it('APFORM-06: removing an existing SKU marks it deleted instead of removing it', async () => { const { view, onSubmit } = renderForm( { variants: [variant({ id: 11 })] }, { mode: 'edit' } ) fireEvent.press(view.getByTestId('variant-0-remove')) expect(view.getByText('Deleted')).toBeTruthy() await submit(view) const [values] = onSubmit.mock.calls[0] expect(values.variants[0]).toEqual(expect.objectContaining({ id: 11, isDeleted: true })) }) it('APFORM-07: restoring a deleted SKU unmarks it', async () => { const { view, onSubmit } = renderForm( { variants: [variant({ id: 11 })] }, { mode: 'edit' } ) fireEvent.press(view.getByTestId('variant-0-remove')) fireEvent.press(view.getByTestId('variant-0-remove')) expect(view.queryByText('Deleted')).toBeNull() await submit(view) const [values] = onSubmit.mock.calls[0] expect(values.variants[0].isDeleted).toBe(false) }) it('APFORM-08: quantity cannot be removed unless it is duplicated', () => { const { view } = renderForm({ variants: [ variant({ features: [ { featureName: 'quantity', featureValue: '1kg' }, { featureName: 'size', featureValue: 'L' }, ], }), ], }) expect(view.queryByTestId('variant-0-attr-0-remove')).toBeNull() expect(view.getByTestId('variant-0-attr-1-remove')).toBeTruthy() fireEvent.press(view.getByTestId('variant-0-add-attr')) fireEvent.changeText(view.getByTestId('variant-0-attr-2-name'), 'quantity') fireEvent.changeText(view.getByTestId('variant-0-attr-2-value'), 'extra') expect(view.getByTestId('variant-0-attr-0-remove')).toBeTruthy() }) it('APFORM-09: duplicate quantity features fail validation', async () => { const { view } = renderForm() fireEvent.press(view.getByTestId('variant-0-add-attr')) fireEvent.changeText(view.getByTestId('variant-0-attr-1-name'), 'quantity') fireEvent.changeText(view.getByTestId('variant-0-attr-1-value'), '2kg') await submit(view) expect(alertSpy).toHaveBeenCalledWith( 'Check your form', 'Each SKU can have only one quantity feature' ) }) it('APFORM-10: price must be a positive number', async () => { const { view } = renderForm({ variants: [variant({ price: undefined })] }) await submit(view) expect(alertSpy).toHaveBeenCalledWith('Check your form', 'Price is required') }) it('APFORM-11: negative market/flash prices fail validation', async () => { const { view } = renderForm({ variants: [variant({ marketPrice: -5 })] }) await submit(view) expect(alertSpy).toHaveBeenCalledWith('Check your form', 'Market price cannot be negative') alertSpy.mockClear() fireEvent.changeText(view.getByTestId('variant-0-market-price'), '0') fireEvent.press(view.getByTestId('variant-0-flash')) fireEvent.changeText(view.getByTestId('variant-0-flash-price'), '-1') await submit(view) expect(alertSpy).toHaveBeenCalledWith('Check your form', 'Flash price cannot be negative') }) it('APFORM-12: turning flash off clears the flash price', async () => { const { view, onSubmit } = renderForm({ variants: [variant({ isFlashAvailable: true, flashPrice: 50 })], }) fireEvent.press(view.getByTestId('variant-0-flash')) expect(view.queryByTestId('variant-0-flash-price')).toBeNull() await submit(view) const [values] = onSubmit.mock.calls[0] expect(values.variants[0].isFlashAvailable).toBe(false) expect(values.variants[0].flashPrice).toBeNull() }) it('APFORM-13: the combo product type reveals the included-items section', () => { const { view } = renderForm() fireEvent.press(view.getByTestId('product-type-dropdown')) fireEvent.press(view.getByTestId('product-type-dropdown-option-combo')) expect(view.getByText('Included Items')).toBeTruthy() }) it('APFORM-14: a duplicate combo SKU is rejected and not set', async () => { const { view } = renderForm() await flush() fireEvent.press(view.getByTestId('product-type-dropdown')) fireEvent.press(view.getByTestId('product-type-dropdown-option-combo')) fireEvent.press(view.getByTestId('variant-0-combo-add')) fireEvent.press(view.getByTestId('variant-0-combo-add')) fireEvent.press(view.getByTestId('variant-0-combo-0-dropdown')) fireEvent.press(view.getByTestId('variant-0-combo-0-dropdown-option-11')) // The second dropdown disables the already-selected SKU, so drive the // defensive duplicate branch through the dropdown callback. const dropdowns = view.UNSAFE_getAllByType(BottomDropdown) const comboDropdown = dropdowns[dropdowns.length - 1] await act(async () => { comboDropdown.props.onValueChange(11) }) expect(alertSpy).toHaveBeenCalledWith('Duplicate', 'This product is already in the combo') expect(comboDropdown.props.value).toBe(0) }) it('APFORM-15: a combo item can be removed', () => { const { view } = renderForm() fireEvent.press(view.getByTestId('product-type-dropdown')) fireEvent.press(view.getByTestId('product-type-dropdown-option-combo')) fireEvent.press(view.getByTestId('variant-0-combo-add')) expect(view.getByTestId('variant-0-combo-0-dropdown')).toBeTruthy() fireEvent.press(view.getByTestId('variant-0-combo-0-remove')) expect(view.queryByTestId('variant-0-combo-0-dropdown')).toBeNull() }) it('APFORM-16: the store dropdown labels regional and global stores', async () => { const { view } = renderForm() await flush() fireEvent.press(view.getByTestId('product-store-dropdown')) expect(view.getAllByText('The Freshyo Meat House').length).toBeGreaterThanOrEqual(1) expect(view.getByText('Freshyo Central (global)')).toBeTruthy() }) it('APFORM-17: a create form with no store prefers the regional store', async () => { const { view } = renderForm({ storeId: 0 }) await flush() expect(view.getByText('The Freshyo Meat House')).toBeTruthy() expect(view.queryByText('Select store')).toBeNull() }) it('APFORM-18: combo products with items hide the combo-only toggle', () => { const { view } = renderForm() expect(view.getByTestId('variant-0-combo-only')).toBeTruthy() fireEvent.press(view.getByTestId('product-type-dropdown')) fireEvent.press(view.getByTestId('product-type-dropdown-option-combo')) fireEvent.press(view.getByTestId('variant-0-combo-add')) expect(view.queryByTestId('variant-0-combo-only')).toBeNull() }) it('APFORM-19: submitting invalid values alerts the first error message', async () => { const { view } = renderForm({ name: '', variants: [variant({ price: undefined })] }) await submit(view) expect(alertSpy).toHaveBeenCalledWith('Check your form', 'Price is required') }) it('APFORM-20: a valid submit sends normalized values, images and deleted keys', async () => { const { view, onSubmit } = renderForm({ name: ' Chicken ', variants: [ variant({ price: '100', marketPrice: '250', isFlashAvailable: true, flashPrice: '199', features: [{ featureName: 'quantity', featureValue: '1kg' }], }), ], }) await submit(view) expect(onSubmit).toHaveBeenCalledTimes(1) const [values, images, deletedKeys] = onSubmit.mock.calls[0] expect(values.name).toBe(' Chicken ') expect(values.storeId).toBe(1) expect(values.variants[0]).toEqual( expect.objectContaining({ price: 100, marketPrice: 250, flashPrice: 199, isFlashAvailable: true, }) ) expect(values.variants[0].features).toEqual([ { featureName: 'quantity', featureValue: '1kg' }, ]) expect(images).toEqual([[]]) expect(deletedKeys).toEqual([]) }) it('APFORM-21: the per-variant image uploader adds and removes images', async () => { ;(ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValueOnce({ canceled: false, assets: [{ uri: 'file:///picked.jpg', mimeType: 'image/jpeg', fileName: 'picked.jpg' }], }) const { view } = renderForm() fireEvent.press(view.getByTestId('variant-0-image-add')) await flush() expect(view.getByTestId('variant-0-image-add-remove')).toBeTruthy() fireEvent.press(view.getByTestId('variant-0-image-add-remove')) expect(view.queryByTestId('variant-0-image-add-remove')).toBeNull() }) it('APFORM-22: many features render without crashing', () => { const { view } = renderForm({ variants: [ variant({ features: Array.from({ length: 8 }, (_, index) => ({ featureName: `feature-${index}`, featureValue: `value-${index}`, })), }), ], }) // quantity was prepended, so the eight custom features occupy 1..8. expect(view.getByTestId('variant-0-attr-8-name').props.value).toBe('feature-7') }) })