445 lines
15 KiB
TypeScript
445 lines
15 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 5.1 — Prices overview
|
|
* (`tests/pages/prices-overview.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert, Image, Modal } from 'react-native'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
|
|
import PricesOverview from '@/app/(drawer)/dashboard/prices-overview/index'
|
|
import { renderAndFlush, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { PRODUCTS_RESPONSE, STORES, P_CHICKEN, P_NO_IMAGE } from '../fixtures'
|
|
|
|
async function renderPrices() {
|
|
return renderAndFlush(<PricesOverview />, { auth: 'in' })
|
|
}
|
|
|
|
type View = Awaited<ReturnType<typeof renderPrices>>
|
|
|
|
function openEdit(view: View, skuId: number) {
|
|
fireEvent.press(view.getByTestId(`edit-sku-price-${skuId}`))
|
|
}
|
|
|
|
function fillDialog(view: View, values: { price?: string; market?: string; flash?: string }) {
|
|
if (values.price !== undefined) {
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter price'), values.price)
|
|
}
|
|
if (values.market !== undefined) {
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter market price'), values.market)
|
|
}
|
|
if (values.flash !== undefined) {
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter flash price'), values.flash)
|
|
}
|
|
}
|
|
|
|
function submitDialog(view: View) {
|
|
fireEvent.press(view.getByText('Update Price'))
|
|
}
|
|
|
|
async function pressSave(view: View) {
|
|
fireEvent.press(view.getByTestId('save-prices-button'))
|
|
await flush(3)
|
|
}
|
|
|
|
describe('Prices overview (APRICE-01..30)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('APRICE-01: shows the loading copy while products or stores load', async () => {
|
|
setProcedure('admin.product.getProducts', { mode: 'pending' })
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getByText('Loading...')).toBeTruthy()
|
|
})
|
|
|
|
it('APRICE-02: flattens SKUs and shows product name, unit and prices', async () => {
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getAllByText('Chicken Breast').length).toBe(2)
|
|
expect(view.getByText('Cucumber')).toBeTruthy()
|
|
expect(view.getAllByText('₹215').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.getAllByText('₹410').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.getAllByText('₹15').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.getAllByText('500g').length).toBeGreaterThanOrEqual(2)
|
|
expect(view.getByText('1kg')).toBeTruthy()
|
|
})
|
|
|
|
it('APRICE-03: a product with no SKUs is omitted from the list', async () => {
|
|
setProcedure('admin.product.getProducts', {
|
|
data: {
|
|
products: [
|
|
P_CHICKEN,
|
|
{
|
|
id: 40,
|
|
name: 'Ghost Product',
|
|
storeId: 1,
|
|
skus: [],
|
|
},
|
|
],
|
|
count: 2,
|
|
},
|
|
})
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getAllByText('Chicken Breast').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.queryByText('Ghost Product')).toBeNull()
|
|
})
|
|
|
|
it('APRICE-04: a SKU without images uses the placeholder URI', async () => {
|
|
setProcedure('admin.product.getProducts', {
|
|
data: { products: [P_NO_IMAGE], count: 1 },
|
|
})
|
|
const view = await renderPrices()
|
|
|
|
const images = view.UNSAFE_getAllByType(Image)
|
|
const sources = images.map((image) => image.props.source?.uri)
|
|
expect(sources).toContain('https://via.placeholder.com/32x32?text=No+Image')
|
|
})
|
|
|
|
it('APRICE-05: missing market and flash prices render as N/A', async () => {
|
|
setProcedure('admin.product.getProducts', {
|
|
data: { products: [P_NO_IMAGE], count: 1 },
|
|
})
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getAllByText('N/A')).toHaveLength(2)
|
|
})
|
|
|
|
it('APRICE-06: long product names are truncated to 20 characters', async () => {
|
|
const longName = 'This Is A Very Long Product Name'
|
|
setProcedure('admin.product.getProducts', {
|
|
data: {
|
|
products: [{ ...P_CHICKEN, name: longName }],
|
|
count: 1,
|
|
},
|
|
})
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getAllByText('This Is A Very Long ...').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.queryByText(longName)).toBeNull()
|
|
})
|
|
|
|
it('APRICE-07: all stores are selected by default in the store filter', async () => {
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getByText('Freshyo Central, The Freshyo Meat House')).toBeTruthy()
|
|
})
|
|
|
|
it('APRICE-08: deselecting a store hides its SKUs', async () => {
|
|
const view = await renderPrices()
|
|
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-option-2'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-done'))
|
|
|
|
expect(view.getAllByText('Chicken Breast').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.queryByText('Global Paneer')).toBeNull()
|
|
})
|
|
|
|
it('APRICE-09: a store filter with no matching products yields an empty list', async () => {
|
|
setProcedure('admin.store.getStores', {
|
|
data: {
|
|
stores: [...STORES, { id: 3, name: 'Empty Store', isGlobal: false, productCount: 0, createdAt: null }],
|
|
count: 3,
|
|
},
|
|
})
|
|
const view = await renderPrices()
|
|
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-option-1'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-option-2'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-done'))
|
|
|
|
expect(view.queryByText('Chicken Breast')).toBeNull()
|
|
expect(view.queryByText('Global Paneer')).toBeNull()
|
|
expect(view.getByText('Save (0)')).toBeTruthy()
|
|
})
|
|
|
|
it('APRICE-10: the edit dialog is prefilled with the SKU prices', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
|
|
expect(view.getByPlaceholderText('Enter price').props.value).toBe('15')
|
|
expect(view.getByPlaceholderText('Enter market price').props.value).toBe('20')
|
|
expect(view.getByPlaceholderText('Enter flash price').props.value).toBe('')
|
|
})
|
|
|
|
it('APRICE-11: an empty, zero or negative price is rejected', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '' })
|
|
submitDialog(view)
|
|
expect(alertSpy).toHaveBeenLastCalledWith('Error', 'Please enter a valid price')
|
|
|
|
fillDialog(view, { price: '0' })
|
|
submitDialog(view)
|
|
expect(alertSpy).toHaveBeenLastCalledWith('Error', 'Please enter a valid price')
|
|
|
|
fillDialog(view, { price: '-5' })
|
|
submitDialog(view)
|
|
expect(alertSpy).toHaveBeenLastCalledWith('Error', 'Please enter a valid price')
|
|
|
|
expect(view.getByText('Save (0)')).toBeTruthy()
|
|
})
|
|
|
|
it('APRICE-12: an invalid market price is rejected', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { market: 'abc' })
|
|
submitDialog(view)
|
|
|
|
expect(alertSpy).toHaveBeenLastCalledWith('Error', 'Please enter a valid market price')
|
|
})
|
|
|
|
it('APRICE-13: an invalid flash price is rejected', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { flash: '-1' })
|
|
submitDialog(view)
|
|
|
|
expect(alertSpy).toHaveBeenLastCalledWith('Error', 'Please enter a valid flash price')
|
|
})
|
|
|
|
it('APRICE-14: empty market/flash fields become null pending values', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { market: '', flash: '' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
|
|
expect(lastMutationInput('admin.product.updateProductPrices')).toEqual({
|
|
updates: [{ skuId: 13, marketPrice: null, flashPrice: null }],
|
|
})
|
|
})
|
|
|
|
it('APRICE-15: unchanged values still create a pending change object', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
submitDialog(view)
|
|
|
|
expect(view.getByText('Save (1)')).toBeTruthy()
|
|
|
|
await pressSave(view)
|
|
expect(lastMutationInput('admin.product.updateProductPrices')).toEqual({
|
|
updates: [{ skuId: 13, flashPrice: null }],
|
|
})
|
|
})
|
|
|
|
it('APRICE-16: a pending change shows the green check indicator', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '18' })
|
|
submitDialog(view)
|
|
|
|
// The green dot is the round indicator above each SKU row; it renders a
|
|
// check glyph only when the SKU has a pending change.
|
|
const checkIcons = view.UNSAFE_getAllByProps({ name: 'check' })
|
|
expect(checkIcons.length).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('APRICE-17: the save button shows the pending change count', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 11)
|
|
fillDialog(view, { price: '300' })
|
|
submitDialog(view)
|
|
expect(view.getByText('Save (1)')).toBeTruthy()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '20' })
|
|
submitDialog(view)
|
|
expect(view.getByText('Save (2)')).toBeTruthy()
|
|
})
|
|
|
|
it('APRICE-18: save is disabled when there are no changes', async () => {
|
|
const view = await renderPrices()
|
|
|
|
expect(view.getByText('Save (0)')).toBeTruthy()
|
|
await pressSave(view)
|
|
|
|
expect(procedureMutationCalls('admin.product.updateProductPrices')).toHaveLength(0)
|
|
})
|
|
|
|
it('APRICE-19: saving sends updateProductPrices with the SKU updates', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 11)
|
|
fillDialog(view, { price: '300', market: '250' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
|
|
expect(lastMutationInput('admin.product.updateProductPrices')).toEqual({
|
|
updates: [{ skuId: 11, price: 300, marketPrice: 250, flashPrice: null }],
|
|
})
|
|
})
|
|
|
|
it('APRICE-20: multiple SKUs produce one update entry per SKU', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 11)
|
|
fillDialog(view, { price: '300' })
|
|
submitDialog(view)
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '20' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
|
|
const input = lastMutationInput('admin.product.updateProductPrices')
|
|
expect(input.updates).toHaveLength(2)
|
|
expect(input.updates.map((update: any) => update.skuId)).toEqual([11, 13])
|
|
})
|
|
|
|
it('APRICE-21: toggling only the flash checkbox sends isFlashAvailable', async () => {
|
|
const view = await renderPrices()
|
|
|
|
fireEvent.press(view.getByTestId('flash-checkbox-11'))
|
|
await pressSave(view)
|
|
|
|
expect(lastMutationInput('admin.product.updateProductPrices')).toEqual({
|
|
updates: [{ skuId: 11, isFlashAvailable: true }],
|
|
})
|
|
})
|
|
|
|
it('APRICE-22: a pending save disables the save action and keeps the spinner', async () => {
|
|
setProcedure('admin.product.updateProductPrices', { mode: 'pending' })
|
|
const view = await renderPrices()
|
|
|
|
fireEvent.press(view.getByTestId('flash-checkbox-11'))
|
|
await pressSave(view)
|
|
await flush()
|
|
|
|
expect(view.getByText('Save (1)')).toBeTruthy()
|
|
await pressSave(view)
|
|
await flush()
|
|
|
|
expect(procedureMutationCalls('admin.product.updateProductPrices')).toHaveLength(1)
|
|
})
|
|
|
|
it('APRICE-23: a successful save clears pending changes, refetches and alerts', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '18' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
await flush()
|
|
|
|
expect(view.getByText('Save (0)')).toBeTruthy()
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Success', 'Prices updated successfully'))
|
|
})
|
|
|
|
it('APRICE-24: a failed mutation alerts with the error message', async () => {
|
|
setProcedure('admin.product.updateProductPrices', {
|
|
mode: 'error',
|
|
error: new Error('boom'),
|
|
})
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '18' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
await flush()
|
|
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'Failed to update prices: boom'))
|
|
})
|
|
|
|
it('APRICE-25: closing the edit dialog resets its temporary fields', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '999' })
|
|
fireEvent(view.UNSAFE_getAllByType(Modal).find((modal) => modal.props.visible)!, 'requestClose')
|
|
|
|
expect(view.queryByPlaceholderText('Enter price')).toBeNull()
|
|
|
|
openEdit(view, 13)
|
|
expect(view.getByPlaceholderText('Enter price').props.value).toBe('15')
|
|
})
|
|
|
|
it('APRICE-26: the options menu navigates to rebalance orders', async () => {
|
|
const view = await renderPrices()
|
|
|
|
fireEvent.press(view.getByTestId('prices-options-button'))
|
|
fireEvent.press(view.getByText('Re-Balance Orders'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/rebalance-orders')
|
|
})
|
|
|
|
it('APRICE-27: pending changes are preserved across multiple dialogs', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 11)
|
|
fillDialog(view, { price: '300' })
|
|
submitDialog(view)
|
|
openEdit(view, 13)
|
|
fillDialog(view, { price: '20' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
|
|
const input = lastMutationInput('admin.product.updateProductPrices')
|
|
expect(input.updates).toEqual([
|
|
{ skuId: 11, price: 300, flashPrice: null },
|
|
{ skuId: 13, price: 20, flashPrice: null },
|
|
])
|
|
})
|
|
|
|
it('APRICE-28: switching the store filter keeps pending changes', async () => {
|
|
const view = await renderPrices()
|
|
|
|
fireEvent.press(view.getByTestId('flash-checkbox-11'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-option-1'))
|
|
fireEvent.press(view.getByTestId('store-filter-dropdown-done'))
|
|
|
|
expect(view.queryByText('Chicken Breast')).toBeNull()
|
|
expect(view.getByText('Save (1)')).toBeTruthy()
|
|
|
|
await pressSave(view)
|
|
expect(lastMutationInput('admin.product.updateProductPrices')).toEqual({
|
|
updates: [{ skuId: 11, isFlashAvailable: true }],
|
|
})
|
|
})
|
|
|
|
it('APRICE-29: the payload uses the SKU id, not the product id (regression)', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 11)
|
|
fillDialog(view, { price: '300' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
|
|
const input = lastMutationInput('admin.product.updateProductPrices')
|
|
expect(input.updates[0].skuId).toBe(11)
|
|
expect(input.updates[0].productId).toBeUndefined()
|
|
})
|
|
|
|
it('APRICE-30: an explicitly cleared market price is sent as null', async () => {
|
|
const view = await renderPrices()
|
|
|
|
openEdit(view, 13)
|
|
fillDialog(view, { market: '' })
|
|
submitDialog(view)
|
|
await pressSave(view)
|
|
|
|
const input = lastMutationInput('admin.product.updateProductPrices')
|
|
expect(input.updates[0]).toEqual({ skuId: 13, marketPrice: null, flashPrice: null })
|
|
})
|
|
})
|