219 lines
7 KiB
TypeScript
219 lines
7 KiB
TypeScript
/**
|
|
* ProductForm (src/components/ProductForm.tsx) — the shared create/edit form.
|
|
* Covers render, store options, variant add/remove, validation gating and the
|
|
* submit payload.
|
|
*
|
|
* KNOWN APP BUG (src/components/ProductForm.tsx:80): `variantSignature` calls
|
|
* `a.featureValue.trim()` unguarded, so Yup validation throws whenever any
|
|
* attribute has an empty value. Triggering it from empty fields makes
|
|
* `validateForm()` reject and the form unusable. The submit tests here seed the
|
|
* form with valid `initialValues` so no empty attribute is ever validated.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import ProductForm from '@/src/components/ProductForm'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const validValues = (overrides: Record<string, unknown> = {}) => ({
|
|
name: 'Paneer',
|
|
shortDescription: 'Fresh',
|
|
longDescription: 'Very fresh',
|
|
storeId: 1,
|
|
productType: 'item' as const,
|
|
variants: [
|
|
{
|
|
id: undefined,
|
|
name: '',
|
|
price: 200,
|
|
marketPrice: null,
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOffer: false,
|
|
isComboOnly: false,
|
|
isDeleted: false,
|
|
isSuspended: false,
|
|
features: [{ featureName: 'quantity', featureValue: '1 kg' }],
|
|
comboItems: [],
|
|
},
|
|
],
|
|
...overrides,
|
|
})
|
|
|
|
const stores = [
|
|
{ id: 1, name: 'Main Store' },
|
|
{ id: 2, name: 'Second Store' },
|
|
]
|
|
|
|
describe('ProductForm', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
it('renders the fields and the seeded variant', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'common.getStoresSummary': { stores } },
|
|
})
|
|
renderWithProviders(
|
|
<ProductForm mode="create" initialValues={validValues()} onSubmit={jest.fn()} isLoading={false} />
|
|
)
|
|
|
|
expect(screen.getByTestId('product-name-input').props.value).toBe('Paneer')
|
|
expect(screen.getByTestId('variant-0-price').props.value).toBe('200')
|
|
expect(screen.getByTestId('variant-0-attr-0-name').props.value).toBe('quantity')
|
|
expect(screen.getByTestId('variant-0-attr-0-value').props.value).toBe('1 kg')
|
|
expect(screen.getByText('Create Product')).toBeVisible()
|
|
})
|
|
|
|
it('loads store options from getStoresSummary', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: { 'common.getStoresSummary': { stores } },
|
|
})
|
|
renderWithProviders(
|
|
<ProductForm mode="create" initialValues={validValues()} onSubmit={jest.fn()} isLoading={false} />
|
|
)
|
|
|
|
await waitFor(() =>
|
|
expect(api.fetchMock).toHaveBeenCalled()
|
|
)
|
|
expect(screen.getByTestId('product-store-dropdown')).toBeVisible()
|
|
})
|
|
|
|
it('shows edit-mode labels and the suspend checkbox', () => {
|
|
setupApiMocks({ procedures: { 'common.getStoresSummary': { stores } } })
|
|
renderWithProviders(
|
|
<ProductForm
|
|
mode="edit"
|
|
initialValues={validValues()}
|
|
onSubmit={jest.fn()}
|
|
isLoading={false}
|
|
existingVariantImages={[[]]}
|
|
existingVariantImageKeys={[[]]}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByText('Save Changes')).toBeVisible()
|
|
expect(screen.getByTestId('variant-0-suspend')).toBeVisible()
|
|
})
|
|
|
|
it('shows the saving label while isLoading is true', () => {
|
|
setupApiMocks({ procedures: { 'common.getStoresSummary': { stores } } })
|
|
renderWithProviders(
|
|
<ProductForm mode="edit" initialValues={validValues()} onSubmit={jest.fn()} isLoading />
|
|
)
|
|
|
|
expect(screen.getByText('Saving...')).toBeVisible()
|
|
})
|
|
|
|
it('removes a non-existing variant', () => {
|
|
const onSubmit = jest.fn()
|
|
setupApiMocks({ procedures: { 'common.getStoresSummary': { stores } } })
|
|
renderWithProviders(
|
|
<ProductForm
|
|
mode="create"
|
|
initialValues={validValues({
|
|
variants: [
|
|
{
|
|
id: undefined,
|
|
name: 'A',
|
|
price: 100,
|
|
marketPrice: null,
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOffer: false,
|
|
isComboOnly: false,
|
|
isDeleted: false,
|
|
isSuspended: false,
|
|
features: [{ featureName: 'quantity', featureValue: '1 kg' }],
|
|
comboItems: [],
|
|
},
|
|
{
|
|
id: undefined,
|
|
name: 'B',
|
|
price: 120,
|
|
marketPrice: null,
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOffer: false,
|
|
isComboOnly: false,
|
|
isDeleted: false,
|
|
isSuspended: false,
|
|
features: [{ featureName: 'quantity', featureValue: '2 kg' }],
|
|
comboItems: [],
|
|
},
|
|
],
|
|
})}
|
|
onSubmit={onSubmit}
|
|
isLoading={false}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByTestId('variant-1-price')).toBeVisible()
|
|
fireEvent.press(screen.getByTestId('variant-1-remove'))
|
|
expect(screen.queryByTestId('variant-1-price')).toBeNull()
|
|
})
|
|
|
|
it('submits a valid form with the seeded values', async () => {
|
|
const onSubmit = jest.fn()
|
|
const api = setupApiMocks({
|
|
procedures: { 'common.getStoresSummary': { stores } },
|
|
})
|
|
renderWithProviders(
|
|
<ProductForm mode="create" initialValues={validValues()} onSubmit={onSubmit} isLoading={false} />
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('create-product-button'))
|
|
|
|
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1))
|
|
const normalized = onSubmit.mock.calls[0][0]
|
|
expect(normalized.name).toBe('Paneer')
|
|
expect(normalized.variants[0].price).toBe(200)
|
|
expect(normalized.variants[0].features).toEqual([
|
|
{ featureName: 'quantity', featureValue: '1 kg' },
|
|
])
|
|
// create mode passes an empty deleted-keys list.
|
|
expect(onSubmit.mock.calls[0][2]).toEqual([])
|
|
expect(api.fetchMock).toHaveBeenCalled()
|
|
})
|
|
|
|
it('alerts instead of submitting when a price is invalid', async () => {
|
|
const onSubmit = jest.fn()
|
|
setupApiMocks({ procedures: { 'common.getStoresSummary': { stores } } })
|
|
renderWithProviders(
|
|
<ProductForm
|
|
mode="create"
|
|
initialValues={validValues({
|
|
variants: [
|
|
{
|
|
id: undefined,
|
|
name: '',
|
|
price: undefined,
|
|
marketPrice: null,
|
|
isFlashAvailable: false,
|
|
flashPrice: null,
|
|
isOffer: false,
|
|
isComboOnly: false,
|
|
isDeleted: false,
|
|
isSuspended: false,
|
|
features: [{ featureName: 'quantity', featureValue: '1 kg' }],
|
|
comboItems: [],
|
|
},
|
|
],
|
|
})}
|
|
onSubmit={onSubmit}
|
|
isLoading={false}
|
|
/>
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('create-product-button'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('Check your form', expect.any(String))
|
|
)
|
|
expect(onSubmit).not.toHaveBeenCalled()
|
|
})
|
|
})
|