freshyo/apps/admin-ui/tests/components/store-form.test.tsx
2026-10-10 15:00:50 +05:30

175 lines
4.6 KiB
TypeScript

/**
* StoreForm component (components/StoreForm.tsx) — validation, owner/product
* selection and the submit payload.
*/
import React from 'react'
import { Alert } from 'react-native'
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native'
import StoreForm, { StoreFormData } from '@/components/StoreForm'
import { setupApiMocks, resetStorage } from '../mocks/api'
import { resetStaffAuthMock } from '../mocks/staff-auth'
import { expectProcedureCalled } from '../mocks/procedures'
import { searchParamsMock } from '../mocks/router'
import { renderWithProviders } from '../helpers/render'
const CREATE_INITIAL: StoreFormData = {
name: '',
description: '',
products: [],
}
const procedures = {
'admin.staffUser.getStaff': {
staff: [
{ id: 1, name: 'shafi', role: null, permissions: [] },
{ id: 2, name: 'naveen', role: null, permissions: [] },
],
},
'admin.product.getProducts': { products: [], count: 0 },
'common.product.getAllSkusSummary': {
skus: [
{
id: 11,
productId: 1,
productName: 'Chicken',
label: 'Chicken 1kg',
images: [],
storeId: null,
price: '100',
},
],
},
}
describe('StoreForm', () => {
beforeEach(async () => {
resetStaffAuthMock()
await resetStorage()
searchParamsMock.current = {}
setupApiMocks({ procedures })
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
})
it('renders the create form', async () => {
const onSubmit = jest.fn()
renderWithProviders(
<StoreForm
mode="create"
initialValues={CREATE_INITIAL}
onSubmit={onSubmit}
isLoading={false}
/>
)
expect(screen.getByPlaceholderText('Enter store name')).toBeVisible()
expect(screen.getByPlaceholderText('Enter store description')).toBeVisible()
expect(screen.getByText('Select products')).toBeVisible()
expect(screen.getByText('Create Store')).toBeVisible()
})
it('BUG: submits even when the name is empty (validation is bypassed)', async () => {
const onSubmit = jest.fn()
renderWithProviders(
<StoreForm
mode="create"
initialValues={CREATE_INITIAL}
onSubmit={onSubmit}
isLoading={false}
/>
)
fireEvent.press(screen.getByText('Create Store'))
await act(async () => {
await Promise.resolve()
await Promise.resolve()
})
// Intended: Yup `name` is required and Formik should block this.
// Actual: the button calls the `onSubmit` prop directly, so the
// validationSchema never runs.
expect(onSubmit).toHaveBeenCalledWith(
expect.objectContaining({ name: '' })
)
})
it('submits the typed name with the selected product', async () => {
const onSubmit = jest.fn()
renderWithProviders(
<StoreForm
mode="create"
initialValues={CREATE_INITIAL}
onSubmit={onSubmit}
isLoading={false}
/>
)
fireEvent.changeText(
screen.getByPlaceholderText('Enter store name'),
'Freshyo Form'
)
fireEvent.press(screen.getByText('Select products'))
fireEvent.press(await screen.findByText('Chicken - ₹100'))
fireEvent.press(screen.getByText('Create Store'))
await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({
name: 'Freshyo Form',
description: '',
products: [11],
imageUrl: undefined,
})
)
})
it('renders edit mode with prefilled values and the update label', () => {
const onSubmit = jest.fn()
renderWithProviders(
<StoreForm
mode="edit"
initialValues={{
name: 'Existing',
description: 'Old description',
products: [],
}}
onSubmit={onSubmit}
isLoading={false}
storeId={3}
/>
)
expect(screen.getByDisplayValue('Existing')).toBeVisible()
expect(screen.getByDisplayValue('Old description')).toBeVisible()
expect(screen.getByText('Update Store')).toBeVisible()
})
it('shows the pending label when isLoading', () => {
renderWithProviders(
<StoreForm
mode="create"
initialValues={CREATE_INITIAL}
onSubmit={jest.fn()}
isLoading
/>
)
expect(screen.getByText('Creating...')).toBeVisible()
})
it('loads products', async () => {
const { fetchMock } = setupApiMocks({ procedures })
renderWithProviders(
<StoreForm
mode="create"
initialValues={CREATE_INITIAL}
onSubmit={jest.fn()}
isLoading={false}
/>
)
await waitFor(() => expectProcedureCalled(fetchMock, 'admin.product.getProducts'))
})
})