120 lines
3.5 KiB
TypeScript
120 lines
3.5 KiB
TypeScript
/**
|
|
* BannerForm component (components/BannerForm.tsx) — validation, submit
|
|
* gating, submit/cancel callbacks and the existing-image (edit) display.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import BannerForm, { BannerFormData } from '@/components/BannerForm'
|
|
import { setupApiMocks } from '../mocks/api'
|
|
import { resetStaffAuthMock } from '../mocks/staff-auth'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const onSubmit = jest.fn()
|
|
const onCancel = jest.fn()
|
|
|
|
const initialValues: BannerFormData = {
|
|
name: '',
|
|
imageUrl: '',
|
|
description: '',
|
|
skuIds: [],
|
|
redirectUrl: '',
|
|
}
|
|
|
|
const blur = (placeholder: string, name: string) => {
|
|
fireEvent(screen.getByPlaceholderText(placeholder), 'blur', {
|
|
target: { name },
|
|
persist: () => {},
|
|
})
|
|
}
|
|
|
|
const renderForm = (props: Partial<React.ComponentProps<typeof BannerForm>> = {}) =>
|
|
renderWithProviders(
|
|
<BannerForm
|
|
initialValues={initialValues}
|
|
onSubmit={onSubmit}
|
|
onCancel={onCancel}
|
|
{...props}
|
|
/>
|
|
)
|
|
|
|
describe('BannerForm', () => {
|
|
beforeEach(() => {
|
|
resetStaffAuthMock()
|
|
onSubmit.mockClear()
|
|
onCancel.mockClear()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
setupApiMocks()
|
|
})
|
|
|
|
it('renders the banner fields', () => {
|
|
renderForm()
|
|
|
|
expect(screen.getByPlaceholderText('Enter banner name')).toBeVisible()
|
|
expect(screen.getByPlaceholderText('Enter banner description (optional)')).toBeVisible()
|
|
expect(screen.getByPlaceholderText('https://example.com/redirect')).toBeVisible()
|
|
expect(screen.getByText('Create Banner')).toBeVisible()
|
|
})
|
|
|
|
it('requires a banner name', async () => {
|
|
renderForm()
|
|
|
|
blur('Enter banner name', 'name')
|
|
|
|
expect(await screen.findByText('Banner name is required')).toBeVisible()
|
|
})
|
|
|
|
it('rejects a malformed redirect URL', async () => {
|
|
renderForm()
|
|
|
|
fireEvent.changeText(screen.getByPlaceholderText('https://example.com/redirect'), 'nope')
|
|
blur('https://example.com/redirect', 'redirectUrl')
|
|
|
|
expect(await screen.findByText('Please enter a valid URL')).toBeVisible()
|
|
})
|
|
|
|
it('does not submit until the form is valid and dirty', async () => {
|
|
renderForm()
|
|
|
|
fireEvent.press(screen.getByText('Create Banner'))
|
|
expect(onSubmit).not.toHaveBeenCalled()
|
|
|
|
await act(async () => {
|
|
fireEvent.changeText(screen.getByPlaceholderText('Enter banner name'), 'My Banner')
|
|
})
|
|
fireEvent.press(screen.getByText('Create Banner'))
|
|
|
|
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1))
|
|
expect(onSubmit.mock.calls[0][0]).toMatchObject({ name: 'My Banner' })
|
|
expect(onSubmit.mock.calls[0][1]).toBeUndefined()
|
|
})
|
|
|
|
it('uses the provided submit button label', () => {
|
|
renderForm({ submitButtonText: 'Update Banner' })
|
|
|
|
expect(screen.getByText('Update Banner')).toBeVisible()
|
|
})
|
|
|
|
it('shows the saving state', () => {
|
|
renderForm({ isSubmitting: true })
|
|
|
|
expect(screen.getByText('Saving...')).toBeVisible()
|
|
})
|
|
|
|
it('calls onCancel', () => {
|
|
renderForm()
|
|
|
|
fireEvent.press(screen.getByText('Cancel'))
|
|
|
|
expect(onCancel).toHaveBeenCalled()
|
|
})
|
|
|
|
it('shows the existing image as filling the single-image slot', async () => {
|
|
renderForm({ existingImageUrl: 'https://cdn.example.com/seven.jpg' })
|
|
|
|
// ImageUploader blocks the add tile once an existing image is present.
|
|
expect(await screen.findByText('Only one image allowed')).toBeVisible()
|
|
})
|
|
})
|