183 lines
4.9 KiB
TypeScript
183 lines
4.9 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: '',
|
|
owner: 0,
|
|
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 owner')).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 owner and 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 owner'))
|
|
fireEvent.press(await screen.findByText('naveen'))
|
|
|
|
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: '',
|
|
owner: 2,
|
|
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',
|
|
owner: 1,
|
|
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 staff and products', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
renderWithProviders(
|
|
<StoreForm
|
|
mode="create"
|
|
initialValues={CREATE_INITIAL}
|
|
onSubmit={jest.fn()}
|
|
isLoading={false}
|
|
/>
|
|
)
|
|
|
|
await waitFor(() => expectProcedureCalled(fetchMock, 'admin.staffUser.getStaff'))
|
|
expectProcedureCalled(fetchMock, 'admin.product.getProducts')
|
|
})
|
|
})
|