237 lines
8.3 KiB
TypeScript
237 lines
8.3 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 6.2 — Edit store (`tests/pages/store-edit.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 EditStore from '@/app/(drawer)/dashboard/stores/edit'
|
|
import { renderAndFlush, renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
setProcedure,
|
|
setRawFetchHandler,
|
|
lastMutationInput,
|
|
procedureMutationCalls,
|
|
} from '../mocks/trpc-client'
|
|
import { __setParams, router } from '../mocks/expo-router-mock'
|
|
import { STORE_1, STORE_GLOBAL } from '../fixtures'
|
|
|
|
jest.mock('common-ui/src/components/ImageUploader', () => {
|
|
const ReactModule = require('react')
|
|
const { View, TouchableOpacity } = require('react-native')
|
|
const MockImageUploader = (props: any) =>
|
|
ReactModule.createElement(
|
|
View,
|
|
null,
|
|
ReactModule.createElement(TouchableOpacity, {
|
|
testID: 'mock-add-image',
|
|
onPress: props.onAddImage,
|
|
}),
|
|
...(props.existingImageUrls || []).map((url: string, index: number) =>
|
|
ReactModule.createElement(TouchableOpacity, {
|
|
key: `existing-${index}`,
|
|
testID: `mock-remove-existing-${index}`,
|
|
onPress: () => props.onRemoveExistingImage?.(url),
|
|
})
|
|
)
|
|
)
|
|
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' }],
|
|
}
|
|
|
|
async function renderEdit() {
|
|
const view = await renderAndFlush(<EditStore />, { auth: 'in' })
|
|
await flush(4)
|
|
return view
|
|
}
|
|
|
|
const PRESELECTED = [11, 12, 13, 61, 71, 81, 82]
|
|
|
|
describe('Edit store (ASTOREEDIT-01..12)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
__setParams({ id: '1' })
|
|
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('ASTOREEDIT-01: shows the loading copy', () => {
|
|
setProcedure('admin.store.getStoreById', { mode: 'pending' })
|
|
// Loading state is asserted synchronously, before the query settles.
|
|
const view = renderWithProviders(<EditStore />, { auth: 'in' })
|
|
|
|
expect(view.getByText('Loading store...')).toBeTruthy()
|
|
})
|
|
|
|
it('ASTOREEDIT-02: an unknown store shows "Store not found"', async () => {
|
|
setProcedure('admin.store.getStoreById', { data: {} })
|
|
const view = await renderEdit()
|
|
|
|
expect(view.getByText('Store not found')).toBeTruthy()
|
|
})
|
|
|
|
it('ASTOREEDIT-03: prefills name, description, image and products', async () => {
|
|
const view = await renderEdit()
|
|
|
|
expect(view.getByText('Edit Store')).toBeTruthy()
|
|
expect(view.getByTestId('store-name-input').props.value).toBe(STORE_1.name)
|
|
expect(view.getByTestId('store-description-input').props.value).toBe(STORE_1.description)
|
|
// The existing image is handed to the uploader.
|
|
expect(view.getByTestId('mock-remove-existing-0')).toBeTruthy()
|
|
expect(
|
|
view.getByText('Chicken Breast - ₹215, Chicken Breast - ₹410, Cucumber - ₹15')
|
|
).toBeTruthy()
|
|
})
|
|
|
|
it('ASTOREEDIT-04: updating sends updateStore with the form values', async () => {
|
|
setProcedure('admin.store.updateStore', {
|
|
data: { store: STORE_1, message: 'Store updated' },
|
|
})
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.changeText(view.getByTestId('store-name-input'), 'Renamed Store')
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(lastMutationInput('admin.store.updateStore')).toEqual({
|
|
id: 1,
|
|
name: 'Renamed Store',
|
|
description: STORE_1.description,
|
|
products: PRESELECTED,
|
|
imageUrl: undefined,
|
|
})
|
|
})
|
|
})
|
|
|
|
it('ASTOREEDIT-05: removing the existing image submits imageUrl undefined', async () => {
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('mock-remove-existing-0'))
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(lastMutationInput('admin.store.updateStore')).toEqual({
|
|
id: 1,
|
|
name: STORE_1.name,
|
|
description: STORE_1.description,
|
|
products: PRESELECTED,
|
|
})
|
|
})
|
|
expect(lastMutationInput('admin.store.updateStore')).not.toHaveProperty('imageUrl')
|
|
})
|
|
|
|
it('ASTOREEDIT-06: a replacement image is uploaded and its URL submitted', async () => {
|
|
;(ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValue(PICKED_IMAGE)
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('mock-add-image'))
|
|
await flush()
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(lastMutationInput('admin.store.updateStore')).toEqual(
|
|
expect.objectContaining({ id: 1, imageUrl: PRICE_URL })
|
|
)
|
|
})
|
|
expect(global.fetch).toHaveBeenCalledWith(PRICE_URL, expect.objectContaining({ method: 'PUT' }))
|
|
})
|
|
|
|
it('ASTOREEDIT-07: the SKUs belonging to the store are preselected', async () => {
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('store-products-selector'))
|
|
;[11, 12, 13].forEach((skuId) => {
|
|
expect(view.getByTestId(`store-products-selector-option-${skuId}`)).toBeTruthy()
|
|
})
|
|
})
|
|
|
|
it('ASTOREEDIT-08: a global store renders read-only and has no update action', async () => {
|
|
setProcedure('admin.store.getStoreById', {
|
|
data: { store: { ...STORE_GLOBAL, products: [] } },
|
|
})
|
|
const view = await renderEdit()
|
|
|
|
expect(view.getByText('Freshyo Central')).toBeTruthy()
|
|
expect(
|
|
view.getByText('Global store — managed by a superadmin and shared across all regions.')
|
|
).toBeTruthy()
|
|
expect(view.queryByText('Update Store')).toBeNull()
|
|
expect(view.queryByTestId('store-name-input')).toBeNull()
|
|
})
|
|
|
|
it('ASTOREEDIT-09: an update failure alerts the error', async () => {
|
|
setProcedure('admin.store.updateStore', { mode: 'error', error: new Error('nope') })
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'nope')
|
|
})
|
|
})
|
|
|
|
it('ASTOREEDIT-10: a successful update alerts and returns to the list', async () => {
|
|
setProcedure('admin.store.updateStore', {
|
|
data: { store: STORE_1, message: 'Store updated' },
|
|
})
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(alertSpy).toHaveBeenCalledWith('Success', 'Store updated')
|
|
})
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/stores')
|
|
})
|
|
|
|
it('ASTOREEDIT-11: SKUs from another store are disabled for this store', async () => {
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('store-products-selector'))
|
|
fireEvent.press(view.getByTestId('store-products-selector-option-91'))
|
|
fireEvent.press(view.getByTestId('store-products-selector-done'))
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(lastMutationInput('admin.store.updateStore')).toBeTruthy()
|
|
})
|
|
|
|
const input = lastMutationInput('admin.store.updateStore')
|
|
expect(input.products).toEqual(PRESELECTED)
|
|
expect(input.products).not.toContain(91)
|
|
})
|
|
|
|
it('ASTOREEDIT-12: the payload selects SKU ids rather than product ids', async () => {
|
|
const view = await renderEdit()
|
|
|
|
fireEvent.press(view.getByTestId('store-submit-button'))
|
|
|
|
await waitFor(() => {
|
|
expect(lastMutationInput('admin.store.updateStore')).toBeTruthy()
|
|
})
|
|
|
|
const input = lastMutationInput('admin.store.updateStore')
|
|
expect(input.products).toEqual(PRESELECTED)
|
|
expect(input.products).not.toContain(2)
|
|
expect(input.products).not.toContain(5)
|
|
})
|
|
})
|