/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 4.7 — Global product form (`src/components/GlobalProductForm.tsx`). */ import React from 'react' import { ActivityIndicator as PaperActivityIndicator } from 'react-native-paper' import { fireEvent } from '@testing-library/react-native' import GlobalProductForm from '@/src/components/GlobalProductForm' import { renderWithProviders } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { P_CHICKEN, P_GLOBAL } from '../fixtures' const globalSku = (over: Record = {}) => ({ ...P_GLOBAL.skus[0], ...over }) const globalProduct = (over: Record = {}) => ({ ...P_GLOBAL, skus: [globalSku()], ...over, }) function renderForm(product: any = globalProduct(), isLoading = false) { const onSubmit = jest.fn() const view = renderWithProviders( , { auth: false } ) return { view, onSubmit } } describe('GlobalProductForm (AGP-01..09)', () => { beforeEach(() => resetAllMocks()) it('AGP-01: renders the global banner and read-only product details', () => { const { view } = renderForm() expect(view.getByText('Global product')).toBeTruthy() expect( view.getByText('Shared across all regions. You can edit the price & stock for your region only.') ).toBeTruthy() expect(view.getByText('Product details (read-only)')).toBeTruthy() expect(view.getAllByText('Global Paneer').length).toBeGreaterThanOrEqual(1) expect(view.getByText('Store: Freshyo Central · Type: item')).toBeTruthy() }) it('AGP-02: renders one market-data card per SKU', () => { const { view } = renderForm({ ...P_CHICKEN, isGlobal: true, skus: P_CHICKEN.skus, }) expect(view.getAllByText('Market data (your region)')).toHaveLength(2) expect(view.getByTestId('global-sku-11-price')).toBeTruthy() expect(view.getByTestId('global-sku-12-price')).toBeTruthy() }) it('AGP-03: prefills price, market price and flash price', () => { const { view } = renderForm( globalProduct({ skus: [globalSku({ isFlashAvailable: true, flashPrice: '80' })], }) ) expect(view.getByTestId('global-sku-91-price').props.value).toBe('90') expect(view.getByTestId('global-sku-91-market-price').props.value).toBe('100') expect(view.getByTestId('global-sku-91-flash-price').props.value).toBe('80') }) it('AGP-04: turning flash on reveals the flash price input and sends it', () => { const { view, onSubmit } = renderForm() expect(view.queryByTestId('global-sku-91-flash-price')).toBeNull() fireEvent.press(view.getByTestId('global-sku-91-flash')) fireEvent.changeText(view.getByTestId('global-sku-91-flash-price'), '75') fireEvent.press(view.getByTestId('global-save-button')) const [payload] = onSubmit.mock.calls[0] expect(payload[0].isFlashAvailable).toBe(true) expect(payload[0].flashPrice).toBe(75) }) it('AGP-05: stock/offer/suspend toggles are reflected in the payload', () => { const { view, onSubmit } = renderForm() fireEvent.press(view.getByTestId('global-sku-91-stock')) fireEvent.press(view.getByTestId('global-sku-91-offer')) fireEvent.press(view.getByTestId('global-sku-91-suspend')) fireEvent.press(view.getByTestId('global-save-button')) const [payload] = onSubmit.mock.calls[0] expect(payload[0].isOutOfStock).toBe(true) expect(payload[0].isOffer).toBe(true) // The toggle owns the region's suspension; the global kill-switch is untouched. expect(payload[0].isSuspendedInRegion).toBe(true) expect(payload[0].isSuspended).toBe(false) }) it('AGP-06: an empty price is sent as 0', () => { const { view, onSubmit } = renderForm() fireEvent.changeText(view.getByTestId('global-sku-91-price'), '') fireEvent.press(view.getByTestId('global-save-button')) const [payload] = onSubmit.mock.calls[0] expect(payload[0].price).toBe(0) }) it('AGP-07: an invalid number falls back to 0 / null', () => { const { view, onSubmit } = renderForm() fireEvent.changeText(view.getByTestId('global-sku-91-price'), 'abc') fireEvent.changeText(view.getByTestId('global-sku-91-market-price'), 'nope') fireEvent.press(view.getByTestId('global-save-button')) const [payload] = onSubmit.mock.calls[0] expect(payload[0].price).toBe(0) expect(payload[0].marketPrice).toBeNull() }) it('AGP-08: save submits the payload with IDs and structure preserved', () => { const { view, onSubmit } = renderForm() fireEvent.changeText(view.getByTestId('global-sku-91-price'), '99') fireEvent.press(view.getByTestId('global-save-button')) const [payload] = onSubmit.mock.calls[0] expect(payload[0]).toEqual( expect.objectContaining({ id: 91, name: null, price: 99, marketPrice: 100, images: ['paneer.jpg'], isComboOnly: false, isDeleted: false, features: [{ featureName: 'quantity', featureValue: '200g' }], comboItems: [], }) ) }) it('AGP-09: loading shows a spinner on the save button', () => { const { view } = renderForm(globalProduct(), true) expect(view.UNSAFE_getAllByType(PaperActivityIndicator).length).toBeGreaterThan(0) expect(view.getByText('Save market data')).toBeTruthy() }) })