242 lines
8.8 KiB
TypeScript
242 lines
8.8 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 6.3 — StoreForm shared component
|
|
* (`tests/components/store-form.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
import * as ImagePicker from 'expo-image-picker'
|
|
|
|
import StoreForm, { StoreFormData } from '@/components/StoreForm'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, setRawFetchHandler } from '../mocks/trpc-client'
|
|
import { STORE_1 } from '../fixtures'
|
|
|
|
jest.mock('common-ui/src/components/ImageUploader', () => {
|
|
const ReactModule = require('react')
|
|
const { View, TouchableOpacity } = require('react-native')
|
|
const MockImageUploader = (props: any) => {
|
|
const children: any[] = [
|
|
ReactModule.createElement(TouchableOpacity, {
|
|
key: 'add',
|
|
testID: 'mock-add-image',
|
|
onPress: props.onAddImage,
|
|
}),
|
|
]
|
|
;(props.existingImageUrls || []).forEach((url: string, index: number) => {
|
|
children.push(
|
|
ReactModule.createElement(TouchableOpacity, {
|
|
key: `existing-${index}`,
|
|
testID: `mock-remove-existing-${index}`,
|
|
onPress: () => props.onRemoveExistingImage?.(url),
|
|
})
|
|
)
|
|
})
|
|
;(props.images || []).forEach((image: any, index: number) => {
|
|
children.push(
|
|
ReactModule.createElement(TouchableOpacity, {
|
|
key: `new-${index}`,
|
|
testID: `mock-remove-new-${index}`,
|
|
onPress: () => props.onRemoveImage?.(image.uri),
|
|
})
|
|
)
|
|
})
|
|
return ReactModule.createElement(View, null, children)
|
|
}
|
|
return { __esModule: true, default: MockImageUploader }
|
|
})
|
|
|
|
const PRICE_URL = 'https://upload.test/meatfarmer/product-images/img-1.jpg'
|
|
const PICKED_IMAGE = {
|
|
canceled: false,
|
|
assets: [{ uri: 'file:///tmp/store.jpg', mimeType: 'image/jpeg', fileName: 'store.jpg' }],
|
|
}
|
|
|
|
const initialValues: StoreFormData = {
|
|
name: '',
|
|
description: '',
|
|
products: [],
|
|
}
|
|
|
|
function renderForm(overrides: Partial<React.ComponentProps<typeof StoreForm>> = {}) {
|
|
const onSubmit = jest.fn()
|
|
const view = renderWithProviders(
|
|
<StoreForm
|
|
mode="create"
|
|
initialValues={initialValues}
|
|
onSubmit={onSubmit}
|
|
isLoading={false}
|
|
{...overrides}
|
|
/>,
|
|
{ auth: 'in' }
|
|
)
|
|
return { view, onSubmit }
|
|
}
|
|
|
|
describe('StoreForm (ASTOREF-01..11)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
;(ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValue({ canceled: true, assets: null })
|
|
// Non-tRPC requests (file:// reads and presigned PUTs) go through the raw handler.
|
|
setRawFetchHandler(async (url: string) => {
|
|
if (String(url).startsWith('file:')) {
|
|
return { ok: true, blob: async () => ({ size: 1 }) } as any
|
|
}
|
|
return { ok: true, status: 200, text: async () => '' } as any
|
|
})
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ASTOREF-01: renders name, description, products and image fields', () => {
|
|
const { view } = renderForm()
|
|
|
|
expect(view.getByText('Store Name')).toBeTruthy()
|
|
expect(view.getByTestId('store-name-input')).toBeTruthy()
|
|
expect(view.getByText('Description')).toBeTruthy()
|
|
expect(view.getByTestId('store-description-input')).toBeTruthy()
|
|
expect(view.getByTestId('store-products-selector')).toBeTruthy()
|
|
expect(view.getByText('Store Image')).toBeTruthy()
|
|
expect(view.getByText('Create Store')).toBeTruthy()
|
|
})
|
|
|
|
it('ASTOREF-02: an empty name submits as-is (validation is bypassed)', () => {
|
|
const { view, onSubmit } = renderForm()
|
|
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ name: '' }))
|
|
expect(view.queryByText('Name is required')).toBeNull()
|
|
})
|
|
|
|
it('ASTOREF-03: an empty description is allowed', () => {
|
|
const { view, onSubmit } = renderForm()
|
|
|
|
fireEvent.changeText(view.getByTestId('store-name-input'), 'New Store')
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ description: '' }))
|
|
})
|
|
|
|
it('ASTOREF-04: product selection is optional', () => {
|
|
const { view, onSubmit } = renderForm()
|
|
|
|
fireEvent.changeText(view.getByTestId('store-name-input'), 'New Store')
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ products: [] }))
|
|
})
|
|
|
|
it('ASTOREF-05: picking an image uploads it and replaces the existing one', async () => {
|
|
const { view, onSubmit } = renderForm({
|
|
mode: 'edit',
|
|
initialValues: { ...initialValues, name: STORE_1.name, imageUrl: STORE_1.imageUrl },
|
|
})
|
|
|
|
;(ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValue(PICKED_IMAGE)
|
|
fireEvent.press(view.getByTestId('mock-add-image'))
|
|
await flush()
|
|
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
await waitFor(() => {
|
|
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ imageUrl: PRICE_URL }))
|
|
})
|
|
expect(global.fetch).toHaveBeenCalledWith(PRICE_URL, expect.objectContaining({ method: 'PUT' }))
|
|
})
|
|
|
|
it('ASTOREF-06: removing the existing image submits no imageUrl', () => {
|
|
const { view, onSubmit } = renderForm({
|
|
mode: 'edit',
|
|
initialValues: { ...initialValues, name: STORE_1.name, imageUrl: STORE_1.imageUrl },
|
|
})
|
|
|
|
fireEvent.press(view.getByTestId('mock-remove-existing-0'))
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ imageUrl: undefined }))
|
|
})
|
|
|
|
it('ASTOREF-07: an invalid name still calls submit directly (gap)', () => {
|
|
const { view, onSubmit } = renderForm()
|
|
|
|
fireEvent.changeText(view.getByTestId('store-name-input'), ' ')
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
expect(onSubmit.mock.calls[0][0].name).toBe(' ')
|
|
expect(view.queryByText('Name is required')).toBeNull()
|
|
})
|
|
|
|
it('ASTOREF-08: onSubmit receives the values, the SKU selection and imageUrl', async () => {
|
|
const { view, onSubmit } = renderForm({
|
|
mode: 'edit',
|
|
storeId: 1,
|
|
initialValues: { ...initialValues, name: STORE_1.name },
|
|
})
|
|
await flush(2)
|
|
|
|
fireEvent.changeText(view.getByTestId('store-name-input'), 'My Store')
|
|
fireEvent.changeText(view.getByTestId('store-description-input'), 'A description')
|
|
// Deselect the preselected Cucumber SKU (13).
|
|
fireEvent.press(view.getByTestId('store-products-selector'))
|
|
fireEvent.press(view.getByTestId('store-products-selector-option-13'))
|
|
fireEvent.press(view.getByTestId('store-products-selector-done'))
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
expect(onSubmit).toHaveBeenCalledWith({
|
|
name: 'My Store',
|
|
description: 'A description',
|
|
products: [11, 12, 61, 71, 81, 82],
|
|
imageUrl: undefined,
|
|
})
|
|
})
|
|
|
|
it('ASTOREF-09: create and edit label variants', () => {
|
|
const create = renderForm({ mode: 'create' })
|
|
expect(create.view.getByText('Create Store')).toBeTruthy()
|
|
create.view.unmount()
|
|
|
|
const edit = renderForm({ mode: 'edit', initialValues: { ...initialValues, name: 'A' } })
|
|
expect(edit.view.getByText('Update Store')).toBeTruthy()
|
|
edit.view.unmount()
|
|
|
|
const creating = renderForm({ mode: 'create', isLoading: true })
|
|
expect(creating.view.getByText('Creating...')).toBeTruthy()
|
|
creating.view.unmount()
|
|
|
|
const updating = renderForm({ mode: 'edit', isLoading: true })
|
|
expect(updating.view.getByText('Updating...')).toBeTruthy()
|
|
})
|
|
|
|
it('ASTOREF-10: an upload failure alerts and never calls onSubmit', async () => {
|
|
setProcedure('common.generateUploadUrls', { mode: 'error', error: new Error('upload down') })
|
|
const { view, onSubmit } = renderForm()
|
|
|
|
;(ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValue(PICKED_IMAGE)
|
|
fireEvent.press(view.getByTestId('mock-add-image'))
|
|
await flush()
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
await flush(2)
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Failed to upload image')
|
|
expect(onSubmit).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ASTOREF-11: while uploading the button shows "Uploading..."', async () => {
|
|
setProcedure('common.generateUploadUrls', { mode: 'pending' })
|
|
const { view } = renderForm()
|
|
|
|
;(ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValue(PICKED_IMAGE)
|
|
fireEvent.press(view.getByTestId('mock-add-image'))
|
|
await flush()
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
await flush(2)
|
|
|
|
expect(view.getByText('Uploading...')).toBeTruthy()
|
|
})
|
|
})
|