/** * Edit product (app/(drawer)/dashboard/products/edit.tsx) — loads a product by * route id, seeds ProductForm, loads/refetches, validation and the * updateProduct payload. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import EditProduct from '@/app/(drawer)/dashboard/products/edit' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput, procCount, } from '../mocks/procedures' import { searchParamsMock } from '../mocks/router' import { refreshHooks, resetRefreshHooks } from '../mocks/refresh' import { renderWithProviders } from '../helpers/render' const product = { id: 7, name: 'Chicken', shortDescription: 'Fresh', longDescription: 'Very fresh', storeId: 1, productType: 'item', incrementStep: 1, skus: [ { id: 71, name: null, price: '250', marketPrice: '300', flashPrice: null, isFlashAvailable: false, isOffer: false, isComboOnly: false, isDeleted: false, isSuspended: false, images: ['https://assets.test/p/1.png'], imageKeys: ['product-images/1.png'], features: [{ id: 1, featureName: 'quantity', featureValue: '1 kg' }], comboItems: [], }, ], } const updateResponse = { product: { id: 7 }, message: 'Product updated successfully' } const listResponse = { products: [], count: 0 } describe('Edit product', () => { beforeEach(async () => { resetRefreshHooks() await resetStorage() searchParamsMock.current = { id: '7' } setupApiMocks() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('shows the loading state while the product is fetched', () => { setupApiMocks({ procedures: { 'admin.product.getProductById': () => new Promise(() => {}), }, }) renderWithProviders() expect(screen.getByText('Loading product...')).toBeVisible() }) it('shows "Product not found" when the query resolves without a product', async () => { setupApiMocks({ procedures: { 'admin.product.getProductById': { product: null } }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('Product not found')).toBeVisible()) }) it('seeds the form from the fetched product', async () => { const api = setupApiMocks({ procedures: { 'admin.product.getProductById': { product }, 'common.getStoresSummary': { stores: [{ id: 1, name: 'Main Store' }] }, }, }) renderWithProviders() await waitFor(() => expect(screen.getByTestId('product-name-input').props.value).toBe('Chicken')) expect(screen.getByTestId('variant-0-price').props.value).toBe('250') expect(screen.getByTestId('variant-0-attr-0-value').props.value).toBe('1 kg') expect(screen.getByText('Save Changes')).toBeVisible() await waitFor(() => expect(lastProcedureInput(api.fetchMock, 'admin.product.getProductById')).toEqual({ id: 7 }) ) }) it('routes the product id from the URL params', async () => { searchParamsMock.current = { id: '42' } const api = setupApiMocks({ procedures: { 'admin.product.getProductById': { product: { ...product, id: 42 } }, 'common.getStoresSummary': { stores: [] }, }, }) renderWithProviders() await waitFor(() => expect(lastProcedureInput(api.fetchMock, 'admin.product.getProductById')).toEqual({ id: 42 }) ) }) it('updates the product with the expected payload', async () => { const api = setupApiMocks({ procedures: { 'admin.product.getProductById': { product }, 'admin.product.updateProduct': updateResponse, 'admin.product.getProducts': listResponse, 'common.getStoresSummary': { stores: [{ id: 1, name: 'Main Store' }] }, }, }) renderWithProviders() await waitFor(() => expect(screen.getByTestId('product-name-input').props.value).toBe('Chicken')) fireEvent.press(screen.getByTestId('create-product-button')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.product.updateProduct')) expect(lastProcedureInput(api.fetchMock, 'admin.product.updateProduct')).toMatchObject({ id: 7, name: 'Chicken', storeId: 1, productType: 'item', skus: [ expect.objectContaining({ id: 71, price: 250, marketPrice: 300, features: [{ featureName: 'quantity', featureValue: '1 kg' }], }), ], }) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith('Success', 'Product updated successfully!') ) }) it('refetches the product and list after a successful update', async () => { const api = setupApiMocks({ procedures: { 'admin.product.getProductById': { product }, 'admin.product.updateProduct': updateResponse, 'admin.product.getProducts': listResponse, 'common.getStoresSummary': { stores: [{ id: 1, name: 'Main Store' }] }, }, }) renderWithProviders() await waitFor(() => expect(screen.getByTestId('product-name-input').props.value).toBe('Chicken')) const productBefore = procCount(api.fetchMock, 'admin.product.getProductById') const listBefore = procCount(api.fetchMock, 'admin.product.getProducts') fireEvent.press(screen.getByTestId('create-product-button')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.product.updateProduct')) await waitFor(() => expect(procCount(api.fetchMock, 'admin.product.getProductById')).toBeGreaterThan(productBefore) ) await waitFor(() => expect(procCount(api.fetchMock, 'admin.product.getProducts')).toBeGreaterThan(listBefore) ) }) it('refetches through useManualRefresh', async () => { const api = setupApiMocks({ procedures: { 'admin.product.getProductById': { product }, 'common.getStoresSummary': { stores: [{ id: 1, name: 'Main Store' }] }, }, }) renderWithProviders() await waitFor(() => expect(screen.getByTestId('product-name-input').props.value).toBe('Chicken')) const before = procCount(api.fetchMock, 'admin.product.getProductById') expect(refreshHooks.manualRefresh).toBeTruthy() refreshHooks.manualRefresh?.() await waitFor(() => expect(procCount(api.fetchMock, 'admin.product.getProductById')).toBeGreaterThan(before) ) }) it('shows the saving label while the update is pending', async () => { setupApiMocks({ procedures: { 'admin.product.getProductById': { product }, 'admin.product.updateProduct': () => new Promise(() => {}), 'common.getStoresSummary': { stores: [{ id: 1, name: 'Main Store' }] }, }, }) renderWithProviders() await waitFor(() => expect(screen.getByTestId('product-name-input').props.value).toBe('Chicken')) fireEvent.press(screen.getByTestId('create-product-button')) await waitFor(() => expect(screen.getByText('Saving...')).toBeVisible()) }) })