Full tests coverage
This commit is contained in:
parent
99134d8556
commit
02d782c3da
157 changed files with 1074 additions and 34670 deletions
|
|
@ -1,337 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
const { mockUseSWR } = vi.hoisted(() => ({ mockUseSWR: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('swr', () => ({ default: mockUseSWR }));
|
||||
|
||||
vi.mock('@/services/cabinet', () => ({
|
||||
getCabinetSummary: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/regimens', () => ({
|
||||
getBurnRates: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/purchases', () => ({
|
||||
listPurchases: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/refills', () => ({
|
||||
getRefillAlerts: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/cabinet-events', () => ({
|
||||
listCabinetEvents: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import DashboardPage from '../../../../src/app/(dashboard)/dashboard/page';
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true, profile: undefined });
|
||||
mockUseSWR.mockReturnValue({ data: undefined });
|
||||
});
|
||||
|
||||
describe(DashboardPage.name, () => {
|
||||
it('renders heading', () => {
|
||||
render(<DashboardPage />);
|
||||
expect(screen.getByText('Dashboard')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows loading skeleton when session loading', () => {
|
||||
render(<DashboardPage />);
|
||||
expect(screen.getByText('Dashboard')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders greeting with user name', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'John Doe' },
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText(/John/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders generic greeting without profile', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, profile: null });
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText(/there/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders cabinet items that are in active regimens', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
const summaryData = {
|
||||
data: [
|
||||
{ medicineId: '1', medicineName: 'Aspirin', totalQuantity: 50, unit: 'tablets' },
|
||||
{ medicineId: '2', medicineName: 'Ibuprofen', totalQuantity: 100, unit: 'tablets' },
|
||||
],
|
||||
};
|
||||
|
||||
const burnRateData = {
|
||||
data: [
|
||||
{ medicineId: '1', medicineName: 'Aspirin', daysUntilEmpty: 10 },
|
||||
],
|
||||
};
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: summaryData };
|
||||
if (key.includes('burn-rates')) return { data: burnRateData };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [] } };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Aspirin')).toBeInTheDocument();
|
||||
// Ibuprofen should NOT be in the document because it has no burn rate
|
||||
expect(screen.queryByText('Ibuprofen')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty states when no data', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: { data: [] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [] } };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('No active medicines in regimens.')).toBeInTheDocument();
|
||||
expect(screen.getByText('No alerts — all stocked.')).toBeInTheDocument();
|
||||
expect(screen.getByText('No pending orders.')).toBeInTheDocument();
|
||||
expect(screen.getByText('No recent activity.')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows refill alerts with days left', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
const refillData = {
|
||||
data: [{ medicineId: 'm1', medicineName: 'Vitamin C', daysUntilEmpty: 5 }],
|
||||
};
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: { data: [{ medicineId: '1' }] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts')) return { data: refillData };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Vitamin C')).toBeInTheDocument();
|
||||
expect(screen.getByText('5d')).toBeInTheDocument();
|
||||
expect(screen.getByText(/critically low/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows pending purchases', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
const purchaseData = {
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
storeName: 'Pharmacy Plus',
|
||||
items: [{ name: 'A' }, { name: 'B' }],
|
||||
status: 'ordered',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: { data: [] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [] } };
|
||||
if (key.includes('purchases')) return { data: purchaseData };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Pharmacy Plus')).toBeInTheDocument();
|
||||
expect(screen.getByText('2 items')).toBeInTheDocument();
|
||||
expect(screen.getByText('ordered')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows recent events', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
const eventData = {
|
||||
data: [
|
||||
{
|
||||
_id: 'e1',
|
||||
eventType: 'consumed',
|
||||
medicineName: 'Aspirin',
|
||||
createdAt: '2026-01-15T10:00:00.000Z',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: { data: [] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [] } };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: eventData };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('consumed')).toBeInTheDocument();
|
||||
expect(screen.getByText('Aspirin')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows "good shape" message when no critical alerts', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: { data: [{ medicineId: '1' }] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [{ daysUntilEmpty: 20 }] } };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Your cabinet is in good shape.')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows stat badges with summary data', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary'))
|
||||
return { data: { data: [{ medicineId: '1' }, { medicineId: '2' }, { medicineId: '3' }] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts'))
|
||||
return { data: { data: [{ daysUntilEmpty: 5 }, { daysUntilEmpty: 3 }] } };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Total medicines')).toBeInTheDocument();
|
||||
expect(screen.getByText('3')).toBeInTheDocument();
|
||||
expect(screen.getAllByText('Running low')).toHaveLength(2);
|
||||
expect(screen.getByText('2')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles store without name', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
const purchaseData = {
|
||||
data: [{ _id: 'p1', storeName: undefined, items: [{ name: 'A' }], status: 'ordered' }],
|
||||
};
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: { data: [] } };
|
||||
if (key.includes('burn-rates')) return { data: { data: [] } };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [] } };
|
||||
if (key.includes('purchases')) return { data: purchaseData };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Unknown store')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles cabinet item with no name', () => {
|
||||
mockUseApi.mockReturnValue({
|
||||
householdId: 'hh1',
|
||||
isLoading: false,
|
||||
profile: { displayName: 'Jane' },
|
||||
});
|
||||
|
||||
const summaryData = {
|
||||
data: [{ medicineId: 'c1', medicineName: undefined, totalQuantity: 10, unit: 'pills' }],
|
||||
};
|
||||
|
||||
const burnRateData = {
|
||||
data: [{ medicineId: 'c1', medicineName: undefined, daysUntilEmpty: 5 }],
|
||||
};
|
||||
|
||||
mockUseSWR.mockImplementation((key: string) => {
|
||||
if (!key) return { data: undefined };
|
||||
if (key.includes('cabinet-summary')) return { data: summaryData };
|
||||
if (key.includes('burn-rates')) return { data: burnRateData };
|
||||
if (key.includes('refill-alerts')) return { data: { data: [] } };
|
||||
if (key.includes('purchases')) return { data: { data: [] } };
|
||||
if (key.includes('cabinet-events')) return { data: { data: [] } };
|
||||
return { data: undefined };
|
||||
});
|
||||
|
||||
render(<DashboardPage />);
|
||||
|
||||
expect(screen.getByText('Unknown')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,397 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
const { mockRecordPrice, mockGetPriceHistory, mockCompareStores } = vi.hoisted(() => ({
|
||||
mockRecordPrice: vi.fn(),
|
||||
mockGetPriceHistory: vi.fn(),
|
||||
mockCompareStores: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListMedicines, mockListMedicineProducts } = vi.hoisted(() => ({
|
||||
mockListMedicines: vi.fn(),
|
||||
mockListMedicineProducts: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListStores } = vi.hoisted(() => ({ mockListStores: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('@/services/medicine-prices', () => ({
|
||||
recordPrice: mockRecordPrice,
|
||||
getPriceHistory: mockGetPriceHistory,
|
||||
compareStores: mockCompareStores,
|
||||
}));
|
||||
vi.mock('@/services/medicines', () => ({
|
||||
listMedicines: mockListMedicines,
|
||||
listMedicineProducts: mockListMedicineProducts,
|
||||
}));
|
||||
vi.mock('@/services/stores', () => ({ listStores: mockListStores }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
|
||||
import MedicinePricesPage from '../../../../src/app/(dashboard)/medicine-prices/page';
|
||||
|
||||
const emptyResponse = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
mockListStores.mockResolvedValue(emptyResponse);
|
||||
mockGetPriceHistory.mockResolvedValue(emptyResponse);
|
||||
mockCompareStores.mockResolvedValue({ data: [] });
|
||||
});
|
||||
|
||||
describe('MedicinePricesPage', () => {
|
||||
it('shows loading skeleton when session loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<MedicinePricesPage />);
|
||||
expect(screen.getByText('Medicine Prices')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Record Price')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<MedicinePricesPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders Medicine Prices heading when householdId exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<MedicinePricesPage />);
|
||||
expect(screen.getByText('Medicine Prices')).toBeInTheDocument();
|
||||
expect(screen.getByText('Record Price')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty prompt to select a medicine', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<MedicinePricesPage />);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('Select a medicine to view price history.')).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('loads price history when medicine selected', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockGetPriceHistory.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'pr-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
storeId: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
price: 15.99,
|
||||
pricePerUnit: 0.18,
|
||||
currency: 'USD',
|
||||
quantity: 90,
|
||||
unit: 'tablet',
|
||||
date: '2026-01-01T00:00:00.000Z',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Select a medicine'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockGetPriceHistory).toHaveBeenCalledWith('hh1', 'med-1', expect.any(Object)),
|
||||
);
|
||||
// Wait for price records to render
|
||||
await waitFor(() => expect(screen.getByText('Walgreens')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows validation error in form when required fields missing', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await userEvent.click(screen.getByText('Record Price'));
|
||||
await waitFor(() => screen.getByRole('button', { name: 'Record Price', hidden: false }));
|
||||
|
||||
// The submit button inside the form also has text 'Record Price'
|
||||
const submitBtn = screen
|
||||
.getAllByRole('button', { name: 'Record Price' })
|
||||
.find((b) => b.getAttribute('type') === 'submit');
|
||||
if (submitBtn) {
|
||||
fireEvent.submit(submitBtn.closest('form')!);
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByText('Please select a medicine, a product, and a store.'),
|
||||
).toBeInTheDocument(),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('shows error when price history fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockGetPriceHistory.mockRejectedValue(new Error('History load failed'));
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Select a medicine'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('History load failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('records a price successfully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'st-1',
|
||||
name: 'Walgreens',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [{ _id: 'prod-1', brand: 'Generic', packageSize: 90, packageUnit: 'tablet' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockRecordPrice.mockResolvedValue({});
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Price'));
|
||||
await userEvent.click(screen.getByText('Record Price'));
|
||||
|
||||
await waitFor(() => screen.getByText('Record Price', { selector: 'h2' }));
|
||||
|
||||
// Select store
|
||||
fireEvent.change(screen.getByDisplayValue('Select store'), { target: { value: 'st-1' } });
|
||||
// Select medicine
|
||||
fireEvent.change(screen.getByDisplayValue('Select medicine'), { target: { value: 'med-1' } });
|
||||
|
||||
// Wait for products to load and select product
|
||||
await waitFor(() => screen.getByText(/Generic.*90/));
|
||||
fireEvent.change(screen.getByDisplayValue('Select product'), { target: { value: 'prod-1' } });
|
||||
|
||||
// Submit form
|
||||
fireEvent.submit(
|
||||
screen.getByRole('button', { name: 'Record Price', hidden: true }).closest('form')!,
|
||||
);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockRecordPrice).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ medicineId: 'med-1' }),
|
||||
),
|
||||
);
|
||||
// Form should close after success
|
||||
await waitFor(() =>
|
||||
expect(screen.queryByText('Record Price', { selector: 'h2' })).not.toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows Load more button in price history', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockGetPriceHistory.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'pr-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
storeId: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
price: 15.99,
|
||||
pricePerUnit: 0.18,
|
||||
currency: 'USD',
|
||||
quantity: 90,
|
||||
unit: 'tablet',
|
||||
date: '2026-01-01T00:00:00.000Z',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: 'cur1', hasMore: true },
|
||||
});
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Select a medicine'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Load more')).toBeInTheDocument());
|
||||
await userEvent.click(screen.getByText('Load more'));
|
||||
|
||||
expect(mockGetPriceHistory).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('shows store filter when medicine selected and changes it', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'st-1',
|
||||
name: 'Walgreens',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Select a medicine'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('All stores'));
|
||||
fireEvent.change(screen.getByDisplayValue('All stores'), { target: { value: 'st-1' } });
|
||||
|
||||
await waitFor(() => expect(mockGetPriceHistory).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('dismisses price history error', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockGetPriceHistory.mockRejectedValue(new Error('History load failed'));
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Select a medicine'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => screen.getByText('History load failed'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('History load failed')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows store comparison table', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCompareStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
storeId: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
latestPrice: 12.99,
|
||||
latestPricePerUnit: 0.14,
|
||||
currency: 'USD',
|
||||
date: '2026-01-01T00:00:00.000Z',
|
||||
isInsurancePrice: false,
|
||||
},
|
||||
{
|
||||
storeId: 'st-2',
|
||||
storeName: 'CVS',
|
||||
latestPrice: 14.99,
|
||||
latestPricePerUnit: 0.17,
|
||||
currency: 'USD',
|
||||
date: '2026-01-01T00:00:00.000Z',
|
||||
isInsurancePrice: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Select a medicine'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Store comparison')).toBeInTheDocument());
|
||||
expect(screen.getByText('Walgreens')).toBeInTheDocument();
|
||||
// First row (cheapest) should be highlighted green
|
||||
expect(screen.getByText('cheapest')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes price, currency, quantity, and unit fields in form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await userEvent.click(screen.getByText('Record Price'));
|
||||
await waitFor(() => screen.getByPlaceholderText('9.99'));
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('9.99'), { target: { value: '15.99' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('USD'), { target: { value: 'EUR' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('90'), { target: { value: '30' } });
|
||||
fireEvent.change(screen.getByDisplayValue('tablet'), { target: { value: 'capsule' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('9.99')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes medicine search, notes and insurance price fields in form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
|
||||
render(<MedicinePricesPage />);
|
||||
|
||||
await userEvent.click(screen.getByText('Record Price'));
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Search medicines...'), {
|
||||
target: { value: 'Met' },
|
||||
});
|
||||
|
||||
// Notes field (no placeholder, but maxLength 1000)
|
||||
const notesInput = document.querySelector('input[maxLength="1000"]') as HTMLElement;
|
||||
if (notesInput) {
|
||||
fireEvent.change(notesInput, { target: { value: 'Some notes' } });
|
||||
}
|
||||
|
||||
// Insurance price checkbox
|
||||
fireEvent.click(screen.getByLabelText('Insurance price'));
|
||||
expect(screen.getByLabelText('Insurance price')).toBeChecked();
|
||||
});
|
||||
|
||||
it('toggles Record Price form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<MedicinePricesPage />);
|
||||
await userEvent.click(screen.getByText('Record Price'));
|
||||
// Both the header toggle and the form show "Cancel" when open
|
||||
expect(screen.getAllByRole('button', { name: 'Cancel' }).length).toBeGreaterThan(0);
|
||||
await userEvent.click(screen.getAllByRole('button', { name: 'Cancel' })[0]);
|
||||
expect(screen.queryByRole('button', { name: 'Cancel' })).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,306 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
|
||||
const { mockListCabinetEvents, mockGetSpendingSummary } = vi.hoisted(() => ({
|
||||
mockListCabinetEvents: vi.fn(),
|
||||
mockGetSpendingSummary: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListMedicines } = vi.hoisted(() => ({ mockListMedicines: vi.fn() }));
|
||||
|
||||
vi.mock('@/services/cabinet-events', () => ({
|
||||
listCabinetEvents: mockListCabinetEvents,
|
||||
getSpendingSummary: mockGetSpendingSummary,
|
||||
}));
|
||||
|
||||
vi.mock('@/services/medicines', () => ({ listMedicines: mockListMedicines }));
|
||||
|
||||
import { ActivityTab } from '../../../../src/app/(dashboard)/medicines/ActivityTab';
|
||||
|
||||
const emptyMeds = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptyEvents = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptySummary = {
|
||||
totalSpent: 0,
|
||||
currency: null,
|
||||
byMedicine: [],
|
||||
byPeriod: [],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockListMedicines.mockResolvedValue(emptyMeds);
|
||||
mockListCabinetEvents.mockResolvedValue(emptyEvents);
|
||||
mockGetSpendingSummary.mockResolvedValue(emptySummary);
|
||||
});
|
||||
|
||||
describe('ActivityTab', () => {
|
||||
it('renders Spending Summary and Cabinet Activity sections', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Spending Summary')).toBeInTheDocument());
|
||||
expect(screen.getByText('Cabinet Activity')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('fetches spending summary on mount', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockGetSpendingSummary).toHaveBeenCalledWith('hh1', expect.any(Object)),
|
||||
);
|
||||
});
|
||||
|
||||
it('fetches events on mount', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockListCabinetEvents).toHaveBeenCalledWith('hh1', expect.any(Object)),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows empty state when no events', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('No events found for the selected filters.')).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows error when events fail to load', async () => {
|
||||
mockListCabinetEvents.mockRejectedValue(new Error('Events error'));
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Events error')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when spending summary fails', async () => {
|
||||
mockGetSpendingSummary.mockRejectedValue(new Error('Spending error'));
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Spending error')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('renders event entries', async () => {
|
||||
mockListCabinetEvents.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'ev-1',
|
||||
eventType: 'purchased',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantity: 10,
|
||||
quantityBefore: 0,
|
||||
quantityAfter: 10,
|
||||
unit: 'tablet',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows spending summary with data', async () => {
|
||||
mockGetSpendingSummary.mockResolvedValue({
|
||||
totalSpent: 125.5,
|
||||
currency: 'USD',
|
||||
byMedicine: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
totalSpent: 125.5,
|
||||
purchaseCount: 2,
|
||||
avgUnitPrice: 0.69,
|
||||
},
|
||||
],
|
||||
byPeriod: [{ period: '2026-01', totalSpent: 125.5 }],
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getAllByText(/125.50/).length).toBeGreaterThan(0));
|
||||
expect(screen.getByText('Metformin')).toBeInTheDocument();
|
||||
expect(screen.getByText('By medicine')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows no purchase data message when total is zero', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('No purchase data found for this period.')).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('filters events by event type', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(mockListCabinetEvents).toHaveBeenCalledTimes(1));
|
||||
fireEvent.change(screen.getByDisplayValue('All event types'), {
|
||||
target: { value: 'purchased' },
|
||||
});
|
||||
|
||||
await waitFor(() => expect(mockListCabinetEvents).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('filters events by medicine', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [{ _id: 'med-1', name: 'Metformin' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getAllByDisplayValue('All medicines').length).toBeGreaterThan(1),
|
||||
);
|
||||
const allMedSelects = screen.getAllByDisplayValue('All medicines');
|
||||
// The last select is the cabinet events medicine filter
|
||||
fireEvent.change(allMedSelects[allMedSelects.length - 1]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => expect(mockListCabinetEvents).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('shows clear filters button when filter applied and clears them', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(mockListCabinetEvents).toHaveBeenCalled());
|
||||
fireEvent.change(screen.getByDisplayValue('All event types'), {
|
||||
target: { value: 'purchased' },
|
||||
});
|
||||
|
||||
await waitFor(() => screen.getByText('Clear filters'));
|
||||
await userEvent.click(screen.getByText('Clear filters'));
|
||||
|
||||
expect(screen.queryByText('Clear filters')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('dismisses cabinet activity error', async () => {
|
||||
mockListCabinetEvents.mockRejectedValue(new Error('Events error'));
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Events error'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('Events error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes spending summary period and medicine filters', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [{ _id: 'med-1', name: 'Metformin' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(mockGetSpendingSummary).toHaveBeenCalledTimes(1));
|
||||
|
||||
// Change period
|
||||
fireEvent.change(screen.getByDisplayValue('This month'), { target: { value: 'quarter' } });
|
||||
|
||||
await waitFor(() => expect(mockGetSpendingSummary).toHaveBeenCalledTimes(2));
|
||||
|
||||
// Change medicine filter in spending summary (first "All medicines" select)
|
||||
const medSelects = screen.getAllByDisplayValue('All medicines');
|
||||
fireEvent.change(medSelects[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
await waitFor(() => expect(mockGetSpendingSummary).toHaveBeenCalledTimes(3));
|
||||
});
|
||||
|
||||
it('changes start and end date filters', async () => {
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(mockListCabinetEvents).toHaveBeenCalledTimes(1));
|
||||
|
||||
const dateInputs = document.querySelectorAll('input[type="date"]');
|
||||
fireEvent.change(dateInputs[0]!, { target: { value: '2026-01-01' } });
|
||||
fireEvent.change(dateInputs[1]!, { target: { value: '2026-03-31' } });
|
||||
|
||||
await waitFor(() => expect(mockListCabinetEvents).toHaveBeenCalledTimes(3));
|
||||
});
|
||||
|
||||
it('loads more events when Load more is clicked', async () => {
|
||||
mockListCabinetEvents.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'ev-1',
|
||||
eventType: 'purchased',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantity: 10,
|
||||
quantityBefore: 0,
|
||||
quantityAfter: 10,
|
||||
unit: 'tablet',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: 'cursor-1', hasMore: true },
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Load more'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Load more' }));
|
||||
|
||||
expect(mockListCabinetEvents).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('renders event with negative quantity (red dot)', async () => {
|
||||
mockListCabinetEvents.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'ev-1',
|
||||
eventType: 'taken',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantity: -1,
|
||||
quantityBefore: 10,
|
||||
quantityAfter: 9,
|
||||
unit: 'tablet',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('renders event with reason, storeName, totalPrice, and notes', async () => {
|
||||
mockListCabinetEvents.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'ev-1',
|
||||
eventType: 'purchased',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantity: 30,
|
||||
quantityBefore: 0,
|
||||
quantityAfter: 30,
|
||||
unit: 'tablet',
|
||||
reason: 'Monthly refill',
|
||||
storeName: 'Pharmacy',
|
||||
totalPrice: 25.5,
|
||||
currency: 'USD',
|
||||
notes: 'On sale',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ActivityTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
expect(screen.getByText(/Monthly refill/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Pharmacy/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/25.50/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/On sale/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,684 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { SWRConfig } from 'swr';
|
||||
import type React from 'react';
|
||||
|
||||
const {
|
||||
mockListCabinetItems,
|
||||
mockGetCabinetSummary,
|
||||
mockCreateCabinetItem,
|
||||
mockAdjustCabinetItemQuantity,
|
||||
mockDeleteCabinetItem,
|
||||
} = vi.hoisted(() => ({
|
||||
mockListCabinetItems: vi.fn(),
|
||||
mockGetCabinetSummary: vi.fn(),
|
||||
mockCreateCabinetItem: vi.fn(),
|
||||
mockAdjustCabinetItemQuantity: vi.fn(),
|
||||
mockDeleteCabinetItem: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListMedicines } = vi.hoisted(() => ({ mockListMedicines: vi.fn() }));
|
||||
|
||||
vi.mock('@/services/cabinet', () => ({
|
||||
listCabinetItems: mockListCabinetItems,
|
||||
getCabinetSummary: mockGetCabinetSummary,
|
||||
createCabinetItem: mockCreateCabinetItem,
|
||||
adjustCabinetItemQuantity: mockAdjustCabinetItemQuantity,
|
||||
deleteCabinetItem: mockDeleteCabinetItem,
|
||||
}));
|
||||
|
||||
vi.mock('@/services/medicines', () => ({ listMedicines: mockListMedicines }));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => (
|
||||
<a href={props.href}>{props.children}</a>
|
||||
),
|
||||
}));
|
||||
|
||||
import { CabinetTab } from '../../../../src/app/(dashboard)/medicines/CabinetTab';
|
||||
|
||||
const emptySummary = { data: [] };
|
||||
const emptyItems = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptyMeds = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
const cabinetItem = {
|
||||
_id: 'ci-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantity: 10,
|
||||
unit: 'tablet',
|
||||
status: 'active',
|
||||
expirationDate: null,
|
||||
unitPrice: null,
|
||||
totalPrice: null,
|
||||
storeId: null,
|
||||
notes: null,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockGetCabinetSummary.mockResolvedValue(emptySummary);
|
||||
mockListCabinetItems.mockResolvedValue(emptyItems);
|
||||
mockListMedicines.mockResolvedValue(emptyMeds);
|
||||
});
|
||||
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<SWRConfig value={{ provider: () => new Map(), dedupingInterval: 0 }}>{children}</SWRConfig>
|
||||
);
|
||||
|
||||
describe('CabinetTab', () => {
|
||||
it('shows empty state when no items', async () => {
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/cabinet is empty/i)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when list fails', async () => {
|
||||
mockGetCabinetSummary.mockRejectedValue(new Error('Server error'));
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Server error')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses error on Dismiss click', async () => {
|
||||
mockGetCabinetSummary.mockRejectedValue(new Error('Server error'));
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Server error'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('Server error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles Add to Cabinet form', async () => {
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
expect(screen.getByPlaceholderText('Search medicines...')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getAllByText('Cancel')[0]);
|
||||
expect(screen.queryByPlaceholderText('Search medicines...')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('switches between Summary and All Items views', async () => {
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
expect(mockListCabinetItems).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('renders cabinet items in detail view', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('adjusts item quantity', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAdjustCabinetItemQuantity.mockResolvedValue({ ...cabinetItem, quantity: 11 });
|
||||
mockGetCabinetSummary.mockResolvedValue(emptySummary);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByTitle('Add 1'));
|
||||
await userEvent.click(screen.getByTitle('Add 1'));
|
||||
|
||||
expect(mockAdjustCabinetItemQuantity).toHaveBeenCalledWith('hh1', 'ci-1', { delta: 1 });
|
||||
});
|
||||
|
||||
it('shows summary view with medicine data', async () => {
|
||||
mockGetCabinetSummary.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
totalQuantity: 30,
|
||||
unit: 'tablet',
|
||||
itemCount: 2,
|
||||
earliestExpiry: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
expect(screen.getByText(/2 items/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('expands medicine in summary view', async () => {
|
||||
mockGetCabinetSummary.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
totalQuantity: 30,
|
||||
unit: 'tablet',
|
||||
itemCount: 1,
|
||||
earliestExpiry: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
await userEvent.click(screen.getByText('Metformin').closest('button')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockListCabinetItems).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ medicineId: 'med-1' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('submits AddToCabinetForm with medicine selection validation', async () => {
|
||||
mockCreateCabinetItem.mockResolvedValue(cabinetItem);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
|
||||
// Submit without selecting a medicine - should show error
|
||||
fireEvent.submit(screen.getByPlaceholderText('Search medicines...').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Please select a medicine')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('submits AddToCabinetForm successfully', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'med-1',
|
||||
name: 'Metformin',
|
||||
form: 'tablet',
|
||||
strength: 500,
|
||||
strengthUnit: 'mg',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateCabinetItem.mockResolvedValue(cabinetItem);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
|
||||
// Select a medicine (first combobox is the medicine select)
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, {
|
||||
target: { value: 'med-1' },
|
||||
});
|
||||
|
||||
fireEvent.submit(screen.getByPlaceholderText('Search medicines...').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateCabinetItem).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ medicineId: 'med-1' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('submits AddToCabinetForm with expiration date and notes', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'med-1',
|
||||
name: 'Metformin',
|
||||
form: 'tablet',
|
||||
strength: 500,
|
||||
strengthUnit: 'mg',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateCabinetItem.mockResolvedValue(cabinetItem);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
|
||||
// Select a medicine
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, {
|
||||
target: { value: 'med-1' },
|
||||
});
|
||||
|
||||
// Set expiration date (type="date" input, no label association)
|
||||
const dateInput = document.querySelector('input[type="date"]') as HTMLElement;
|
||||
if (dateInput) fireEvent.change(dateInput, { target: { value: '2027-12-31' } });
|
||||
|
||||
// Set notes
|
||||
fireEvent.change(screen.getByPlaceholderText('Any notes about this item'), {
|
||||
target: { value: 'Store in fridge' },
|
||||
});
|
||||
|
||||
fireEvent.submit(screen.getByPlaceholderText('Search medicines...').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateCabinetItem).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({
|
||||
medicineId: 'med-1',
|
||||
notes: 'Store in fridge',
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('changes quantity and unit in AddToCabinetForm', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', form: 'tablet', strength: 500, strengthUnit: 'mg' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
|
||||
// Search for medicine
|
||||
fireEvent.change(screen.getByPlaceholderText('Search medicines...'), {
|
||||
target: { value: 'Met' },
|
||||
});
|
||||
|
||||
// Change quantity
|
||||
fireEvent.change(screen.getByPlaceholderText('30'), { target: { value: '60' } });
|
||||
|
||||
// Change unit
|
||||
fireEvent.change(screen.getAllByRole('combobox')[1]!, { target: { value: 'capsule' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('Search medicines...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('switches from detail view back to summary view', async () => {
|
||||
mockGetCabinetSummary.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
totalQuantity: 10,
|
||||
unit: 'tablet',
|
||||
itemCount: 1,
|
||||
earliestExpiry: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
// Now in detail view
|
||||
await waitFor(() => screen.getByText('Summary'));
|
||||
await userEvent.click(screen.getByText('Summary'));
|
||||
|
||||
// Back in summary view — Metformin should show
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows expiry date for cabinet items', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
...cabinetItem,
|
||||
expirationDate: '2099-12-31T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
expect(screen.getByText(/days\)/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('filters cabinet items by status in detail view', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('All Statuses'));
|
||||
fireEvent.change(screen.getByDisplayValue('All Statuses'), { target: { value: 'active' } });
|
||||
|
||||
await waitFor(() => expect(mockListCabinetItems).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('takes 1 from cabinet item', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAdjustCabinetItemQuantity.mockResolvedValue({ ...cabinetItem, quantity: 9 });
|
||||
mockGetCabinetSummary.mockResolvedValue(emptySummary);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByTitle('Take 1'));
|
||||
await userEvent.click(screen.getByTitle('Take 1'));
|
||||
|
||||
expect(mockAdjustCabinetItemQuantity).toHaveBeenCalledWith('hh1', 'ci-1', { delta: -1 });
|
||||
});
|
||||
|
||||
it('collapses expanded medicine when it returns 0 items', async () => {
|
||||
mockGetCabinetSummary.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
totalQuantity: 0,
|
||||
unit: 'tablet',
|
||||
itemCount: 0,
|
||||
earliestExpiry: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
await userEvent.click(screen.getByText('Metformin').closest('button')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockListCabinetItems).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ medicineId: 'med-1' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows error when createCabinetItem fails', async () => {
|
||||
mockCreateCabinetItem.mockRejectedValue(new Error('Create failed'));
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
fireEvent.submit(screen.getByPlaceholderText('Search medicines...').closest('form')!);
|
||||
|
||||
// Should show 'Please select a medicine' validation first (since no medicine selected)
|
||||
await waitFor(() => expect(screen.getByText('Please select a medicine')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('deletes cabinet item after confirmation', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteCabinetItem.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByTitle('Delete'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteCabinetItem).toHaveBeenCalledWith('hh1', 'ci-1');
|
||||
});
|
||||
|
||||
it('shows error when adjust fails', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAdjustCabinetItemQuantity.mockRejectedValue(new Error('Adjust failed'));
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByTitle('Add 1'));
|
||||
await userEvent.click(screen.getByTitle('Add 1'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Adjust failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when delete fails', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteCabinetItem.mockRejectedValue(new Error('Delete failed'));
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => screen.getByTitle('Delete'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Delete failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('cancels AddToCabinetForm with internal Cancel button', async () => {
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
// Click the Cancel button inside the form (not the toggle button)
|
||||
const cancelButtons = screen.getAllByText('Cancel');
|
||||
await userEvent.click(cancelButtons[cancelButtons.length - 1]!);
|
||||
|
||||
expect(screen.queryByPlaceholderText('Search medicines...')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('collapses expanded medicine when clicked again', async () => {
|
||||
const summary = {
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
totalQuantity: 10,
|
||||
unit: 'tablet',
|
||||
itemCount: 1,
|
||||
earliestExpiry: null,
|
||||
};
|
||||
mockGetCabinetSummary.mockResolvedValue({ data: [summary] });
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [cabinetItem],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
// First click expands — handleExpand calls listCabinetItems
|
||||
await userEvent.click(screen.getByText('Metformin').closest('button')!);
|
||||
await waitFor(() => expect(mockListCabinetItems).toHaveBeenCalledTimes(1));
|
||||
const callsAfterExpand = mockListCabinetItems.mock.calls.length;
|
||||
|
||||
// Second click collapses — no additional listCabinetItems calls
|
||||
await userEvent.click(screen.getByText('Metformin').closest('button')!);
|
||||
await waitFor(() => expect(screen.queryByTitle('Take 1')).not.toBeInTheDocument());
|
||||
|
||||
expect(mockListCabinetItems.mock.calls.length).toBe(callsAfterExpand);
|
||||
});
|
||||
|
||||
it('shows empty items when expand fails', async () => {
|
||||
mockGetCabinetSummary.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
totalQuantity: 10,
|
||||
unit: 'tablet',
|
||||
itemCount: 1,
|
||||
earliestExpiry: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
mockListCabinetItems.mockRejectedValue(new Error('Expand failed'));
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
await userEvent.click(screen.getByText('Metformin').closest('button')!);
|
||||
|
||||
await waitFor(() => expect(mockListCabinetItems).toHaveBeenCalledTimes(1));
|
||||
// No items should be shown (empty after error)
|
||||
expect(screen.queryByTitle('Take 1')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows create error when medicine is selected and create fails', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', form: 'tablet', strength: 500, strengthUnit: 'mg' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateCabinetItem.mockRejectedValue(new Error('Create failed'));
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
fireEvent.submit(screen.getByPlaceholderText('Search medicines...').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Create failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows item notes in detail view', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [{ ...cabinetItem, notes: 'Store in fridge' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All Items'));
|
||||
await userEvent.click(screen.getByText('All Items'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Store in fridge')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('waits for medicines to load then selects medicine in form', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', form: 'tablet', strength: 500, strengthUnit: 'mg' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
// Wait for medicine options to load (covers medicines.map callback)
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
(screen.getAllByRole('combobox')[0] as HTMLSelectElement).options.length,
|
||||
).toBeGreaterThan(1),
|
||||
);
|
||||
|
||||
// Select a valid medicine (true branch: medForm is set → defaultUnitForForm called)
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
|
||||
// Select empty value (false branch: selectedMed undefined → DosageUnit.TABLET fallback)
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: '' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('Search medicines...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error is thrown during create', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', form: 'tablet', strength: 500, strengthUnit: 'mg' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateCabinetItem.mockRejectedValue('unexpected');
|
||||
|
||||
render(<CabinetTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Add to Cabinet'));
|
||||
await userEvent.click(screen.getByText('Add to Cabinet'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Search medicines...'));
|
||||
fireEvent.change(screen.getAllByRole('combobox')[0]!, { target: { value: 'med-1' } });
|
||||
fireEvent.submit(screen.getByPlaceholderText('Search medicines...').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to add item')).toBeInTheDocument());
|
||||
});
|
||||
});
|
||||
|
|
@ -1,250 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockListMedicines, mockCreateMedicine, mockDeleteMedicine } = vi.hoisted(() => ({
|
||||
mockListMedicines: vi.fn(),
|
||||
mockCreateMedicine: vi.fn(),
|
||||
mockDeleteMedicine: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/medicines', () => ({
|
||||
listMedicines: mockListMedicines,
|
||||
createMedicine: mockCreateMedicine,
|
||||
deleteMedicine: mockDeleteMedicine,
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => (
|
||||
<a href={props.href}>{props.children}</a>
|
||||
),
|
||||
}));
|
||||
|
||||
import { LibraryTab } from '../../../../src/app/(dashboard)/medicines/LibraryTab';
|
||||
|
||||
const emptyResponse = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
const medicine = {
|
||||
_id: 'med-1',
|
||||
name: 'Metformin',
|
||||
form: 'tablet',
|
||||
strength: 500,
|
||||
strengthUnit: 'mg',
|
||||
category: 'prescription',
|
||||
tags: [],
|
||||
};
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
describe('LibraryTab', () => {
|
||||
it('shows loading skeleton initially', () => {
|
||||
mockListMedicines.mockReturnValue(new Promise(() => {}));
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
expect(screen.queryByText('Metformin')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders medicine list after load', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [medicine],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows empty state when no medicines', async () => {
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No medicines yet/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when list fails', async () => {
|
||||
mockListMedicines.mockRejectedValue(new Error('Failed to load'));
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to load')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses error on Dismiss click', async () => {
|
||||
mockListMedicines.mockRejectedValue(new Error('Failed to load'));
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Failed to load'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('Failed to load')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles Add Medicine form', async () => {
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Medicine'));
|
||||
await userEvent.click(screen.getByText('Add Medicine'));
|
||||
expect(screen.getByPlaceholderText('e.g. Metformin')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getAllByText('Cancel')[0]);
|
||||
expect(screen.queryByPlaceholderText('e.g. Metformin')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('creates medicine and refreshes list', async () => {
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
mockCreateMedicine.mockResolvedValue({ _id: 'med-2', name: 'Aspirin' });
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await userEvent.click(screen.getByText('Add Medicine'));
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('e.g. Metformin'), 'Aspirin');
|
||||
await userEvent.type(screen.getByPlaceholderText('500'), '100');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Create Medicine' }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateMedicine).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ name: 'Aspirin' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('deletes medicine after confirmation', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [medicine],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteMedicine.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteMedicine).toHaveBeenCalledWith('hh1', 'med-1');
|
||||
});
|
||||
|
||||
it('changes form fields in add medicine form', async () => {
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Medicine'));
|
||||
await userEvent.click(screen.getByText('Add Medicine'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Metformin'));
|
||||
|
||||
// Change form type
|
||||
fireEvent.change(screen.getByDisplayValue('Tablet'), { target: { value: 'capsule' } });
|
||||
// Change strength unit
|
||||
fireEvent.change(screen.getByDisplayValue('mg'), { target: { value: 'mcg' } });
|
||||
// Change category
|
||||
fireEvent.change(screen.getByDisplayValue('OTC'), { target: { value: 'prescription' } });
|
||||
// Change notes (covers truthy branch)
|
||||
fireEvent.change(screen.getByPlaceholderText('Any additional notes'), {
|
||||
target: { value: 'test notes' },
|
||||
});
|
||||
// Clear notes (covers falsy branch → undefined)
|
||||
fireEvent.change(screen.getByPlaceholderText('Any additional notes'), {
|
||||
target: { value: '' },
|
||||
});
|
||||
|
||||
// Verify form is still visible
|
||||
expect(screen.getByPlaceholderText('e.g. Metformin')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('filters medicines by search, category, and form', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [medicine],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
|
||||
// Search filter
|
||||
fireEvent.change(screen.getByPlaceholderText('Search medicines...'), {
|
||||
target: { value: 'met' },
|
||||
});
|
||||
|
||||
// Category filter
|
||||
fireEvent.change(screen.getByDisplayValue('All Categories'), {
|
||||
target: { value: 'prescription' },
|
||||
});
|
||||
|
||||
// Form filter
|
||||
fireEvent.change(screen.getByDisplayValue('All Forms'), { target: { value: 'tablet' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('Search medicines...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error is thrown on delete', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [medicine],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteMedicine.mockRejectedValue('oops');
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to delete')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('cancels CreateMedicineForm with internal Cancel button', async () => {
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Medicine'));
|
||||
await userEvent.click(screen.getByText('Add Medicine'));
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Metformin'));
|
||||
|
||||
const cancelButtons = screen.getAllByText('Cancel');
|
||||
await userEvent.click(cancelButtons[cancelButtons.length - 1]!);
|
||||
|
||||
expect(screen.queryByPlaceholderText('e.g. Metformin')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error is thrown on create medicine', async () => {
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
mockCreateMedicine.mockRejectedValue('create failed');
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Medicine'));
|
||||
await userEvent.click(screen.getByText('Add Medicine'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Metformin'));
|
||||
await userEvent.type(screen.getByPlaceholderText('e.g. Metformin'), 'Aspirin');
|
||||
await userEvent.type(screen.getByPlaceholderText('500'), '100');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Create Medicine' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to create medicine')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('does not delete medicine if confirmation cancelled', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [medicine],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
|
||||
render(<LibraryTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Metformin'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteMedicine).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,524 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
|
||||
const { mockListFills, mockPreviewFill, mockExecuteFill, mockUndoFill } = vi.hoisted(() => ({
|
||||
mockListFills: vi.fn(),
|
||||
mockPreviewFill: vi.fn(),
|
||||
mockExecuteFill: vi.fn(),
|
||||
mockUndoFill: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListRegimens } = vi.hoisted(() => ({ mockListRegimens: vi.fn() }));
|
||||
|
||||
vi.mock('@/services/organizer', () => ({
|
||||
listFills: mockListFills,
|
||||
previewFill: mockPreviewFill,
|
||||
executeFill: mockExecuteFill,
|
||||
undoFill: mockUndoFill,
|
||||
}));
|
||||
|
||||
vi.mock('@/services/regimens', () => ({ listRegimens: mockListRegimens }));
|
||||
|
||||
import { OrganizerTab } from '../../../../src/app/(dashboard)/medicines/OrganizerTab';
|
||||
|
||||
const emptyRegimens = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptyFills = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
const activeRegimen = {
|
||||
_id: 'reg-1',
|
||||
name: 'Morning Routine',
|
||||
isActive: true,
|
||||
medications: [],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockListRegimens.mockResolvedValue(emptyRegimens);
|
||||
mockListFills.mockResolvedValue(emptyFills);
|
||||
});
|
||||
|
||||
describe('OrganizerTab', () => {
|
||||
it('shows fill organizer and fill history sections', async () => {
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Fill Pill Organizer')).toBeInTheDocument());
|
||||
expect(screen.getByText('Fill History')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows no active regimens message when none exist', async () => {
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No active regimens found/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fill form when active regimens exist', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Preview fill')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows empty fill history', async () => {
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('No fills recorded yet.')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('renders fill history entries', async () => {
|
||||
mockListFills.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'fill-1',
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
status: 'completed',
|
||||
numberOfDays: 7,
|
||||
items: [],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Morning Routine')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('previews fill and shows preview result', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
numberOfDays: 7,
|
||||
hasShortages: false,
|
||||
items: [],
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.change(screen.getByDisplayValue('7'), { target: { value: '14' } });
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockPreviewFill).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ regimenId: 'reg-1' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows preview result and executes fill', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: false,
|
||||
items: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantityNeeded: 7,
|
||||
quantityAvailable: 30,
|
||||
isShort: false,
|
||||
shortage: 0,
|
||||
cabinetBreakdown: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
mockExecuteFill.mockResolvedValue({ _id: 'fill-new', status: 'completed' });
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => screen.getByText('Confirm fill'));
|
||||
expect(screen.getByText('Metformin')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getByText('Confirm fill'));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockExecuteFill).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ regimenId: 'reg-1' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows shortage warning in preview', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: true,
|
||||
items: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantityNeeded: 7,
|
||||
quantityAvailable: 3,
|
||||
isShort: true,
|
||||
shortage: 4,
|
||||
cabinetBreakdown: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => screen.getByText('Shortages detected'));
|
||||
expect(screen.getByText('Allow partial fill (fill what is available)')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows undo error when undo fails', async () => {
|
||||
mockListFills.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'fill-1',
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
status: 'completed',
|
||||
numberOfDays: 7,
|
||||
items: [],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUndoFill.mockRejectedValue(new Error('Undo failed'));
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Undo'));
|
||||
await userEvent.click(screen.getByText('Undo'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Undo failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('filters fill history by status', async () => {
|
||||
mockListFills.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'fill-1',
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
status: 'completed',
|
||||
numberOfDays: 7,
|
||||
items: [],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('All statuses'));
|
||||
fireEvent.change(screen.getByDisplayValue('All statuses'), { target: { value: 'completed' } });
|
||||
|
||||
await waitFor(() => expect(mockListFills).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('shows Ready to fill when no shortages', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: false,
|
||||
items: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantityNeeded: 7,
|
||||
quantityAvailable: 30,
|
||||
isShort: false,
|
||||
shortage: 0,
|
||||
cabinetBreakdown: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Ready to fill')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('renders fill items with short status', async () => {
|
||||
mockListFills.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'fill-1',
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
status: 'partial',
|
||||
numberOfDays: 7,
|
||||
fillDate: '2026-01-01T00:00:00.000Z',
|
||||
items: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantityNeeded: 7,
|
||||
quantityTaken: 3,
|
||||
wasShort: true,
|
||||
shortage: 4,
|
||||
deductions: [],
|
||||
},
|
||||
],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/Metformin.*short/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses fill history error', async () => {
|
||||
mockListFills.mockRejectedValue(new Error('History error'));
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('History error'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('History error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('undoes a fill after confirmation', async () => {
|
||||
mockListFills.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'fill-1',
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
status: 'completed',
|
||||
numberOfDays: 7,
|
||||
items: [],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUndoFill.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Undo'));
|
||||
await userEvent.click(screen.getByText('Undo'));
|
||||
|
||||
expect(mockUndoFill).toHaveBeenCalledWith('hh1', 'fill-1');
|
||||
});
|
||||
|
||||
it('cancels from preview screen', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: false,
|
||||
items: [],
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => screen.getByText('Confirm fill'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Back' }));
|
||||
|
||||
// Should return to the form
|
||||
await waitFor(() => expect(screen.getByText('Preview fill')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows cabinet breakdown in preview items', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: false,
|
||||
items: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantityNeeded: 7,
|
||||
quantityAvailable: 30,
|
||||
isShort: false,
|
||||
shortage: 0,
|
||||
cabinetBreakdown: [
|
||||
{
|
||||
cabinetItemId: 'ci-1',
|
||||
quantityToTake: 7,
|
||||
expirationDate: '2027-06-01T00:00:00.000Z',
|
||||
},
|
||||
{ cabinetItemId: 'ci-2', quantityToTake: 3 },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/7 units/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown during preview', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockRejectedValue('preview failed');
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to generate preview')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown during fill', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: false,
|
||||
items: [],
|
||||
});
|
||||
mockExecuteFill.mockRejectedValue('fill failed');
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => screen.getByText('Confirm fill'));
|
||||
await userEvent.click(screen.getByText('Confirm fill'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Fill failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('toggles allow partial checkbox in shortage preview', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockPreviewFill.mockResolvedValue({
|
||||
regimenId: 'reg-1',
|
||||
regimenName: 'Morning Routine',
|
||||
numberOfDays: 7,
|
||||
hasShortages: true,
|
||||
items: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
quantityNeeded: 7,
|
||||
quantityAvailable: 3,
|
||||
isShort: true,
|
||||
shortage: 4,
|
||||
cabinetBreakdown: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByText('Preview fill'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select regimen...').closest('select')!, {
|
||||
target: { value: 'reg-1' },
|
||||
});
|
||||
fireEvent.submit(screen.getByText('Preview fill').closest('form')!);
|
||||
|
||||
await waitFor(() => screen.getByText('Allow partial fill (fill what is available)'));
|
||||
fireEvent.click(screen.getByLabelText('Allow partial fill (fill what is available)'));
|
||||
|
||||
expect(screen.getByLabelText('Allow partial fill (fill what is available)')).toBeChecked();
|
||||
});
|
||||
|
||||
it('changes notes field in the organizer form', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [activeRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<OrganizerTab householdId="hh1" />);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Any notes for this fill'));
|
||||
fireEvent.change(screen.getByPlaceholderText('Any notes for this fill'), {
|
||||
target: { value: 'Fill for next week' },
|
||||
});
|
||||
|
||||
expect(screen.getByDisplayValue('Fill for next week')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,580 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { SWRConfig } from 'swr';
|
||||
|
||||
const {
|
||||
mockListRegimens,
|
||||
mockCreateRegimen,
|
||||
mockUpdateRegimen,
|
||||
mockDeleteRegimen,
|
||||
mockGetBurnRates,
|
||||
} = vi.hoisted(() => ({
|
||||
mockListRegimens: vi.fn(),
|
||||
mockCreateRegimen: vi.fn(),
|
||||
mockUpdateRegimen: vi.fn(),
|
||||
mockDeleteRegimen: vi.fn(),
|
||||
mockGetBurnRates: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListMedicines } = vi.hoisted(() => ({ mockListMedicines: vi.fn() }));
|
||||
|
||||
vi.mock('@/services/regimens', () => ({
|
||||
listRegimens: mockListRegimens,
|
||||
createRegimen: mockCreateRegimen,
|
||||
updateRegimen: mockUpdateRegimen,
|
||||
deleteRegimen: mockDeleteRegimen,
|
||||
getBurnRates: mockGetBurnRates,
|
||||
}));
|
||||
|
||||
vi.mock('@/services/medicines', () => ({ listMedicines: mockListMedicines }));
|
||||
|
||||
import { RegimensTab } from '../../../../src/app/(dashboard)/medicines/RegimensTab';
|
||||
|
||||
const emptyMeds = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptyRegimens = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
const regimen = {
|
||||
_id: 'reg-1',
|
||||
name: 'Morning Routine',
|
||||
isActive: true,
|
||||
medications: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
medicineForm: 'tablet',
|
||||
dosage: 1,
|
||||
dosageUnit: 'tablet',
|
||||
frequency: 'once_daily',
|
||||
},
|
||||
],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockListRegimens.mockResolvedValue(emptyRegimens);
|
||||
mockListMedicines.mockResolvedValue(emptyMeds);
|
||||
mockGetBurnRates.mockResolvedValue({ data: [] });
|
||||
});
|
||||
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<SWRConfig value={{ provider: () => new Map(), dedupingInterval: 0 }}>{children}</SWRConfig>
|
||||
);
|
||||
|
||||
describe('RegimensTab', () => {
|
||||
it('shows empty state when no regimens', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No regimens yet/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('renders regimen list', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Morning Routine')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when list fails', async () => {
|
||||
mockListRegimens.mockRejectedValue(new Error('Load failed'));
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Load failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses error', async () => {
|
||||
mockListRegimens.mockRejectedValue(new Error('Load failed'));
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Load failed'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('Load failed')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles new regimen form', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getByText('New Regimen'));
|
||||
expect(screen.getByText(/New Regimen/)).toBeInTheDocument();
|
||||
expect(screen.getByPlaceholderText('e.g. Morning routine')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getAllByText('Cancel')[0]);
|
||||
expect(screen.queryByPlaceholderText('e.g. Morning routine')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('creates a regimen when form is submitted', async () => {
|
||||
mockCreateRegimen.mockResolvedValue(regimen);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Morning routine'), {
|
||||
target: { value: 'Evening Routine' },
|
||||
});
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. Morning routine').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateRegimen).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ name: 'Evening Routine' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('deletes regimen after confirmation', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteRegimen.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteRegimen).toHaveBeenCalledWith('hh1', 'reg-1');
|
||||
});
|
||||
|
||||
it('shows burn rate section when toggled', async () => {
|
||||
mockGetBurnRates.mockResolvedValue({ data: [] });
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Burn rate'));
|
||||
await userEvent.click(screen.getByText('Burn rate'));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('Burn Rate & Spending Projections')).toBeInTheDocument(),
|
||||
);
|
||||
expect(mockGetBurnRates).toHaveBeenCalledWith('hh1');
|
||||
});
|
||||
|
||||
it('opens edit form for regimen', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
expect(screen.getByDisplayValue('Morning Routine')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('saves edited regimen', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRegimen.mockResolvedValue({ ...regimen, name: 'Evening Routine' });
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Morning Routine'));
|
||||
fireEvent.change(screen.getByDisplayValue('Morning Routine'), {
|
||||
target: { value: 'Evening Routine' },
|
||||
});
|
||||
fireEvent.submit(screen.getByDisplayValue('Evening Routine').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockUpdateRegimen).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
'reg-1',
|
||||
expect.objectContaining({ name: 'Evening Routine' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows validation error when submitting regimen form with no medications', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Morning routine'), {
|
||||
target: { value: 'My Regimen' },
|
||||
});
|
||||
// Remove the default medication
|
||||
await userEvent.click(screen.getByTitle('Remove medication'));
|
||||
// Submit with no medications
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. Morning routine').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('At least one medication is required.')).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows error when delete fails', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteRegimen.mockRejectedValue(new Error('Delete failed'));
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Delete failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows burn rate table with data', async () => {
|
||||
mockGetBurnRates.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Metformin',
|
||||
dailyConsumption: 2.0,
|
||||
totalInCabinet: 60,
|
||||
daysUntilEmpty: 30,
|
||||
earliestExpiry: null,
|
||||
avgUnitPrice: null,
|
||||
projectedDailyCost: null,
|
||||
projectedMonthlyCost: null,
|
||||
projectedYearlyCost: null,
|
||||
currency: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Burn rate'));
|
||||
await userEvent.click(screen.getByText('Burn rate'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
expect(screen.getByText('2.00')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('cancels edit form and hides it', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Morning Routine'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
||||
|
||||
expect(screen.queryByDisplayValue('Morning Routine')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('filters regimens by active status', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('All regimens'));
|
||||
fireEvent.change(screen.getByDisplayValue('All regimens'), { target: { value: 'active' } });
|
||||
|
||||
await waitFor(() => expect(mockListRegimens).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('adds a medication in the regimen form', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByText('+ Add medication'));
|
||||
await userEvent.click(screen.getByText('+ Add medication'));
|
||||
|
||||
// Now 2 medications — verify the button still shows
|
||||
expect(screen.getByText('+ Add medication')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles isActive checkbox in regimen form', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByLabelText('Active'));
|
||||
expect(screen.getByLabelText('Active')).toBeChecked();
|
||||
fireEvent.click(screen.getByLabelText('Active'));
|
||||
expect(screen.getByLabelText('Active')).not.toBeChecked();
|
||||
});
|
||||
|
||||
it('changes medicine, dosage, and unit in medication row', async () => {
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg', form: 'tablet' },
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByText('Select medicine...'));
|
||||
|
||||
// Select a medicine in the medication row
|
||||
const medicineSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find(
|
||||
(s) => (s as HTMLSelectElement).options[0]?.text === 'Select medicine...',
|
||||
) as HTMLSelectElement;
|
||||
expect(medicineSelect).toBeDefined();
|
||||
fireEvent.change(medicineSelect!, { target: { value: 'med-1' } });
|
||||
|
||||
// Change dosage
|
||||
const dosageInput = document.querySelector('input[min="0.01"]') as HTMLElement;
|
||||
if (dosageInput) fireEvent.change(dosageInput, { target: { value: '2' } });
|
||||
|
||||
// Change dosage unit
|
||||
const unitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).value === 'tablet') as HTMLSelectElement;
|
||||
if (unitSelect) fireEvent.change(unitSelect!, { target: { value: 'capsule' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('e.g. Morning routine')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes instructions field in medication row', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Take with food'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Take with food'), {
|
||||
target: { value: 'With meals' },
|
||||
});
|
||||
|
||||
expect(screen.getByDisplayValue('With meals')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes frequency to custom and sets times per day', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
|
||||
// The frequency select has 'daily' as its first option value
|
||||
const frequencySelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).options[0]?.value === 'daily') as HTMLSelectElement;
|
||||
expect(frequencySelect).toBeDefined();
|
||||
fireEvent.change(frequencySelect!, { target: { value: 'custom' } });
|
||||
|
||||
// customFrequencyPerDay input should appear
|
||||
await waitFor(() => expect(document.querySelector('input[min="1"][step="1"]')).not.toBeNull());
|
||||
const timesPerDayInput = document.querySelector('input[min="1"][step="1"]') as HTMLElement;
|
||||
fireEvent.change(timesPerDayInput, { target: { value: '3' } });
|
||||
|
||||
// Change back to non-custom (covers the undefined branch)
|
||||
fireEvent.change(frequencySelect!, { target: { value: 'twice_daily' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('e.g. Morning routine')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes time of day in medication row', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
|
||||
// The time-of-day select has 'Any time' as its first option text
|
||||
const timeOfDaySelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).options[0]?.text === 'Any time') as HTMLSelectElement;
|
||||
expect(timeOfDaySelect).toBeDefined();
|
||||
fireEvent.change(timeOfDaySelect!, { target: { value: 'morning' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('e.g. Morning routine')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when burn rate fetch fails', async () => {
|
||||
mockGetBurnRates.mockRejectedValue(new Error('Burn rate failed'));
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Burn rate'));
|
||||
await userEvent.click(screen.getByText('Burn rate'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Burn rate failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when update fails', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRegimen.mockRejectedValue(new Error('Update failed'));
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Deactivate'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Update failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('toggles active/inactive status', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRegimen.mockResolvedValue({ ...regimen, isActive: false });
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Deactivate'));
|
||||
|
||||
expect(mockUpdateRegimen).toHaveBeenCalledWith('hh1', 'reg-1', { isActive: false });
|
||||
});
|
||||
|
||||
it('cancels new regimen form using internal Cancel button', async () => {
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
// The Cancel button inside the RegimenForm (not the toggle button)
|
||||
const cancelButtons = screen.getAllByRole('button', { name: 'Cancel' });
|
||||
await userEvent.click(cancelButtons[cancelButtons.length - 1]!);
|
||||
|
||||
expect(screen.queryByPlaceholderText('e.g. Morning routine')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('filters regimens by inactive status', async () => {
|
||||
mockListRegimens.mockResolvedValue(emptyRegimens);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(mockListRegimens).toHaveBeenCalledTimes(1));
|
||||
|
||||
const filterSelect = screen.getByDisplayValue('All regimens');
|
||||
fireEvent.change(filterSelect, { target: { value: 'inactive' } });
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockListRegimens).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ isActive: false }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows filtered empty state when filter is active and no results', async () => {
|
||||
mockListRegimens.mockResolvedValue(emptyRegimens);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('All regimens'));
|
||||
fireEvent.change(screen.getByDisplayValue('All regimens'), { target: { value: 'active' } });
|
||||
|
||||
await waitFor(() => expect(screen.getByText('No active regimens found.')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows null when form is open and regimens list is empty', async () => {
|
||||
mockListRegimens.mockResolvedValue(emptyRegimens);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
// Form is open with no regimens — empty state shows null (nothing visible in that spot)
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
expect(screen.queryByText('No regimens yet.')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows plural medications label when regimen has multiple meds', async () => {
|
||||
const multiMedRegimen = {
|
||||
...regimen,
|
||||
medications: [
|
||||
{ ...regimen.medications[0] },
|
||||
{ ...regimen.medications[0], medicineId: 'med-2', medicineName: 'Aspirin' },
|
||||
],
|
||||
};
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [multiMedRegimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/2 medications/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on burn rates', async () => {
|
||||
mockGetBurnRates.mockRejectedValue('burn failed');
|
||||
mockListRegimens.mockResolvedValue(emptyRegimens);
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Burn rate'));
|
||||
await userEvent.click(screen.getByText('Burn rate'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to load data')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on create regimen', async () => {
|
||||
mockCreateRegimen.mockRejectedValue('save failed');
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('New Regimen'));
|
||||
await userEvent.click(screen.getAllByText('New Regimen')[0]);
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Morning routine'));
|
||||
await userEvent.type(screen.getByPlaceholderText('e.g. Morning routine'), 'Test');
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. Morning routine').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to save regimen')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('initializes edit form with existing medications', async () => {
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [regimen],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RegimensTab householdId="hh1" />, { wrapper });
|
||||
|
||||
await waitFor(() => screen.getByText('Morning Routine'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Morning Routine'));
|
||||
// The medication row should be pre-filled — Remove button should be present
|
||||
expect(screen.getByTitle('Remove medication')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,794 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
const { mockUseParams } = vi.hoisted(() => ({ mockUseParams: vi.fn() }));
|
||||
|
||||
const {
|
||||
mockGetMedicine,
|
||||
mockListMedicineProducts,
|
||||
mockCreateMedicineProduct,
|
||||
mockDeleteMedicineProduct,
|
||||
mockUpdateMedicine,
|
||||
mockUpdateMedicineProduct,
|
||||
} = vi.hoisted(() => ({
|
||||
mockGetMedicine: vi.fn(),
|
||||
mockListMedicineProducts: vi.fn(),
|
||||
mockCreateMedicineProduct: vi.fn(),
|
||||
mockDeleteMedicineProduct: vi.fn(),
|
||||
mockUpdateMedicine: vi.fn(),
|
||||
mockUpdateMedicineProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListCabinetItems, mockAdjustCabinetItemQuantity, mockDeleteCabinetItem } = vi.hoisted(
|
||||
() => ({
|
||||
mockListCabinetItems: vi.fn(),
|
||||
mockAdjustCabinetItemQuantity: vi.fn(),
|
||||
mockDeleteCabinetItem: vi.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('next/navigation', () => ({ useParams: mockUseParams }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => (
|
||||
<a href={props.href}>{props.children}</a>
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/medicines', () => ({
|
||||
getMedicine: mockGetMedicine,
|
||||
listMedicineProducts: mockListMedicineProducts,
|
||||
createMedicineProduct: mockCreateMedicineProduct,
|
||||
deleteMedicineProduct: mockDeleteMedicineProduct,
|
||||
updateMedicine: mockUpdateMedicine,
|
||||
updateMedicineProduct: mockUpdateMedicineProduct,
|
||||
}));
|
||||
|
||||
vi.mock('@/services/cabinet', () => ({
|
||||
listCabinetItems: mockListCabinetItems,
|
||||
adjustCabinetItemQuantity: mockAdjustCabinetItemQuantity,
|
||||
deleteCabinetItem: mockDeleteCabinetItem,
|
||||
}));
|
||||
|
||||
import MedicineDetailPage from '../../../../../src/app/(dashboard)/medicines/[id]/page';
|
||||
|
||||
const medicine = {
|
||||
_id: 'med-1',
|
||||
name: 'Metformin',
|
||||
form: 'tablet',
|
||||
strength: 500,
|
||||
strengthUnit: 'mg',
|
||||
category: 'prescription',
|
||||
notes: '',
|
||||
tags: [],
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
const emptyProducts = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptyItems = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockUseParams.mockReturnValue({ id: 'med-1' });
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetMedicine.mockResolvedValue(medicine);
|
||||
mockListMedicineProducts.mockResolvedValue(emptyProducts);
|
||||
mockListCabinetItems.mockResolvedValue(emptyItems);
|
||||
});
|
||||
|
||||
describe('MedicineDetailPage', () => {
|
||||
it('shows loading skeleton when session is loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
expect(screen.queryByText('Metformin')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders medicine name and sections after load', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Metformin')).toBeInTheDocument());
|
||||
expect(screen.getByText('Inventory')).toBeInTheDocument();
|
||||
expect(screen.getByText('Products (Brands/Packages)')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when medicine fails to load', async () => {
|
||||
mockGetMedicine.mockRejectedValue(new Error('Not found'));
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Not found')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows empty inventory state', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No inventory items/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows empty products state', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No products yet/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('toggles Add Product form', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
expect(screen.getByText('Add Product', { selector: 'h3' })).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getAllByText('Cancel')[0]);
|
||||
expect(screen.queryByText('Add Product', { selector: 'h3' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles edit medicine form', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
expect(screen.getByText('Edit Medicine')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getByText('Cancel'));
|
||||
expect(screen.queryByText('Edit Medicine')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('saves medicine edit', async () => {
|
||||
mockUpdateMedicine.mockResolvedValue({ ...medicine, name: 'Metformin XR' });
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockUpdateMedicine).toHaveBeenCalledWith('hh1', 'med-1', expect.any(Object)),
|
||||
);
|
||||
});
|
||||
|
||||
it('deletes a product after confirmation', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Glucophage',
|
||||
packageSize: 60,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteMedicineProduct.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Glucophage'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteMedicineProduct).toHaveBeenCalledWith('hh1', 'prod-1');
|
||||
});
|
||||
|
||||
it('submits CreateProductForm', async () => {
|
||||
mockCreateMedicineProduct.mockResolvedValue({
|
||||
_id: 'prod-new',
|
||||
brand: 'NewBrand',
|
||||
packageSize: 30,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. CVS Health'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. CVS Health'), {
|
||||
target: { value: 'NewBrand' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('90'), {
|
||||
target: { value: '30' },
|
||||
});
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. CVS Health').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateMedicineProduct).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
'med-1',
|
||||
expect.objectContaining({ brand: 'NewBrand' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows create product error on failure', async () => {
|
||||
mockCreateMedicineProduct.mockRejectedValue(new Error('Duplicate brand'));
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. CVS Health'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. CVS Health'), {
|
||||
target: { value: 'Brand' },
|
||||
});
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. CVS Health').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Duplicate brand')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('opens and saves product edit form', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Glucophage',
|
||||
packageSize: 60,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateMedicineProduct.mockResolvedValue({
|
||||
_id: 'prod-1',
|
||||
brand: 'Glucophage XR',
|
||||
packageSize: 60,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Glucophage'));
|
||||
// First Edit title is the medicine edit, second is the product edit
|
||||
await userEvent.click(screen.getAllByTitle('Edit')[1]!);
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Glucophage'));
|
||||
fireEvent.change(screen.getByDisplayValue('Glucophage'), {
|
||||
target: { value: 'Glucophage XR' },
|
||||
});
|
||||
fireEvent.submit(screen.getByDisplayValue('Glucophage XR').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockUpdateMedicineProduct).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
'prod-1',
|
||||
expect.objectContaining({ brand: 'Glucophage XR' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows concentration fields when injection medicine with ml unit', async () => {
|
||||
mockGetMedicine.mockResolvedValue({
|
||||
...medicine,
|
||||
form: 'injection',
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. CVS Health'));
|
||||
// Change package unit to ml to show concentration fields
|
||||
const unitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).value === 'vial') as HTMLSelectElement;
|
||||
fireEvent.change(unitSelect!, { target: { value: 'ml' } });
|
||||
|
||||
await waitFor(() => expect(screen.getByPlaceholderText('e.g. 100')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('deletes a product after confirmation', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Glucophage',
|
||||
packageSize: 60,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteMedicineProduct.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Glucophage'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteMedicineProduct).toHaveBeenCalledWith('hh1', 'prod-1');
|
||||
});
|
||||
|
||||
it('edits injection product and changes concentration fields', async () => {
|
||||
mockGetMedicine.mockResolvedValue({
|
||||
...medicine,
|
||||
form: 'injection',
|
||||
});
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Insulin',
|
||||
packageSize: 10,
|
||||
packageUnit: 'ml',
|
||||
source: 'manual',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Insulin'));
|
||||
await userEvent.click(screen.getAllByTitle('Edit')[1]!);
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Insulin'));
|
||||
|
||||
// concentration field should be visible since form=injection, unit=ml
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. 100'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '100' } });
|
||||
|
||||
// Change concentration unit
|
||||
const concUnitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).options[0]?.text === '--') as HTMLSelectElement;
|
||||
if (concUnitSelect) {
|
||||
fireEvent.change(concUnitSelect, { target: { value: 'mg/ml' } });
|
||||
}
|
||||
|
||||
expect(screen.getByDisplayValue('Insulin')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes fields in the product edit inline form', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Glucophage',
|
||||
packageSize: 60,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Glucophage'));
|
||||
await userEvent.click(screen.getAllByTitle('Edit')[1]!);
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Glucophage'));
|
||||
|
||||
// Change package size
|
||||
fireEvent.change(screen.getByDisplayValue('60'), { target: { value: '90' } });
|
||||
|
||||
// Change package unit
|
||||
const unitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).value === 'tablet') as HTMLSelectElement;
|
||||
fireEvent.change(unitSelect!, { target: { value: 'capsule' } });
|
||||
|
||||
expect(screen.getByDisplayValue('Glucophage')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes manufacturer, concentration and notes fields in product form', async () => {
|
||||
mockGetMedicine.mockResolvedValue({
|
||||
...medicine,
|
||||
form: 'injection',
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Pfizer'));
|
||||
|
||||
// Change manufacturer (truthy) then clear (falsy → undefined)
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Pfizer'), { target: { value: 'Pfizer' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Pfizer'), { target: { value: '' } });
|
||||
|
||||
// Change notes (truthy) then clear (falsy → undefined)
|
||||
fireEvent.change(screen.getByPlaceholderText('Any additional notes'), {
|
||||
target: { value: 'Store in fridge' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('Any additional notes'), {
|
||||
target: { value: '' },
|
||||
});
|
||||
|
||||
// Change unit to ml to show concentration fields
|
||||
const unitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).value === 'vial') as HTMLSelectElement;
|
||||
fireEvent.change(unitSelect!, { target: { value: 'ml' } });
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. 100'));
|
||||
// Set concentration (truthy) then clear (falsy → undefined)
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '50' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '' } });
|
||||
|
||||
// Change concentration unit
|
||||
const concUnitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).value === '') as HTMLSelectElement;
|
||||
if (concUnitSelect) {
|
||||
fireEvent.change(concUnitSelect, { target: { value: 'mg/ml' } });
|
||||
}
|
||||
|
||||
expect(screen.getByPlaceholderText('e.g. Pfizer')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes fields in the product edit inline form (manufacturer, notes)', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Glucophage',
|
||||
packageSize: 60,
|
||||
packageUnit: 'tablet',
|
||||
source: 'manual',
|
||||
manufacturer: '',
|
||||
notes: '',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Glucophage'));
|
||||
await userEvent.click(screen.getAllByTitle('Edit')[1]!);
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Glucophage'));
|
||||
|
||||
// Find the manufacturer input (empty string value, maxLength 200)
|
||||
const allInputs = document.querySelectorAll('input[maxLength="200"]');
|
||||
// First is brand (Glucophage), second is manufacturer
|
||||
if (allInputs.length > 1) {
|
||||
fireEvent.change(allInputs[1]!, { target: { value: 'Pfizer' } });
|
||||
// Clear manufacturer to cover the || undefined false branch
|
||||
fireEvent.change(allInputs[1]!, { target: { value: '' } });
|
||||
}
|
||||
|
||||
// Change notes (maxLength 1000 input)
|
||||
const notesInput = document.querySelector('input[maxLength="1000"]') as HTMLElement;
|
||||
if (notesInput) {
|
||||
fireEvent.change(notesInput, { target: { value: 'Keep refrigerated' } });
|
||||
}
|
||||
|
||||
expect(screen.getByDisplayValue('Glucophage')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('adjusts cabinet item quantity', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'ci-1',
|
||||
quantity: 10,
|
||||
unit: 'tablet',
|
||||
status: 'active',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAdjustCabinetItemQuantity.mockResolvedValue({});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Add 1'));
|
||||
await userEvent.click(screen.getByTitle('Add 1'));
|
||||
|
||||
expect(mockAdjustCabinetItemQuantity).toHaveBeenCalledWith('hh1', 'ci-1', { delta: 1 });
|
||||
});
|
||||
|
||||
it('takes 1 from cabinet item', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [{ _id: 'ci-1', quantity: 10, unit: 'tablet', status: 'active' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAdjustCabinetItemQuantity.mockResolvedValue({});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Take 1'));
|
||||
await userEvent.click(screen.getByTitle('Take 1'));
|
||||
|
||||
expect(mockAdjustCabinetItemQuantity).toHaveBeenCalledWith('hh1', 'ci-1', { delta: -1 });
|
||||
});
|
||||
|
||||
it('deletes cabinet item after confirmation', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [{ _id: 'ci-1', quantity: 10, unit: 'tablet', status: 'active' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteCabinetItem.mockResolvedValue({});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Delete'));
|
||||
await userEvent.click(screen.getByTitle('Delete'));
|
||||
|
||||
expect(mockDeleteCabinetItem).toHaveBeenCalledWith('hh1', 'ci-1');
|
||||
});
|
||||
|
||||
it('cancels CreateProductForm with internal Cancel button', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product', { selector: 'h3' }));
|
||||
// Click the Cancel button inside the form (last Cancel button in DOM)
|
||||
const cancelButtons = screen.getAllByText('Cancel');
|
||||
await userEvent.click(cancelButtons[cancelButtons.length - 1]!);
|
||||
|
||||
expect(screen.queryByText('Add Product', { selector: 'h3' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes all fields in edit medicine form', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. daily, morning'));
|
||||
|
||||
// Change name
|
||||
const nameInput = document.querySelector('input[maxLength="200"]') as HTMLElement;
|
||||
if (nameInput) fireEvent.change(nameInput, { target: { value: 'Metformin XR' } });
|
||||
|
||||
// Change form select
|
||||
const formSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).options[0]?.value === 'tablet') as HTMLSelectElement;
|
||||
if (formSelect) fireEvent.change(formSelect, { target: { value: 'capsule' } });
|
||||
|
||||
// Change strength
|
||||
const strengthInput = document.querySelector('input[min="0.01"]') as HTMLElement;
|
||||
if (strengthInput) fireEvent.change(strengthInput, { target: { value: '250' } });
|
||||
|
||||
// Change category select
|
||||
const catSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find(
|
||||
(s) => (s as HTMLSelectElement).options[0]?.value === 'prescription',
|
||||
) as HTMLSelectElement;
|
||||
if (catSelect) fireEvent.change(catSelect, { target: { value: 'otc' } });
|
||||
|
||||
// Change notes (truthy value)
|
||||
const notesInputs = document.querySelectorAll('input[maxLength="1000"]');
|
||||
if (notesInputs[0]) fireEvent.change(notesInputs[0]!, { target: { value: 'Take with food' } });
|
||||
// Also clear notes (covers the || undefined false branch)
|
||||
if (notesInputs[0]) fireEvent.change(notesInputs[0]!, { target: { value: '' } });
|
||||
|
||||
// Change tags
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. daily, morning'), {
|
||||
target: { value: 'morning, daily' },
|
||||
});
|
||||
|
||||
expect(screen.getByPlaceholderText('e.g. daily, morning')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when update medicine fails', async () => {
|
||||
mockUpdateMedicine.mockRejectedValue(new Error('Update failed'));
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Update failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses error in detail page', async () => {
|
||||
mockUpdateMedicine.mockRejectedValue(new Error('Update failed'));
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
await waitFor(() => screen.getByText('Update failed'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('Update failed')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes strength unit in edit medicine form', async () => {
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Edit'));
|
||||
await userEvent.click(screen.getByTitle('Edit'));
|
||||
|
||||
await waitFor(() => screen.getByText('Edit Medicine'));
|
||||
// Change strength unit select (the one with 'mg' options)
|
||||
const unitSelect = screen
|
||||
.getAllByRole('combobox')
|
||||
.find((s) => (s as HTMLSelectElement).value === 'mg') as HTMLSelectElement;
|
||||
if (unitSelect) fireEvent.change(unitSelect, { target: { value: 'mcg' } });
|
||||
|
||||
expect(screen.getByText('Edit Medicine')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('cancels product edit form', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [{ _id: 'mp-1', brand: 'Bayer', packageSize: 100, unit: 'tablet', form: 'tablet' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Bayer'));
|
||||
const editButtons = screen.getAllByTitle('Edit');
|
||||
await userEvent.click(editButtons[editButtons.length - 1]!);
|
||||
await waitFor(() => screen.getByDisplayValue('Bayer'));
|
||||
|
||||
// Click Cancel to close product edit form
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
||||
|
||||
expect(screen.queryByDisplayValue('Bayer')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on create product', async () => {
|
||||
mockCreateMedicineProduct.mockRejectedValue('create failed');
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Product'));
|
||||
await userEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. CVS Health'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. CVS Health'), {
|
||||
target: { value: 'Brand X' },
|
||||
});
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. CVS Health').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to create product')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on update product', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [{ _id: 'mp-1', brand: 'Bayer', packageSize: 100, unit: 'tablet', form: 'tablet' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateMedicineProduct.mockRejectedValue('update failed');
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Bayer'));
|
||||
// Click the product Edit button (last Edit button, not the medicine one)
|
||||
const editButtons = screen.getAllByTitle('Edit');
|
||||
await userEvent.click(editButtons[editButtons.length - 1]!);
|
||||
await waitFor(() => screen.getByDisplayValue('Bayer'));
|
||||
fireEvent.submit(screen.getByDisplayValue('Bayer').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to update product')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on delete product', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [{ _id: 'mp-1', brand: 'Bayer', packageSize: 100, unit: 'tablet', form: 'tablet' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteMedicineProduct.mockRejectedValue('delete failed');
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Bayer'));
|
||||
await userEvent.click(screen.getAllByTitle('Delete')[0]!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to delete product')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on adjust cabinet', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [{ _id: 'ci-1', quantity: 10, unit: 'tablet', status: 'active' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAdjustCabinetItemQuantity.mockRejectedValue('adjust failed');
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByTitle('Take 1'));
|
||||
await userEvent.click(screen.getByTitle('Take 1'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to adjust quantity')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows product concentration and manufacturer in display', async () => {
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'mp-1',
|
||||
brand: 'Lantus',
|
||||
packageSize: 10,
|
||||
packageUnit: 'ml',
|
||||
concentration: 100,
|
||||
concentrationUnit: 'IU/ml',
|
||||
manufacturer: 'Sanofi',
|
||||
notes: 'Refrigerate after opening',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Lantus'));
|
||||
expect(screen.getByText(/100 IU\/ml/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Sanofi/)).toBeInTheDocument();
|
||||
expect(screen.getByText('Refrigerate after opening')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('clears concentration and notes in edit inline form for injection', async () => {
|
||||
mockGetMedicine.mockResolvedValue({ ...medicine, form: 'injection' });
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'prod-1',
|
||||
brand: 'Lantus',
|
||||
packageSize: 10,
|
||||
packageUnit: 'ml',
|
||||
source: 'manual',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Lantus'));
|
||||
await userEvent.click(screen.getAllByTitle('Edit')[1]!);
|
||||
|
||||
await waitFor(() => screen.getByDisplayValue('Lantus'));
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. 100'));
|
||||
|
||||
// Set concentration then clear (covers undefined branch at line 675)
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '100' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '' } });
|
||||
|
||||
// Set notes then clear (covers undefined branch at line 720)
|
||||
const notesInput = document.querySelector('input[maxLength="1000"]') as HTMLElement;
|
||||
if (notesInput) fireEvent.change(notesInput, { target: { value: 'Refrigerate' } });
|
||||
if (notesInput) fireEvent.change(notesInput, { target: { value: '' } });
|
||||
|
||||
expect(screen.getByDisplayValue('Lantus')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on delete cabinet', async () => {
|
||||
mockListCabinetItems.mockResolvedValue({
|
||||
data: [{ _id: 'ci-1', quantity: 10, unit: 'tablet', status: 'active' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteCabinetItem.mockRejectedValue('delete failed');
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<MedicineDetailPage />);
|
||||
|
||||
await waitFor(() => screen.getAllByTitle('Delete'));
|
||||
await userEvent.click(screen.getAllByTitle('Delete')[0]!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('Failed to delete cabinet item')).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
vi.mock('@/app/(dashboard)/medicines/ActivityTab', () => ({
|
||||
ActivityTab: ({ householdId }: { householdId: string }) => (
|
||||
<div data-testid="activity-tab">{householdId}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
import ActivityPage from '../../../../../src/app/(dashboard)/medicines/activity/page';
|
||||
|
||||
describe('ActivityPage', () => {
|
||||
it('shows skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<ActivityPage />);
|
||||
expect(screen.getByText('Cabinet Activity')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('activity-tab')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<ActivityPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders ActivityTab when householdId exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<ActivityPage />);
|
||||
expect(screen.getByTestId('activity-tab')).toHaveTextContent('hh1');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
vi.mock('@/app/(dashboard)/medicines/CabinetTab', () => ({
|
||||
CabinetTab: ({ householdId }: { householdId: string }) => (
|
||||
<div data-testid="cabinet-tab">{householdId}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
import CabinetPage from '../../../../../src/app/(dashboard)/medicines/cabinet/page';
|
||||
|
||||
describe('CabinetPage', () => {
|
||||
it('shows skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<CabinetPage />);
|
||||
expect(screen.getByText('Medicine Cabinet')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('cabinet-tab')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<CabinetPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders CabinetTab when householdId exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<CabinetPage />);
|
||||
expect(screen.getByTestId('cabinet-tab')).toHaveTextContent('hh1');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
vi.mock('@/app/(dashboard)/medicines/LibraryTab', () => ({
|
||||
LibraryTab: ({ householdId }: { householdId: string }) => (
|
||||
<div data-testid="library-tab">{householdId}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
import LibraryPage from '../../../../../src/app/(dashboard)/medicines/library/page';
|
||||
|
||||
describe('LibraryPage', () => {
|
||||
it('shows skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<LibraryPage />);
|
||||
expect(screen.getByText('Medicine Library')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('library-tab')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<LibraryPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders LibraryTab when householdId exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<LibraryPage />);
|
||||
expect(screen.getByTestId('library-tab')).toHaveTextContent('hh1');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
vi.mock('@/app/(dashboard)/medicines/OrganizerTab', () => ({
|
||||
OrganizerTab: ({ householdId }: { householdId: string }) => (
|
||||
<div data-testid="organizer-tab">{householdId}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
import OrganizerPage from '../../../../../src/app/(dashboard)/medicines/organizer/page';
|
||||
|
||||
describe('OrganizerPage', () => {
|
||||
it('shows skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<OrganizerPage />);
|
||||
expect(screen.getByText('Pill Organizer')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('organizer-tab')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<OrganizerPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders OrganizerTab when householdId exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<OrganizerPage />);
|
||||
expect(screen.getByTestId('organizer-tab')).toHaveTextContent('hh1');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('next/link', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return { default: (props: any) => props.children };
|
||||
});
|
||||
|
||||
import MedicinesPage from '../../../../src/app/(dashboard)/medicines/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
describe('MedicinesPage', () => {
|
||||
it('shows skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<MedicinesPage />);
|
||||
|
||||
expect(screen.getByText('Medicines')).toBeInTheDocument();
|
||||
// Should show skeleton, not section cards
|
||||
expect(screen.queryByText('Library')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
|
||||
render(<MedicinesPage />);
|
||||
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders section cards when household exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
|
||||
render(<MedicinesPage />);
|
||||
|
||||
expect(screen.getByText('Library')).toBeInTheDocument();
|
||||
expect(screen.getByText('Cabinet')).toBeInTheDocument();
|
||||
expect(screen.getByText('Regimens')).toBeInTheDocument();
|
||||
expect(screen.getByText('Organizer')).toBeInTheDocument();
|
||||
expect(screen.getByText('Activity')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
vi.mock('@/app/(dashboard)/medicines/RegimensTab', () => ({
|
||||
RegimensTab: ({ householdId }: { householdId: string }) => (
|
||||
<div data-testid="regimens-tab">{householdId}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
import RegimensPage from '../../../../../src/app/(dashboard)/medicines/regimens/page';
|
||||
|
||||
describe('RegimensPage', () => {
|
||||
it('shows skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<RegimensPage />);
|
||||
expect(screen.getByText('Regimens')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('regimens-tab')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<RegimensPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders RegimensTab when householdId exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<RegimensPage />);
|
||||
expect(screen.getByTestId('regimens-tab')).toHaveTextContent('hh1');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,305 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListRegimens } = vi.hoisted(() => ({
|
||||
mockListRegimens: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/regimens', () => ({
|
||||
listRegimens: mockListRegimens,
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import SchedulePage from '../../../../../src/app/(dashboard)/medicines/schedule/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const makeRegimen = (overrides = {}) => ({
|
||||
_id: 'reg1',
|
||||
householdId: 'hh1',
|
||||
name: 'Daily Vitamins',
|
||||
isActive: true,
|
||||
startDate: '2026-01-01',
|
||||
medications: [
|
||||
{
|
||||
medicineId: 'm1',
|
||||
medicineName: 'Vitamin D',
|
||||
medicineStrength: '1000',
|
||||
medicineStrengthUnit: 'IU',
|
||||
dosage: 1,
|
||||
dosageUnit: 'tablet',
|
||||
frequency: 'daily',
|
||||
timeOfDay: 'morning',
|
||||
},
|
||||
],
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe('SchedulePage', () => {
|
||||
it('shows loading skeleton when session is loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
expect(screen.getByText('Schedule & Log')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows no household message', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty state when no regimens', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('No active regimens found.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows set up link in empty state', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Set up a regimen')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders medication in morning slot', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [makeRegimen()],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Vitamin D')).toBeInTheDocument();
|
||||
expect(screen.getByText('Morning')).toBeInTheDocument();
|
||||
expect(screen.getByText('1000 IU')).toBeInTheDocument();
|
||||
expect(screen.getByText('Daily Vitamins')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows frequency label', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [makeRegimen()],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/Once daily/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows custom frequency', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [
|
||||
makeRegimen({
|
||||
medications: [
|
||||
{
|
||||
medicineId: 'm1',
|
||||
medicineName: 'Custom Med',
|
||||
medicineStrength: '50',
|
||||
medicineStrengthUnit: 'mg',
|
||||
dosage: 1,
|
||||
dosageUnit: 'tablet',
|
||||
frequency: 'custom',
|
||||
customFrequencyPerDay: 4,
|
||||
timeOfDay: 'morning',
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/4x daily/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows instructions when present', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [
|
||||
makeRegimen({
|
||||
medications: [
|
||||
{
|
||||
medicineId: 'm1',
|
||||
medicineName: 'Med A',
|
||||
medicineStrength: '10',
|
||||
medicineStrengthUnit: 'mg',
|
||||
dosage: 1,
|
||||
dosageUnit: 'pill',
|
||||
frequency: 'daily',
|
||||
timeOfDay: 'evening',
|
||||
instructions: 'Take with food',
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Take with food')).toBeInTheDocument();
|
||||
expect(screen.getByText('Evening')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows dose count', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [makeRegimen()],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('1 dose')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows error when fetch fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Network error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows fallback error for non-Error failures', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockRejectedValue('unexpected');
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to load regimens')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('groups into "any" slot when timeOfDay is missing', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [
|
||||
makeRegimen({
|
||||
medications: [
|
||||
{
|
||||
medicineId: 'm2',
|
||||
medicineName: 'Aspirin',
|
||||
medicineStrength: '100',
|
||||
medicineStrengthUnit: 'mg',
|
||||
dosage: 1,
|
||||
dosageUnit: 'tablet',
|
||||
frequency: 'as_needed',
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Aspirin')).toBeInTheDocument();
|
||||
expect(screen.getByText('Any time')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('paginates through regimens', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens
|
||||
.mockResolvedValueOnce({
|
||||
data: [makeRegimen()],
|
||||
pagination: { cursor: 'next', hasMore: true },
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
data: [
|
||||
makeRegimen({
|
||||
_id: 'reg2',
|
||||
name: 'Second Regimen',
|
||||
medications: [
|
||||
{
|
||||
medicineId: 'm2',
|
||||
medicineName: 'Iron',
|
||||
medicineStrength: '65',
|
||||
medicineStrengthUnit: 'mg',
|
||||
dosage: 1,
|
||||
dosageUnit: 'tablet',
|
||||
frequency: 'daily',
|
||||
timeOfDay: 'afternoon',
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Vitamin D')).toBeInTheDocument();
|
||||
expect(screen.getByText('Iron')).toBeInTheDocument();
|
||||
});
|
||||
expect(mockListRegimens).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('shows regimen and dose summary', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRegimens.mockResolvedValue({
|
||||
data: [makeRegimen()],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<SchedulePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/1 active regimen/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/1 dose per day/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,322 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import { SWRConfig } from 'swr';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListPantryItems, mockTransitionPantryItem, mockDeletePantryItem } = vi.hoisted(() => ({
|
||||
mockListPantryItems: vi.fn(),
|
||||
mockTransitionPantryItem: vi.fn(),
|
||||
mockDeletePantryItem: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/pantry', () => ({
|
||||
listPantryItems: mockListPantryItems,
|
||||
transitionPantryItem: mockTransitionPantryItem,
|
||||
deletePantryItem: mockDeletePantryItem,
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return { default: (props: any) => props.children };
|
||||
});
|
||||
|
||||
import PantryPage from '../../../../src/app/(dashboard)/pantry/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<SWRConfig value={{ provider: () => new Map(), dedupingInterval: 0 }}>{children}</SWRConfig>
|
||||
);
|
||||
|
||||
const SAMPLE_ITEM = {
|
||||
_id: 'pi-1',
|
||||
householdId: 'hh1',
|
||||
productId: 'p1',
|
||||
productName: 'Whole Milk',
|
||||
storageLocation: 'fridge',
|
||||
quantity: 1,
|
||||
unit: 'liter',
|
||||
purchaseDate: '2026-05-01T00:00:00.000Z',
|
||||
status: 'sealed',
|
||||
freshnessEstimate: {
|
||||
estimatedExpiryDate: '2026-05-15T00:00:00.000Z',
|
||||
daysRemaining: 11,
|
||||
urgency: 'fresh',
|
||||
source: 'rule',
|
||||
},
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-05-01T00:00:00.000Z',
|
||||
updatedAt: '2026-05-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
describe('PantryPage', () => {
|
||||
it('shows loading skeleton when session is loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
expect(screen.getByText('Pantry')).toBeInTheDocument();
|
||||
expect(screen.queryByText('All')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders pantry items when household exists', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Whole Milk')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows empty state when no items', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/No pantry items yet/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows freshness badge', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Fresh')).toBeInTheDocument();
|
||||
expect(screen.getByText('11d left')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows overdue text for negative days', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
...SAMPLE_ITEM,
|
||||
_id: 'pi-2',
|
||||
freshnessEstimate: {
|
||||
...SAMPLE_ITEM.freshnessEstimate,
|
||||
daysRemaining: -2,
|
||||
urgency: 'expired',
|
||||
},
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('2d overdue')).toBeInTheDocument();
|
||||
expect(screen.getByText('Expired')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows transition buttons for active items', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Open')).toBeInTheDocument();
|
||||
expect(screen.getByText('Consume')).toBeInTheDocument();
|
||||
expect(screen.getByText('Discard')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('handles transition click', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockTransitionPantryItem.mockResolvedValue({
|
||||
...SAMPLE_ITEM,
|
||||
status: 'opened',
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Open')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Open'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockTransitionPantryItem).toHaveBeenCalledWith('hh1', 'pi-1', { status: 'opened' });
|
||||
});
|
||||
});
|
||||
|
||||
it('handles delete with confirmation', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeletePantryItem.mockResolvedValue(undefined);
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Delete'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockDeletePantryItem).toHaveBeenCalledWith('hh1', 'pi-1');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not delete when confirm is cancelled', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Delete'));
|
||||
|
||||
expect(mockDeletePantryItem).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('filters by storage tab', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Fridge')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Fridge'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockListPantryItems).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ storageLocation: 'fridge' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('filters by status select', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByDisplayValue('All statuses')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.change(screen.getByDisplayValue('All statuses'), { target: { value: 'opened' } });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockListPantryItems).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ status: 'opened' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('shows error when fetch fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
render(<PantryPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Network error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows error when transition fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [SAMPLE_ITEM],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockTransitionPantryItem.mockRejectedValue(new Error('Transition failed'));
|
||||
|
||||
render(<PantryPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Open')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Open'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Transition failed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows filter empty state message', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPantryItems.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PantryPage />);
|
||||
|
||||
// Click a filter tab to trigger filter state
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Fridge')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Fridge'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/No items match your filters/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,156 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
|
||||
const { mockImportProducts } = vi.hoisted(() => ({
|
||||
mockImportProducts: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/products', () => ({
|
||||
importProducts: mockImportProducts,
|
||||
}));
|
||||
|
||||
import { ImportDialog } from '../../../../src/app/(dashboard)/products/ImportDialog';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const defaultProps = {
|
||||
open: true,
|
||||
onClose: vi.fn(),
|
||||
householdId: 'hh1',
|
||||
onSuccess: vi.fn(),
|
||||
};
|
||||
|
||||
describe('ImportDialog', () => {
|
||||
it('renders nothing when closed', () => {
|
||||
const { container } = render(<ImportDialog {...defaultProps} open={false} />);
|
||||
expect(container.innerHTML).toBe('');
|
||||
});
|
||||
|
||||
it('renders Import Products title when open', () => {
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
expect(screen.getByText('Import Products')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows file chooser button', () => {
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
expect(screen.getByText('Choose .csv or .json file...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('Upload button is disabled without file', () => {
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
expect(screen.getByText('Upload')).toBeDisabled();
|
||||
});
|
||||
|
||||
it('shows file name after selection', () => {
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['name,category\nTest,other'], 'products.csv', {
|
||||
type: 'text/csv',
|
||||
});
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
|
||||
expect(screen.getByText('products.csv')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('uploads file and shows success result', async () => {
|
||||
mockImportProducts.mockResolvedValue({
|
||||
imported: 5,
|
||||
skippedDuplicates: 1,
|
||||
errors: [],
|
||||
});
|
||||
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['data'], 'products.csv', { type: 'text/csv' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
fireEvent.click(screen.getByText('Upload'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Import complete')).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByText('5')).toBeInTheDocument();
|
||||
expect(screen.getByText('1')).toBeInTheDocument();
|
||||
expect(defaultProps.onSuccess).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows errors in result', async () => {
|
||||
mockImportProducts.mockResolvedValue({
|
||||
imported: 0,
|
||||
skippedDuplicates: 0,
|
||||
errors: [{ row: 2, message: 'Missing name' }],
|
||||
});
|
||||
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['data'], 'products.csv', { type: 'text/csv' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
fireEvent.click(screen.getByText('Upload'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Import complete')).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByText(/Row 2: Missing name/)).toBeInTheDocument();
|
||||
expect(defaultProps.onSuccess).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows error on import failure', async () => {
|
||||
mockImportProducts.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['data'], 'products.csv', { type: 'text/csv' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
fireEvent.click(screen.getByText('Upload'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Network error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows fallback error on non-Error rejection', async () => {
|
||||
mockImportProducts.mockRejectedValue('unknown');
|
||||
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['data'], 'products.csv', { type: 'text/csv' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
fireEvent.click(screen.getByText('Upload'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Import failed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('closes dialog via Cancel button', () => {
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
fireEvent.click(screen.getByText('Cancel'));
|
||||
expect(defaultProps.onClose).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('closes dialog via Close button after result', async () => {
|
||||
mockImportProducts.mockResolvedValue({
|
||||
imported: 1,
|
||||
skippedDuplicates: 0,
|
||||
errors: [],
|
||||
});
|
||||
|
||||
render(<ImportDialog {...defaultProps} />);
|
||||
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['data'], 'products.csv', { type: 'text/csv' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
fireEvent.click(screen.getByText('Upload'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Import complete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Close'));
|
||||
expect(defaultProps.onClose).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,280 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
|
||||
const { mockLookupBarcode } = vi.hoisted(() => ({
|
||||
mockLookupBarcode: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/products', () => ({
|
||||
lookupBarcode: mockLookupBarcode,
|
||||
}));
|
||||
|
||||
import { ProductModal } from '../../../../src/app/(dashboard)/products/ProductModal';
|
||||
import { ProductCategory, ServingUnit } from '@meshitrack/shared';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const defaultProps = {
|
||||
open: true,
|
||||
onClose: vi.fn(),
|
||||
onSave: vi.fn().mockResolvedValue(undefined),
|
||||
householdId: 'hh1',
|
||||
};
|
||||
|
||||
describe('ProductModal', () => {
|
||||
it('renders nothing when closed', () => {
|
||||
const { container } = render(<ProductModal {...defaultProps} open={false} />);
|
||||
expect(container.innerHTML).toBe('');
|
||||
});
|
||||
|
||||
it('renders Add Product title by default', () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
expect(screen.getByText('Add Product')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders custom title when provided', () => {
|
||||
render(<ProductModal {...defaultProps} title="Custom Title" />);
|
||||
expect(screen.getByText('Custom Title')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders Edit Product title when initial is provided', () => {
|
||||
render(<ProductModal {...defaultProps} initial={{ name: 'Test' }} />);
|
||||
expect(screen.getByText('Edit Product')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('pre-fills form fields from initial', () => {
|
||||
render(
|
||||
<ProductModal
|
||||
{...defaultProps}
|
||||
initial={{
|
||||
name: 'Chicken',
|
||||
brand: 'Tyson',
|
||||
barcode: '12345678',
|
||||
category: ProductCategory.MEAT,
|
||||
servingSize: 100,
|
||||
servingUnit: ServingUnit.GRAMS,
|
||||
tags: ['organic', 'protein'],
|
||||
nutrition: { calories: 165, protein: 31, carbs: 0, fat: 3.6 },
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByDisplayValue('Chicken')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('Tyson')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('12345678')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('100')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('organic, protein')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('165')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when name is empty and form submitted', async () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
expect(screen.getByText('Name is required')).toBeInTheDocument();
|
||||
expect(defaultProps.onSave).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows error when serving size is empty', async () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
expect(screen.getByText('Serving size must be a positive number')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when nutrition fields are missing', async () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), {
|
||||
target: { value: '100' },
|
||||
});
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
expect(
|
||||
screen.getByText('Calories, protein, carbs, and fat are required'),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onSave with correct data on valid submit', async () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Chicken Breast' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), {
|
||||
target: { value: '100' },
|
||||
});
|
||||
|
||||
// Fill nutrition
|
||||
const numberInputs = screen.getAllByRole('spinbutton');
|
||||
// serving size is index 0, density=1, calories=2, protein=3, carbs=4, fat=5
|
||||
fireEvent.change(numberInputs[2]!, { target: { value: '165' } });
|
||||
fireEvent.change(numberInputs[3]!, { target: { value: '31' } });
|
||||
fireEvent.change(numberInputs[4]!, { target: { value: '0' } });
|
||||
fireEvent.change(numberInputs[5]!, { target: { value: '3.6' } });
|
||||
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(defaultProps.onSave).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
name: 'Chicken Breast',
|
||||
servingSize: 100,
|
||||
nutrition: expect.objectContaining({
|
||||
calories: 165,
|
||||
protein: 31,
|
||||
carbs: 0,
|
||||
fat: 3.6,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('closes modal after successful save', async () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '50' } });
|
||||
|
||||
const numberInputs = screen.getAllByRole('spinbutton');
|
||||
fireEvent.change(numberInputs[2]!, { target: { value: '10' } });
|
||||
fireEvent.change(numberInputs[3]!, { target: { value: '5' } });
|
||||
fireEvent.change(numberInputs[4]!, { target: { value: '2' } });
|
||||
fireEvent.change(numberInputs[5]!, { target: { value: '1' } });
|
||||
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(defaultProps.onClose).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows error when onSave rejects', async () => {
|
||||
const saveFn = vi.fn().mockRejectedValue(new Error('Server error'));
|
||||
render(<ProductModal {...defaultProps} onSave={saveFn} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '50' } });
|
||||
|
||||
const numberInputs = screen.getAllByRole('spinbutton');
|
||||
fireEvent.change(numberInputs[2]!, { target: { value: '10' } });
|
||||
fireEvent.change(numberInputs[3]!, { target: { value: '5' } });
|
||||
fireEvent.change(numberInputs[4]!, { target: { value: '2' } });
|
||||
fireEvent.change(numberInputs[5]!, { target: { value: '1' } });
|
||||
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Server error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows fallback error when onSave throws non-Error', async () => {
|
||||
const saveFn = vi.fn().mockRejectedValue('unknown');
|
||||
render(<ProductModal {...defaultProps} onSave={saveFn} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. 100'), { target: { value: '50' } });
|
||||
|
||||
const numberInputs = screen.getAllByRole('spinbutton');
|
||||
fireEvent.change(numberInputs[2]!, { target: { value: '10' } });
|
||||
fireEvent.change(numberInputs[3]!, { target: { value: '5' } });
|
||||
fireEvent.change(numberInputs[4]!, { target: { value: '2' } });
|
||||
fireEvent.change(numberInputs[5]!, { target: { value: '1' } });
|
||||
|
||||
fireEvent.click(screen.getByText('Save'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to save')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('closes when Cancel is clicked', () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
fireEvent.click(screen.getByText('Cancel'));
|
||||
expect(defaultProps.onClose).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('closes when backdrop is clicked', () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
// Click the backdrop (outermost div with onClick=onClose)
|
||||
const backdrop = screen.getByText('Add Product').closest('div')!.parentElement!;
|
||||
fireEvent.click(backdrop);
|
||||
expect(defaultProps.onClose).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Lookup button is disabled when barcode is empty', () => {
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
const lookupBtn = screen.getByText('Lookup');
|
||||
expect(lookupBtn).toBeDisabled();
|
||||
});
|
||||
|
||||
it('barcode lookup fills form on success', async () => {
|
||||
mockLookupBarcode.mockResolvedValue({
|
||||
_id: 'p1',
|
||||
householdId: 'hh1',
|
||||
name: 'Nutella',
|
||||
brand: 'Ferrero',
|
||||
category: 'snacks',
|
||||
servingSize: 15,
|
||||
servingUnit: 'g',
|
||||
nutrition: { calories: 80, protein: 1, carbs: 8.5, fat: 4.7 },
|
||||
tags: ['spread'],
|
||||
source: 'barcode_lookup',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00Z',
|
||||
updatedAt: '2026-01-01T00:00:00Z',
|
||||
});
|
||||
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('8-14 digits'), {
|
||||
target: { value: '3017620422003' },
|
||||
});
|
||||
fireEvent.click(screen.getByText('Lookup'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByDisplayValue('Nutella')).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByDisplayValue('Ferrero')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('80')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('barcode lookup shows not found', async () => {
|
||||
mockLookupBarcode.mockResolvedValue({ found: false });
|
||||
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('8-14 digits'), {
|
||||
target: { value: '0000000000000' },
|
||||
});
|
||||
fireEvent.click(screen.getByText('Lookup'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Product not found for this barcode')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('barcode lookup shows error on failure', async () => {
|
||||
mockLookupBarcode.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
render(<ProductModal {...defaultProps} />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('8-14 digits'), {
|
||||
target: { value: '1234567890123' },
|
||||
});
|
||||
fireEvent.click(screen.getByText('Lookup'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Barcode lookup failed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,342 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListProducts, mockDeleteProduct, mockCreateProduct, mockUpdateProduct } = vi.hoisted(() => ({
|
||||
mockListProducts: vi.fn(),
|
||||
mockDeleteProduct: vi.fn(),
|
||||
mockCreateProduct: vi.fn(),
|
||||
mockUpdateProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/products', () => ({
|
||||
listProducts: mockListProducts,
|
||||
deleteProduct: mockDeleteProduct,
|
||||
createProduct: mockCreateProduct,
|
||||
updateProduct: mockUpdateProduct,
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return { default: (props: any) => props.children };
|
||||
});
|
||||
|
||||
import ProductsPage from '../../../../src/app/(dashboard)/products/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const SAMPLE_PRODUCT = {
|
||||
_id: 'p1',
|
||||
householdId: 'hh1',
|
||||
name: 'Chicken Breast',
|
||||
brand: 'Tyson',
|
||||
category: 'meat',
|
||||
servingSize: 100,
|
||||
servingUnit: 'g',
|
||||
nutrition: { calories: 165, protein: 31, carbs: 0, fat: 3.6 },
|
||||
tags: [],
|
||||
source: 'manual',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-05-01T00:00:00.000Z',
|
||||
updatedAt: '2026-05-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
describe('ProductsPage', () => {
|
||||
it('shows loading skeleton when session is loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
expect(screen.getByText('Product Library')).toBeInTheDocument();
|
||||
expect(screen.queryByText('All categories')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders products when household exists', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Chicken Breast')).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByText('Tyson')).toBeInTheDocument();
|
||||
expect(screen.getByText('165 kcal')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty state when no products', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/No products yet/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows error message on fetch failure', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Network error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('filters by search input with debounce', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => expect(mockListProducts).toHaveBeenCalledTimes(1));
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search products...');
|
||||
fireEvent.change(searchInput, { target: { value: 'chicken' } });
|
||||
|
||||
await waitFor(
|
||||
() => {
|
||||
expect(mockListProducts).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ q: 'chicken' }),
|
||||
);
|
||||
},
|
||||
{ timeout: 500 },
|
||||
);
|
||||
});
|
||||
|
||||
it('filters by category dropdown', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => expect(mockListProducts).toHaveBeenCalledTimes(1));
|
||||
|
||||
const categorySelect = screen.getByRole('combobox');
|
||||
fireEvent.change(categorySelect, { target: { value: 'meat' } });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockListProducts).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ category: 'meat' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('deletes product on confirm', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteProduct.mockResolvedValue(undefined);
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Chicken Breast')).toBeInTheDocument());
|
||||
|
||||
const deleteBtn = screen.getByRole('button', { name: /delete product/i });
|
||||
fireEvent.click(deleteBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockDeleteProduct).toHaveBeenCalledWith('hh1', 'p1');
|
||||
});
|
||||
expect(screen.queryByText('Chicken Breast')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not delete when confirm is cancelled', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Chicken Breast')).toBeInTheDocument());
|
||||
|
||||
const deleteBtn = screen.getByRole('button', { name: /delete product/i });
|
||||
fireEvent.click(deleteBtn);
|
||||
|
||||
expect(mockDeleteProduct).not.toHaveBeenCalled();
|
||||
expect(screen.getByText('Chicken Breast')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when delete fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteProduct.mockRejectedValue(new Error('Delete failed'));
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Chicken Breast')).toBeInTheDocument());
|
||||
|
||||
const deleteBtn = screen.getByRole('button', { name: /delete product/i });
|
||||
fireEvent.click(deleteBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete failed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows macro breakdown in product card', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Chicken Breast')).toBeInTheDocument());
|
||||
|
||||
expect(screen.getByText('P: 31g')).toBeInTheDocument();
|
||||
expect(screen.getByText('C: 0g')).toBeInTheDocument();
|
||||
expect(screen.getByText('F: 3.6g')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles import dialog open and close', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
await waitFor(() => expect(screen.getByText('Import')).toBeInTheDocument());
|
||||
|
||||
fireEvent.click(screen.getByText('Import'));
|
||||
expect(screen.getByText('Import Products')).toBeInTheDocument();
|
||||
|
||||
// Use button name or text inside the dialog container to close
|
||||
const cancelBtns = screen.getAllByRole('button', { name: /Cancel/i });
|
||||
fireEvent.click(cancelBtns[cancelBtns.length - 1]);
|
||||
});
|
||||
|
||||
it('toggles add product modal open and close', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
await waitFor(() => expect(screen.getByText('Add Product')).toBeInTheDocument());
|
||||
|
||||
fireEvent.click(screen.getByText('Add Product'));
|
||||
const headings = screen.getAllByRole('heading');
|
||||
expect(headings.some(h => h.textContent === 'Add Product')).toBe(true);
|
||||
|
||||
const cancelBtn = screen.getByRole('button', { name: /Cancel/i });
|
||||
fireEvent.click(cancelBtn);
|
||||
});
|
||||
|
||||
it('opens and closes edit product modal', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<ProductsPage />);
|
||||
await screen.findByText('Chicken Breast');
|
||||
|
||||
const editBtn = screen.getByRole('button', { name: /edit product/i });
|
||||
fireEvent.click(editBtn);
|
||||
|
||||
const headings = screen.getAllByRole('heading');
|
||||
expect(headings.some(h => h.textContent === 'Edit Product')).toBe(true);
|
||||
|
||||
const cancelBtn = screen.getByRole('button', { name: /Cancel/i });
|
||||
fireEvent.click(cancelBtn);
|
||||
});
|
||||
|
||||
it('submits add product modal successfully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateProduct.mockResolvedValue({ _id: 'pnew' });
|
||||
|
||||
render(<ProductsPage />);
|
||||
await screen.findByText('Add Product');
|
||||
fireEvent.click(screen.getByText('Add Product'));
|
||||
|
||||
const nameInput = screen.getByPlaceholderText('Product name');
|
||||
fireEvent.change(nameInput, { target: { value: 'Fresh Banana' } });
|
||||
|
||||
// Fill mandatory numeric inputs to satisfy form validation
|
||||
const numInputs = screen.getAllByRole('spinbutton');
|
||||
numInputs.forEach(input => {
|
||||
fireEvent.change(input, { target: { value: '100' } });
|
||||
});
|
||||
|
||||
const saveBtn = screen.getByRole('button', { name: /^Save$/i });
|
||||
fireEvent.click(saveBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockCreateProduct).toHaveBeenCalledWith('hh1', expect.objectContaining({ name: 'Fresh Banana' }));
|
||||
});
|
||||
});
|
||||
|
||||
it('submits edit product modal successfully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListProducts.mockResolvedValue({
|
||||
data: [SAMPLE_PRODUCT],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateProduct.mockResolvedValue({ _id: 'p1' });
|
||||
|
||||
render(<ProductsPage />);
|
||||
await screen.findByText('Chicken Breast');
|
||||
|
||||
const editBtn = screen.getByRole('button', { name: /edit product/i });
|
||||
fireEvent.click(editBtn);
|
||||
|
||||
const nameInput = screen.getByPlaceholderText('Product name');
|
||||
fireEvent.change(nameInput, { target: { value: 'Updated Chicken' } });
|
||||
|
||||
const saveBtn = screen.getByRole('button', { name: /^Save$/i });
|
||||
fireEvent.click(saveBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockUpdateProduct).toHaveBeenCalledWith('hh1', 'p1', expect.objectContaining({ name: 'Updated Chicken' }));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,572 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
const { mockListPurchases, mockCreatePurchase, mockReceivePurchase, mockDeletePurchase } =
|
||||
vi.hoisted(() => ({
|
||||
mockListPurchases: vi.fn(),
|
||||
mockCreatePurchase: vi.fn(),
|
||||
mockReceivePurchase: vi.fn(),
|
||||
mockDeletePurchase: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListStores } = vi.hoisted(() => ({ mockListStores: vi.fn() }));
|
||||
const { mockListMedicines, mockListMedicineProducts } = vi.hoisted(() => ({
|
||||
mockListMedicines: vi.fn(),
|
||||
mockListMedicineProducts: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('@/services/purchases', () => ({
|
||||
listPurchases: mockListPurchases,
|
||||
createPurchase: mockCreatePurchase,
|
||||
receivePurchase: mockReceivePurchase,
|
||||
deletePurchase: mockDeletePurchase,
|
||||
}));
|
||||
vi.mock('@/services/stores', () => ({ listStores: mockListStores }));
|
||||
vi.mock('@/services/medicines', () => ({
|
||||
listMedicines: mockListMedicines,
|
||||
listMedicineProducts: mockListMedicineProducts,
|
||||
}));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
|
||||
import PurchasesPage from '../../../../src/app/(dashboard)/purchases/page';
|
||||
|
||||
const emptyResponse = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockListStores.mockResolvedValue(emptyResponse);
|
||||
mockListMedicines.mockResolvedValue(emptyResponse);
|
||||
});
|
||||
|
||||
describe('PurchasesPage', () => {
|
||||
it('shows loading skeleton when session loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<PurchasesPage />);
|
||||
expect(screen.getByText('Purchases')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Record Purchase')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<PurchasesPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty state when no purchases', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No purchases recorded yet/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows Record Purchase button', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Record Purchase')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when purchases fail to load', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockRejectedValue(new Error('Network failure'));
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Network failure')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('renders ordered and received sections', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
status: 'ordered',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
{
|
||||
_id: 'p2',
|
||||
status: 'in_cabinet',
|
||||
purchasedAt: '2026-01-02T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-02T00:00:00.000Z',
|
||||
updatedAt: '2026-01-02T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Pending arrival')).toBeInTheDocument());
|
||||
// "Received" appears as a section heading (h2) — use getAllByText since PurchaseCard may also render it
|
||||
expect(screen.getAllByText('Received').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('toggles the create form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
// After clicking, both the header toggle and the form show "Cancel"
|
||||
expect(screen.getAllByRole('button', { name: 'Cancel' }).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('shows Load more button when hasMore', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: 'cur1', hasMore: true },
|
||||
});
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Load more')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows validation error when no store selected', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'st-1',
|
||||
name: 'Walgreens',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByText('Save Purchase'));
|
||||
// Submit without selecting a store
|
||||
fireEvent.submit(screen.getByRole('button', { name: 'Save Purchase' }).closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Please select a store.')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows validation error when no items', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'st-1',
|
||||
name: 'Walgreens',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByText('Save Purchase'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select store'), { target: { value: 'st-1' } });
|
||||
fireEvent.submit(screen.getByRole('button', { name: 'Save Purchase' }).closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByText('Add at least one item with a name and quantity.'),
|
||||
).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('receives a purchase after confirmation', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
status: 'ordered',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockReceivePurchase.mockResolvedValue({});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Mark as received'));
|
||||
await userEvent.click(screen.getByText('Mark as received'));
|
||||
|
||||
expect(mockReceivePurchase).toHaveBeenCalledWith('hh1', 'p1');
|
||||
});
|
||||
|
||||
it('deletes a purchase after confirmation', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
status: 'ordered',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeletePurchase.mockResolvedValue({});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Cancel order'));
|
||||
await userEvent.click(screen.getByText('Cancel order'));
|
||||
|
||||
expect(mockDeletePurchase).toHaveBeenCalledWith('hh1', 'p1');
|
||||
});
|
||||
|
||||
it('shows error when receive fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
status: 'ordered',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockReceivePurchase.mockRejectedValue(new Error('Receive failed'));
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Mark as received'));
|
||||
await userEvent.click(screen.getByText('Mark as received'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Receive failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when delete fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
status: 'ordered',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeletePurchase.mockRejectedValue(new Error('Delete failed'));
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Cancel order'));
|
||||
await userEvent.click(screen.getByText('Cancel order'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Delete failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('creates a purchase successfully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
mockListMedicineProducts.mockResolvedValue(emptyResponse);
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'st-1',
|
||||
name: 'Walgreens',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreatePurchase.mockResolvedValue({
|
||||
_id: 'p-new',
|
||||
status: 'in_cabinet',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByText('Save Purchase'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select store'), { target: { value: 'st-1' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('Brand / product name'), {
|
||||
target: { value: 'Aspirin' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('90'), { target: { value: '30' } });
|
||||
fireEvent.submit(screen.getByRole('button', { name: 'Save Purchase' }).closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreatePurchase).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ storeId: 'st-1' }),
|
||||
),
|
||||
);
|
||||
// Form hides after success
|
||||
await waitFor(() => expect(screen.queryByText('Save Purchase')).not.toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('cancels the create form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getAllByRole('button', { name: 'Cancel' }));
|
||||
// Click the form's Cancel button (inside the form)
|
||||
const cancelBtns = screen.getAllByRole('button', { name: 'Cancel' });
|
||||
await userEvent.click(cancelBtns[cancelBtns.length - 1]!);
|
||||
|
||||
expect(screen.queryByText('Save Purchase')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders purchase items with price data', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'p1',
|
||||
status: 'in_cabinet',
|
||||
storeName: 'Walgreens',
|
||||
purchasedAt: '2026-01-01T00:00:00.000Z',
|
||||
items: [
|
||||
{
|
||||
name: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
actualPrice: 5.99,
|
||||
currency: 'USD',
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Aspirin')).toBeInTheDocument());
|
||||
expect(screen.getByText(/5.99/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('loads more purchases when Load more is clicked', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: 'cur1', hasMore: true },
|
||||
});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Load more'));
|
||||
await userEvent.click(screen.getByText('Load more'));
|
||||
|
||||
expect(mockListPurchases).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('toggles online order checkbox and changes notes', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByLabelText('Online order (pending arrival)'));
|
||||
fireEvent.click(screen.getByLabelText('Online order (pending arrival)'));
|
||||
fireEvent.change(document.querySelector('input[maxLength="1000"]') as HTMLElement, {
|
||||
target: { value: 'Some notes' },
|
||||
});
|
||||
|
||||
expect(screen.getByLabelText('Online order (pending arrival)')).toBeChecked();
|
||||
});
|
||||
|
||||
it('adds and removes purchase items', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByText('Save Purchase'));
|
||||
const addItemBtn = screen.getByRole('button', { name: 'Add item' });
|
||||
await userEvent.click(addItemBtn);
|
||||
|
||||
// Now 2 items — remove button shows
|
||||
await waitFor(() => screen.getAllByText('Remove').length > 0);
|
||||
await userEvent.click(screen.getAllByText('Remove')[0]!);
|
||||
|
||||
expect(screen.queryByText('Remove')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes item price and currency fields', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('9.99'));
|
||||
fireEvent.change(screen.getByPlaceholderText('9.99'), { target: { value: '5.99' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('USD'), { target: { value: 'EUR' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('9.99')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes item name and unit fields', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Brand / product name'));
|
||||
fireEvent.change(screen.getByPlaceholderText('Brand / product name'), {
|
||||
target: { value: 'Aspirin' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('90'), { target: { value: '30' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('tablet'), { target: { value: 'capsule' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('Brand / product name')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('selects a medicine and product in the form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockListMedicineProducts.mockResolvedValue({
|
||||
data: [{ _id: 'prod-1', brand: 'Glucophage', packageSize: 60, packageUnit: 'tablet' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByText('Save Purchase'));
|
||||
// Select a medicine
|
||||
fireEvent.change(screen.getByDisplayValue('Select medicine'), { target: { value: 'med-1' } });
|
||||
|
||||
// Wait for products to load
|
||||
await waitFor(() => screen.getByDisplayValue('Select product'));
|
||||
|
||||
// Select a product
|
||||
fireEvent.change(screen.getByDisplayValue('Select product'), { target: { value: 'prod-1' } });
|
||||
|
||||
// Product details should auto-fill the form
|
||||
expect(screen.getByDisplayValue('60')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles product list failure gracefully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
mockListMedicines.mockResolvedValue({
|
||||
data: [{ _id: 'med-1', name: 'Metformin', strength: 500, strengthUnit: 'mg' }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockListMedicineProducts.mockRejectedValue(new Error('Products failed'));
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => screen.getByText('Save Purchase'));
|
||||
fireEvent.change(screen.getByDisplayValue('Select medicine'), { target: { value: 'med-1' } });
|
||||
|
||||
// Wait for products loading to finish (error is swallowed)
|
||||
await waitFor(() => expect(mockListMedicineProducts).toHaveBeenCalled());
|
||||
});
|
||||
|
||||
it('shows no stores message when store list is empty in form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockResolvedValue(emptyResponse);
|
||||
mockListStores.mockResolvedValue({ data: [], pagination: { cursor: null, hasMore: false } });
|
||||
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Record Purchase'));
|
||||
await userEvent.click(screen.getByText('Record Purchase'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText(/No stores yet/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses error', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListPurchases.mockRejectedValue(new Error('Oops'));
|
||||
render(<PurchasesPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Oops'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('Oops')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,262 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockCreateRecipe, mockUpdateRecipe } = vi.hoisted(() => ({
|
||||
mockCreateRecipe: vi.fn(),
|
||||
mockUpdateRecipe: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockPush, mockBack } = vi.hoisted(() => ({
|
||||
mockPush: vi.fn(),
|
||||
mockBack: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/recipes', () => ({
|
||||
createRecipe: mockCreateRecipe,
|
||||
updateRecipe: mockUpdateRecipe,
|
||||
listRecipes: vi.fn(),
|
||||
deleteRecipe: vi.fn(),
|
||||
getRecipe: vi.fn(),
|
||||
scaleRecipe: vi.fn(),
|
||||
importRecipeFromText: vi.fn(),
|
||||
importRecipeFromUrl: vi.fn(),
|
||||
listRecipesByProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: mockPush, back: mockBack }),
|
||||
useParams: () => ({ id: 'r1' }),
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import { RecipeEditor } from '../../../../src/app/(dashboard)/recipes/RecipeEditor';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
describe('RecipeEditor', () => {
|
||||
it('renders create form by default', () => {
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
expect(screen.getByPlaceholderText('Recipe name')).toBeInTheDocument();
|
||||
expect(screen.getByText('Create recipe')).toBeInTheDocument();
|
||||
expect(screen.getByText('Ingredients')).toBeInTheDocument();
|
||||
expect(screen.getByText('Instructions')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders edit form when existing recipe provided', () => {
|
||||
const existing = {
|
||||
_id: 'r1',
|
||||
name: 'Pasta',
|
||||
description: 'Good pasta',
|
||||
servings: 2,
|
||||
prepTime: 10,
|
||||
cookTime: 20,
|
||||
cuisine: 'Italian',
|
||||
tags: ['pasta', 'quick'],
|
||||
isFavorite: true,
|
||||
ingredients: [
|
||||
{
|
||||
productId: 'p1',
|
||||
productName: 'Spaghetti',
|
||||
quantity: 200,
|
||||
unit: 'g',
|
||||
originalQuantity: 200,
|
||||
originalUnit: 'g',
|
||||
isOptional: false,
|
||||
},
|
||||
],
|
||||
steps: [{ order: 1, instruction: 'Boil water', duration: 5, tip: 'Use salted water' }],
|
||||
perServingNutrition: { calories: 300, protein: 10, carbs: 40, fat: 8 },
|
||||
totalNutrition: { calories: 600, protein: 20, carbs: 80, fat: 16 },
|
||||
warnings: [],
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
householdId: 'hh1',
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
render(<RecipeEditor householdId="hh1" existing={existing as any} />);
|
||||
|
||||
expect(screen.getByDisplayValue('Pasta')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('Good pasta')).toBeInTheDocument();
|
||||
expect(screen.getByText('Save changes')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('submits create form', async () => {
|
||||
mockCreateRecipe.mockResolvedValue({ _id: 'new1' });
|
||||
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Recipe name'), {
|
||||
target: { value: 'New Recipe' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Flour' },
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Create recipe'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockCreateRecipe).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ name: 'New Recipe' }),
|
||||
);
|
||||
expect(mockPush).toHaveBeenCalledWith('/recipes/new1');
|
||||
});
|
||||
});
|
||||
|
||||
it('submits update form', async () => {
|
||||
mockUpdateRecipe.mockResolvedValue({ _id: 'r1' });
|
||||
const existing = {
|
||||
_id: 'r1',
|
||||
name: 'Old Name',
|
||||
description: '',
|
||||
servings: 4,
|
||||
tags: [],
|
||||
isFavorite: false,
|
||||
ingredients: [
|
||||
{
|
||||
productId: 'p1',
|
||||
productName: 'Test',
|
||||
quantity: 100,
|
||||
unit: 'g',
|
||||
isOptional: false,
|
||||
},
|
||||
],
|
||||
steps: [{ order: 1, instruction: 'Do thing' }],
|
||||
perServingNutrition: { calories: 100, protein: 5, carbs: 10, fat: 3 },
|
||||
totalNutrition: { calories: 400, protein: 20, carbs: 40, fat: 12 },
|
||||
warnings: [],
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
householdId: 'hh1',
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
render(<RecipeEditor householdId="hh1" existing={existing as any} />);
|
||||
|
||||
fireEvent.change(screen.getByDisplayValue('Old Name'), {
|
||||
target: { value: 'New Name' },
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Save changes'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockUpdateRecipe).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
'r1',
|
||||
expect.objectContaining({ name: 'New Name' }),
|
||||
);
|
||||
expect(mockPush).toHaveBeenCalledWith('/recipes/r1');
|
||||
});
|
||||
});
|
||||
|
||||
it('shows error when create fails', async () => {
|
||||
mockCreateRecipe.mockRejectedValue(new Error('Server error'));
|
||||
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Recipe name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Item' },
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Create recipe'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Server error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows fallback error for non-Error failure', async () => {
|
||||
mockCreateRecipe.mockRejectedValue('unexpected');
|
||||
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Recipe name'), {
|
||||
target: { value: 'Test' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('Product name'), {
|
||||
target: { value: 'Item' },
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Create recipe'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to save recipe')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('can add and remove ingredients', () => {
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.click(screen.getByText('+ Add ingredient'));
|
||||
|
||||
const nameInputs = screen.getAllByPlaceholderText('Product name');
|
||||
expect(nameInputs).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('can add and remove steps', () => {
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.click(screen.getByText('+ Add step'));
|
||||
|
||||
const stepInputs = screen.getAllByPlaceholderText('Instruction...');
|
||||
expect(stepInputs).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('cancel button calls router.back', () => {
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.click(screen.getByText('Cancel'));
|
||||
|
||||
expect(mockBack).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles updating ingredient optional properties and removing ingredients', () => {
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.click(screen.getByText('+ Add ingredient'));
|
||||
|
||||
const prepInputs = screen.getAllByPlaceholderText(/Preparation/i);
|
||||
fireEvent.change(prepInputs[0], { target: { value: 'Diced' } });
|
||||
|
||||
const removeBtns = screen.getAllByRole('button', { name: '×' });
|
||||
fireEvent.click(removeBtns[0]);
|
||||
|
||||
const remainingPrepInputs = screen.getAllByPlaceholderText(/Preparation/i);
|
||||
expect(remainingPrepInputs).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('handles step metadata and removing steps', () => {
|
||||
render(<RecipeEditor householdId="hh1" />);
|
||||
|
||||
fireEvent.click(screen.getByText('+ Add step'));
|
||||
|
||||
const durationInputs = screen.getAllByPlaceholderText(/Duration/i);
|
||||
const tipInputs = screen.getAllByPlaceholderText(/Tip/i);
|
||||
|
||||
fireEvent.change(durationInputs[0], { target: { value: '15' } });
|
||||
fireEvent.change(tipInputs[0], { target: { value: "Don't burn it" } });
|
||||
|
||||
const removeBtns = screen.getAllByRole('button', { name: '×' });
|
||||
fireEvent.click(removeBtns[removeBtns.length - 1]);
|
||||
|
||||
const remainingDurationInputs = screen.getAllByPlaceholderText(/Duration/i);
|
||||
expect(remainingDurationInputs).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockGetRecipe } = vi.hoisted(() => ({
|
||||
mockGetRecipe: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/recipes', () => ({
|
||||
getRecipe: mockGetRecipe,
|
||||
createRecipe: vi.fn(),
|
||||
updateRecipe: vi.fn(),
|
||||
listRecipes: vi.fn(),
|
||||
deleteRecipe: vi.fn(),
|
||||
scaleRecipe: vi.fn(),
|
||||
importRecipeFromText: vi.fn(),
|
||||
importRecipeFromUrl: vi.fn(),
|
||||
listRecipesByProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useParams: () => ({ id: 'r1' }),
|
||||
useRouter: () => ({ push: vi.fn(), back: vi.fn() }),
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import EditRecipePage from '../../../../../../src/app/(dashboard)/recipes/[id]/edit/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const SAMPLE_RECIPE = {
|
||||
_id: 'r1',
|
||||
name: 'Pasta',
|
||||
description: '',
|
||||
servings: 4,
|
||||
tags: [],
|
||||
isFavorite: false,
|
||||
ingredients: [
|
||||
{ productId: 'p1', productName: 'Flour', quantity: 200, unit: 'g', isOptional: false },
|
||||
],
|
||||
steps: [{ order: 1, instruction: 'Mix' }],
|
||||
perServingNutrition: { calories: 200, protein: 5, carbs: 30, fat: 4 },
|
||||
totalNutrition: { calories: 800, protein: 20, carbs: 120, fat: 16 },
|
||||
warnings: [],
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
householdId: 'hh1',
|
||||
};
|
||||
|
||||
describe('EditRecipePage', () => {
|
||||
it('shows loading state', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<EditRecipePage />);
|
||||
|
||||
expect(screen.getByText('Edit Recipe')).toBeInTheDocument();
|
||||
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when fetch fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockRejectedValue(new Error('Not found'));
|
||||
|
||||
render(<EditRecipePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Not found')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders editor when recipe loads', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<EditRecipePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByDisplayValue('Pasta')).toBeInTheDocument();
|
||||
expect(screen.getByText('Save changes')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows fallback when recipe is not found', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockRejectedValue('unexpected');
|
||||
|
||||
render(<EditRecipePage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to load recipe')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,291 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockGetRecipe, mockScaleRecipe } = vi.hoisted(() => ({
|
||||
mockGetRecipe: vi.fn(),
|
||||
mockScaleRecipe: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/recipes', () => ({
|
||||
getRecipe: mockGetRecipe,
|
||||
scaleRecipe: mockScaleRecipe,
|
||||
listRecipes: vi.fn(),
|
||||
deleteRecipe: vi.fn(),
|
||||
createRecipe: vi.fn(),
|
||||
updateRecipe: vi.fn(),
|
||||
importRecipeFromText: vi.fn(),
|
||||
importRecipeFromUrl: vi.fn(),
|
||||
listRecipesByProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useParams: () => ({ id: 'r1' }),
|
||||
useRouter: () => ({ push: vi.fn(), back: vi.fn() }),
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import RecipeDetailPage from '../../../../../src/app/(dashboard)/recipes/[id]/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const SAMPLE_RECIPE = {
|
||||
_id: 'r1',
|
||||
householdId: 'hh1',
|
||||
name: 'Spaghetti Bolognese',
|
||||
description: 'Classic Italian pasta',
|
||||
servings: 4,
|
||||
prepTime: 15,
|
||||
cookTime: 30,
|
||||
totalTime: 45,
|
||||
cuisine: 'Italian',
|
||||
tags: ['pasta'],
|
||||
isFavorite: true,
|
||||
ingredients: [
|
||||
{
|
||||
productId: 'p1',
|
||||
productName: 'Spaghetti',
|
||||
quantity: 400,
|
||||
unit: 'g',
|
||||
isOptional: false,
|
||||
},
|
||||
{
|
||||
productId: 'p2',
|
||||
productName: 'Parmesan',
|
||||
quantity: 50,
|
||||
unit: 'g',
|
||||
isOptional: true,
|
||||
preparation: 'grated',
|
||||
},
|
||||
],
|
||||
steps: [
|
||||
{ order: 1, instruction: 'Boil water', duration: 10 },
|
||||
{ order: 2, instruction: 'Cook pasta', tip: 'Al dente' },
|
||||
],
|
||||
perServingNutrition: {
|
||||
calories: 450,
|
||||
protein: 25,
|
||||
carbs: 55,
|
||||
fat: 12,
|
||||
fiber: 3,
|
||||
sugar: 5,
|
||||
sodium: 400,
|
||||
saturatedFat: 4,
|
||||
cholesterol: 50,
|
||||
},
|
||||
totalNutrition: {
|
||||
calories: 1800,
|
||||
protein: 100,
|
||||
carbs: 220,
|
||||
fat: 48,
|
||||
},
|
||||
warnings: ['high_calories'],
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
describe('RecipeDetailPage', () => {
|
||||
it('shows loading skeleton', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
expect(screen.getByText('Recipe')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error when fetch fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockRejectedValue(new Error('Not found'));
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Not found')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows recipe not found fallback', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
// Resolve with null-like to trigger error path
|
||||
mockGetRecipe.mockRejectedValue(new Error('Recipe not found'));
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Recipe not found')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders recipe details', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Spaghetti Bolognese')).toBeInTheDocument();
|
||||
expect(screen.getByText('Classic Italian pasta')).toBeInTheDocument();
|
||||
expect(screen.getByText('4 servings')).toBeInTheDocument();
|
||||
expect(screen.getByText('Starred')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows ingredients with optional indicator', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Spaghetti')).toBeInTheDocument();
|
||||
expect(screen.getByText('Parmesan')).toBeInTheDocument();
|
||||
expect(screen.getByText('(optional)')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows steps with duration and tips', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Boil water')).toBeInTheDocument();
|
||||
expect(screen.getByText('Cook pasta')).toBeInTheDocument();
|
||||
expect(screen.getByText('Tip: Al dente')).toBeInTheDocument();
|
||||
expect(screen.getByText('10 min')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows nutritional warnings', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Nutritional alerts')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows nutrition panel', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('450 kcal')).toBeInTheDocument();
|
||||
expect(screen.getByText('25.0g')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows edit link', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Edit')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('handles non-Error fetch failure', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockRejectedValue('string error');
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to load recipe')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders recipe without description', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue({
|
||||
...SAMPLE_RECIPE,
|
||||
description: undefined,
|
||||
warnings: [],
|
||||
isFavorite: false,
|
||||
});
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Spaghetti Bolognese')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Starred')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('scales the recipe servings', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
mockScaleRecipe.mockResolvedValue({
|
||||
...SAMPLE_RECIPE,
|
||||
servings: 8,
|
||||
ingredients: SAMPLE_RECIPE.ingredients.map(i => ({ ...i, quantity: i.quantity * 2 })),
|
||||
});
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
await screen.findByText('Spaghetti Bolognese');
|
||||
|
||||
const scaleInput = screen.getByRole('spinbutton');
|
||||
fireEvent.change(scaleInput, { target: { value: '8' } });
|
||||
|
||||
const scaleBtn = screen.getByRole('button', { name: /Scale/i });
|
||||
fireEvent.click(scaleBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockScaleRecipe).toHaveBeenCalledWith('hh1', 'r1', { targetServings: 8 });
|
||||
});
|
||||
|
||||
// Test resetting back to original
|
||||
const resetBtn = screen.getByRole('button', { name: /Reset to original/i });
|
||||
fireEvent.click(resetBtn);
|
||||
expect((scaleInput as HTMLInputElement).value).toBe('4');
|
||||
});
|
||||
|
||||
it('handles scale recipe failure gracefully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
mockScaleRecipe.mockRejectedValue(new Error('Scale failed'));
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
await screen.findByText('Spaghetti Bolognese');
|
||||
|
||||
const scaleInput = screen.getByRole('spinbutton');
|
||||
fireEvent.change(scaleInput, { target: { value: '6' } });
|
||||
|
||||
const scaleBtn = screen.getByRole('button', { name: /Scale/i });
|
||||
fireEvent.click(scaleBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Scale failed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('skips scaling if servings did not change', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRecipe.mockResolvedValue(SAMPLE_RECIPE);
|
||||
|
||||
render(<RecipeDetailPage />);
|
||||
await screen.findByText('Spaghetti Bolognese');
|
||||
|
||||
const scaleBtn = screen.getByRole('button', { name: /Scale/i });
|
||||
fireEvent.click(scaleBtn);
|
||||
|
||||
expect(mockScaleRecipe).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/recipes', () => ({
|
||||
createRecipe: vi.fn(),
|
||||
updateRecipe: vi.fn(),
|
||||
listRecipes: vi.fn(),
|
||||
deleteRecipe: vi.fn(),
|
||||
getRecipe: vi.fn(),
|
||||
scaleRecipe: vi.fn(),
|
||||
importRecipeFromText: vi.fn(),
|
||||
importRecipeFromUrl: vi.fn(),
|
||||
listRecipesByProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn(), back: vi.fn() }),
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import NewRecipePage from '../../../../../src/app/(dashboard)/recipes/new/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
describe('NewRecipePage', () => {
|
||||
it('shows loading state', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<NewRecipePage />);
|
||||
|
||||
expect(screen.getByText('New Recipe')).toBeInTheDocument();
|
||||
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
|
||||
render(<NewRecipePage />);
|
||||
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders editor when household exists', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
|
||||
render(<NewRecipePage />);
|
||||
|
||||
expect(screen.getByPlaceholderText('Recipe name')).toBeInTheDocument();
|
||||
expect(screen.getByText('Create recipe')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,423 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockListRecipes, mockDeleteRecipe } = vi.hoisted(() => ({
|
||||
mockListRecipes: vi.fn(),
|
||||
mockDeleteRecipe: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
vi.mock('@/services/recipes', () => ({
|
||||
listRecipes: mockListRecipes,
|
||||
deleteRecipe: mockDeleteRecipe,
|
||||
createRecipe: vi.fn(),
|
||||
updateRecipe: vi.fn(),
|
||||
getRecipe: vi.fn(),
|
||||
scaleRecipe: vi.fn(),
|
||||
importRecipeFromText: vi.fn(),
|
||||
importRecipeFromUrl: vi.fn(),
|
||||
listRecipesByProduct: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
default: (props: any) => <a href={props.href}>{props.children}</a>,
|
||||
}));
|
||||
|
||||
import RecipesPage from '../../../../src/app/(dashboard)/recipes/page';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
const SAMPLE_RECIPE = {
|
||||
_id: 'r1',
|
||||
householdId: 'hh1',
|
||||
name: 'Spaghetti Bolognese',
|
||||
description: 'Classic Italian pasta',
|
||||
servings: 4,
|
||||
prepTime: 15,
|
||||
cookTime: 30,
|
||||
totalTime: 45,
|
||||
cuisine: 'Italian',
|
||||
tags: ['pasta', 'comfort'],
|
||||
isFavorite: true,
|
||||
ingredients: [
|
||||
{
|
||||
productId: 'p1',
|
||||
productName: 'Spaghetti',
|
||||
quantity: 400,
|
||||
unit: 'g',
|
||||
isOptional: false,
|
||||
},
|
||||
],
|
||||
steps: [{ order: 1, instruction: 'Boil water' }],
|
||||
perServingNutrition: {
|
||||
calories: 450,
|
||||
protein: 25,
|
||||
carbs: 55,
|
||||
fat: 12,
|
||||
fiber: 3,
|
||||
sugar: 5,
|
||||
sodium: 400,
|
||||
},
|
||||
totalNutrition: {
|
||||
calories: 1800,
|
||||
protein: 100,
|
||||
carbs: 220,
|
||||
fat: 48,
|
||||
},
|
||||
warnings: ['high_calories'],
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
|
||||
describe('RecipesPage', () => {
|
||||
it('shows loading skeleton when session is loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
expect(screen.getByText('Recipes')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Search recipes...')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders recipe list when household exists', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Spaghetti Bolognese')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows empty state when no recipes', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/No recipes yet/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows nutrition info on recipe card', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('450 kcal')).toBeInTheDocument();
|
||||
expect(screen.getByText('4 servings')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows time on recipe card', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('45 min')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows warning labels', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('High cal')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows tags on recipe card', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('pasta')).toBeInTheDocument();
|
||||
expect(screen.getByText('comfort')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows favorite star', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
// Star character is rendered for favorites
|
||||
expect(screen.getByText('Spaghetti Bolognese').closest('a')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows edit link', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Edit')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('handles delete with confirmation', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteRecipe.mockResolvedValue(undefined);
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Delete'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockDeleteRecipe).toHaveBeenCalledWith('hh1', 'r1');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not delete when confirm is cancelled', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Delete'));
|
||||
|
||||
expect(mockDeleteRecipe).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows error when fetch fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Network error')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows filter empty state message', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByPlaceholderText('Cuisine...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Cuisine...'), {
|
||||
target: { value: 'Thai' },
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/No recipes match your filters/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows recipe without totalTime using prep+cook', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [{ ...SAMPLE_RECIPE, totalTime: undefined }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('45 min')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('formats hours correctly', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [{ ...SAMPLE_RECIPE, totalTime: 90 }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('1h 30m')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('formats exact hours', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [{ ...SAMPLE_RECIPE, totalTime: 120 }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('2h')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('handles delete error', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteRecipe.mockRejectedValue(new Error('Delete failed'));
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Delete'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete failed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('handles non-Error delete failure', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [SAMPLE_RECIPE],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeleteRecipe.mockRejectedValue('string error');
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Delete')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Delete'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to delete recipe')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows + New Recipe link', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('+ New Recipe')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('recipe with no warnings renders without warning badges', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [{ ...SAMPLE_RECIPE, warnings: [], tags: [] }],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Spaghetti Bolognese')).toBeInTheDocument();
|
||||
expect(screen.queryByText('High cal')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('toggles favorites checkbox', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRecipes.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RecipesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByLabelText('Favorites')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByLabelText('Favorites'));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockListRecipes).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ isFavorite: true }),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,689 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import type React from 'react';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));
|
||||
|
||||
const {
|
||||
mockGetRefillAlerts,
|
||||
mockListRefillLists,
|
||||
mockCreateRefillList,
|
||||
mockUpdateRefillList,
|
||||
mockUpdateRefillListItem,
|
||||
mockAddToCabinet,
|
||||
} = vi.hoisted(() => ({
|
||||
mockGetRefillAlerts: vi.fn(),
|
||||
mockListRefillLists: vi.fn(),
|
||||
mockCreateRefillList: vi.fn(),
|
||||
mockUpdateRefillList: vi.fn(),
|
||||
mockUpdateRefillListItem: vi.fn(),
|
||||
mockAddToCabinet: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('@/services/refills', () => ({
|
||||
getRefillAlerts: mockGetRefillAlerts,
|
||||
listRefillLists: mockListRefillLists,
|
||||
createRefillList: mockCreateRefillList,
|
||||
updateRefillList: mockUpdateRefillList,
|
||||
updateRefillListItem: mockUpdateRefillListItem,
|
||||
addToCabinet: mockAddToCabinet,
|
||||
}));
|
||||
vi.mock('next/link', () => ({
|
||||
default: (props: { href?: string; children: React.ReactNode }) => props.children,
|
||||
}));
|
||||
|
||||
import RefillsPage from '../../../../src/app/(dashboard)/refills/page';
|
||||
|
||||
const emptyAlerts = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
const emptyLists = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockGetRefillAlerts.mockResolvedValue(emptyAlerts);
|
||||
mockListRefillLists.mockResolvedValue(emptyLists);
|
||||
});
|
||||
|
||||
describe('RefillsPage', () => {
|
||||
it('shows loading skeleton when session loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
|
||||
render(<RefillsPage />);
|
||||
expect(screen.getByText('Refills')).toBeInTheDocument();
|
||||
expect(screen.queryByText('New Refill List')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows household prompt when no householdId', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
|
||||
render(<RefillsPage />);
|
||||
expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders Refills heading when householdId exists', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<RefillsPage />);
|
||||
expect(screen.getByText('Refills')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty state for alerts', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<RefillsPage />);
|
||||
await waitFor(() => expect(screen.getByText(/No medicines running low/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows empty state for refill lists', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<RefillsPage />);
|
||||
await waitFor(() => expect(screen.getByText(/No refill lists yet/)).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when alerts fail', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRefillAlerts.mockRejectedValue(new Error('Alert error'));
|
||||
render(<RefillsPage />);
|
||||
await waitFor(() => expect(screen.getByText('Alert error')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows New List button and toggles form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('New List'));
|
||||
await userEvent.click(screen.getByText('New List'));
|
||||
expect(screen.getByPlaceholderText('List name')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('creates a new refill list', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockCreateRefillList.mockResolvedValue({
|
||||
_id: 'rl-1',
|
||||
name: 'Test List',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('New List'));
|
||||
await userEvent.click(screen.getByText('New List'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('List name'));
|
||||
fireEvent.change(screen.getByPlaceholderText('List name'), { target: { value: 'Test List' } });
|
||||
fireEvent.submit(screen.getByPlaceholderText('List name').closest('form')!);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateRefillList).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ name: 'Test List' }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows refill list and can select it', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: false,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Aspirin')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('updates a refill list status', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRefillList.mockResolvedValue({});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText('Start shopping'));
|
||||
await userEvent.click(screen.getByText('Start shopping'));
|
||||
|
||||
expect(mockUpdateRefillList).toHaveBeenCalledWith('hh1', 'rl-1', { status: 'shopping' });
|
||||
});
|
||||
|
||||
it('shows error when create list fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockCreateRefillList.mockRejectedValue(new Error('Create failed'));
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('New List'));
|
||||
await userEvent.click(screen.getByText('New List'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('List name'));
|
||||
fireEvent.change(screen.getByPlaceholderText('List name'), { target: { value: 'Test' } });
|
||||
fireEvent.submit(screen.getByPlaceholderText('List name').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Create failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('adds checked items to cabinet', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: true,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAddToCabinet.mockResolvedValue({ addedCount: 1 });
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText(/Add .* to Cabinet/));
|
||||
await userEvent.click(screen.getByText(/Add .* to Cabinet/));
|
||||
|
||||
expect(mockAddToCabinet).toHaveBeenCalledWith('hh1', 'rl-1');
|
||||
});
|
||||
|
||||
it('generates a refill list from alerts', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRefillAlerts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'm1',
|
||||
medicineName: 'Aspirin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
currentStock: 5,
|
||||
dailyConsumption: 1.5,
|
||||
daysUntilEmpty: 3,
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateRefillList.mockResolvedValue({
|
||||
_id: 'rl-gen',
|
||||
name: 'Auto Refills',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Aspirin'));
|
||||
await userEvent.click(screen.getByText('Generate Refill List'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('List name, e.g. Weekly refills'));
|
||||
fireEvent.change(screen.getByPlaceholderText('List name, e.g. Weekly refills'), {
|
||||
target: { value: 'Auto Refills' },
|
||||
});
|
||||
fireEvent.submit(
|
||||
screen.getByPlaceholderText('List name, e.g. Weekly refills').closest('form')!,
|
||||
);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockCreateRefillList).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ name: 'Auto Refills', fromAlerts: true }),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('cancels create list form', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('New List'));
|
||||
await userEvent.click(screen.getByText('New List'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('List name'));
|
||||
await userEvent.click(
|
||||
screen.getAllByRole('button', { name: 'Cancel' })[
|
||||
screen.getAllByRole('button', { name: 'Cancel' }).length - 1
|
||||
]!,
|
||||
);
|
||||
|
||||
expect(screen.queryByPlaceholderText('List name')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('closes the refill list detail panel', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByTitle('Close'));
|
||||
await userEvent.click(screen.getByTitle('Close'));
|
||||
|
||||
// After close, detail panel should not be visible (no Close button)
|
||||
expect(screen.queryByTitle('Close')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('archives a refill list', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRefillList.mockResolvedValue({});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText('Archive'));
|
||||
await userEvent.click(screen.getByText('Archive'));
|
||||
|
||||
expect(mockUpdateRefillList).toHaveBeenCalledWith('hh1', 'rl-1', { status: 'archived' });
|
||||
});
|
||||
|
||||
it('shows error when generate list fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRefillAlerts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'm1',
|
||||
medicineName: 'Aspirin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
currentStock: 5,
|
||||
dailyConsumption: 1.5,
|
||||
daysUntilEmpty: 3,
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockCreateRefillList.mockRejectedValue(new Error('Generate failed'));
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Aspirin'));
|
||||
await userEvent.click(screen.getByText('Generate Refill List'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('List name, e.g. Weekly refills'));
|
||||
fireEvent.change(screen.getByPlaceholderText('List name, e.g. Weekly refills'), {
|
||||
target: { value: 'Auto Refills' },
|
||||
});
|
||||
fireEvent.submit(
|
||||
screen.getByPlaceholderText('List name, e.g. Weekly refills').closest('form')!,
|
||||
);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Generate failed')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('dismisses refill list panel error', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRefillLists.mockRejectedValue(new Error('List error'));
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('List error'));
|
||||
await userEvent.click(screen.getByText('Dismiss'));
|
||||
expect(screen.queryByText('List error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles item checked state', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: false,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRefillListItem.mockResolvedValue({
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: true,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText('Aspirin'));
|
||||
await userEvent.click(screen.getByRole('checkbox'));
|
||||
|
||||
expect(mockUpdateRefillListItem).toHaveBeenCalledWith('hh1', 'rl-1', 'item-1', {
|
||||
checked: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('marks a shopping list as complete', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'shopping',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRefillList.mockResolvedValue({});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText('Mark complete'));
|
||||
await userEvent.click(screen.getByText('Mark complete'));
|
||||
|
||||
expect(mockUpdateRefillList).toHaveBeenCalledWith('hh1', 'rl-1', { status: 'completed' });
|
||||
});
|
||||
|
||||
it('filters refill lists by status', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'rl-1',
|
||||
name: 'Active List',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Active List'));
|
||||
fireEvent.change(screen.getByDisplayValue('All statuses'), { target: { value: 'active' } });
|
||||
|
||||
await waitFor(() => expect(mockListRefillLists).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it('shows actual price input after checking item and allows price entry', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: true,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Actual price'));
|
||||
fireEvent.change(screen.getByPlaceholderText('Actual price'), { target: { value: '5.99' } });
|
||||
|
||||
expect(screen.getByPlaceholderText('Actual price')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('dismisses panel-level error', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: true,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockAddToCabinet.mockRejectedValue(new Error('Cabinet error'));
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText(/Add .* to Cabinet/));
|
||||
await userEvent.click(screen.getByText(/Add .* to Cabinet/));
|
||||
|
||||
await waitFor(() => screen.getByText('Cabinet error'));
|
||||
// Dismiss via inline button in the panel
|
||||
const dismissBtns = screen.getAllByText('Dismiss');
|
||||
await userEvent.click(dismissBtns[dismissBtns.length - 1]!);
|
||||
|
||||
expect(screen.queryByText('Cabinet error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on update item', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{
|
||||
_id: 'item-1',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Aspirin',
|
||||
quantity: 30,
|
||||
unit: 'tablet',
|
||||
checked: false,
|
||||
addedToCabinet: false,
|
||||
},
|
||||
],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRefillListItem.mockRejectedValue('update failed');
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText('Aspirin'));
|
||||
await userEvent.click(screen.getAllByRole('checkbox')[0]!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to update item')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on update status', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
const refillList = {
|
||||
_id: 'rl-1',
|
||||
name: 'Weekly List',
|
||||
status: 'active',
|
||||
items: [],
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
mockListRefillLists.mockResolvedValue({
|
||||
data: [refillList],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockUpdateRefillList.mockRejectedValue('status failed');
|
||||
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Weekly List'));
|
||||
await userEvent.click(screen.getByText('Weekly List'));
|
||||
|
||||
await waitFor(() => screen.getByText('Start shopping'));
|
||||
await userEvent.click(screen.getByText('Start shopping'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to update status')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows refill alert when present', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockGetRefillAlerts.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
medicineId: 'm1',
|
||||
medicineName: 'Aspirin',
|
||||
medicineStrength: 500,
|
||||
medicineStrengthUnit: 'mg',
|
||||
currentStock: 5,
|
||||
dailyConsumption: 1.5,
|
||||
daysUntilEmpty: 3,
|
||||
},
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
render(<RefillsPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Aspirin')).toBeInTheDocument());
|
||||
});
|
||||
});
|
||||
|
|
@ -1,280 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
|
||||
const { mockUseApi } = vi.hoisted(() => ({
|
||||
mockUseApi: vi.fn(),
|
||||
}));
|
||||
|
||||
const {
|
||||
mockCreateHousehold,
|
||||
mockJoinHousehold,
|
||||
mockGetHousehold,
|
||||
mockUpdateHousehold,
|
||||
mockGenerateInviteCode,
|
||||
} = vi.hoisted(() => ({
|
||||
mockCreateHousehold: vi.fn(),
|
||||
mockJoinHousehold: vi.fn(),
|
||||
mockGetHousehold: vi.fn(),
|
||||
mockUpdateHousehold: vi.fn(),
|
||||
mockGenerateInviteCode: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
vi.mock('@/services/households', () => ({
|
||||
createHousehold: mockCreateHousehold,
|
||||
joinHousehold: mockJoinHousehold,
|
||||
getHousehold: mockGetHousehold,
|
||||
updateHousehold: mockUpdateHousehold,
|
||||
generateInviteCode: mockGenerateInviteCode,
|
||||
}));
|
||||
|
||||
import SettingsPage from '../../../../src/app/(dashboard)/settings/page';
|
||||
|
||||
const household = {
|
||||
name: 'My House',
|
||||
inviteCode: 'ABC123',
|
||||
members: [{ userId: 'u-1', role: 'admin' }],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('SettingsPage', () => {
|
||||
it('shows loading skeleton when loading', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: true, refreshProfile: vi.fn() });
|
||||
render(<SettingsPage />);
|
||||
expect(screen.getByText('Settings')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Household')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows create/join forms when no household', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile: vi.fn() });
|
||||
render(<SettingsPage />);
|
||||
expect(screen.getByPlaceholderText('Household name')).toBeInTheDocument();
|
||||
expect(screen.getByPlaceholderText('Invite code')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows account section always', () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile: vi.fn() });
|
||||
render(<SettingsPage />);
|
||||
expect(screen.getByText('Account')).toBeInTheDocument();
|
||||
expect(screen.getByRole('link', { name: /Manage Keycloak Account/ })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('creates a household on form submit', async () => {
|
||||
const refreshProfile = vi.fn();
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile });
|
||||
mockCreateHousehold.mockResolvedValue({});
|
||||
render(<SettingsPage />);
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('Household name'), 'New Home');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Create' }));
|
||||
|
||||
await waitFor(() => expect(mockCreateHousehold).toHaveBeenCalledWith('New Home'));
|
||||
expect(refreshProfile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows error when create fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile: vi.fn() });
|
||||
mockCreateHousehold.mockRejectedValue(new Error('Already exists'));
|
||||
render(<SettingsPage />);
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('Household name'), 'New Home');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Create' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Already exists')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('joins a household on form submit', async () => {
|
||||
const refreshProfile = vi.fn();
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile });
|
||||
mockJoinHousehold.mockResolvedValue({});
|
||||
render(<SettingsPage />);
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('Invite code'), 'XYZ999');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Join' }));
|
||||
|
||||
await waitFor(() => expect(mockJoinHousehold).toHaveBeenCalledWith('XYZ999'));
|
||||
expect(refreshProfile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows error when join fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile: vi.fn() });
|
||||
mockJoinHousehold.mockRejectedValue(new Error('Invalid code'));
|
||||
render(<SettingsPage />);
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('Invite code'), 'BAD');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Join' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Invalid code')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('loads and displays household details when householdId exists', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('My House')).toBeInTheDocument());
|
||||
expect(screen.getByText('ABC123')).toBeInTheDocument();
|
||||
expect(screen.getByText('1')).toBeInTheDocument(); // member count
|
||||
});
|
||||
|
||||
it('shows loading household details text initially', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockReturnValue(new Promise(() => {})); // never resolves
|
||||
render(<SettingsPage />);
|
||||
expect(screen.getByText('Loading household details...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('allows editing the household name', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
mockUpdateHousehold.mockResolvedValue({ ...household, name: 'Updated Home' });
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
||||
|
||||
const input = screen.getByDisplayValue('My House');
|
||||
await userEvent.clear(input);
|
||||
await userEvent.type(input, 'Updated Home');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(mockUpdateHousehold).toHaveBeenCalledWith('hh1', { name: 'Updated Home' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows error when name update fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
mockUpdateHousehold.mockRejectedValue(new Error('Name taken'));
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
||||
const input = screen.getByDisplayValue('My House');
|
||||
await userEvent.clear(input);
|
||||
await userEvent.type(input, 'Other Home');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Name taken')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('cancels editing name', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
||||
|
||||
expect(screen.getByText('My House')).toBeInTheDocument();
|
||||
expect(screen.queryByDisplayValue('My House')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('regenerates invite code', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
mockGenerateInviteCode.mockResolvedValue({ ...household, inviteCode: 'NEW999' });
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('ABC123'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Regenerate' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('NEW999')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows error when regenerate fails', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
mockGenerateInviteCode.mockRejectedValue(new Error('Server error'));
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('ABC123'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Regenerate' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Server error')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('saves unchanged name without calling API', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
expect(mockUpdateHousehold).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on create household', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile: vi.fn() });
|
||||
mockCreateHousehold.mockRejectedValue('create failed');
|
||||
render(<SettingsPage />);
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('Household name'), 'My Home');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Create' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to create household')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on join household', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: null, isLoading: false, refreshProfile: vi.fn() });
|
||||
mockJoinHousehold.mockRejectedValue('join failed');
|
||||
render(<SettingsPage />);
|
||||
|
||||
await userEvent.type(screen.getByPlaceholderText('Invite code'), 'XYZ123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Join' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to join household')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on name update', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
mockUpdateHousehold.mockRejectedValue('update failed');
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
||||
const input = screen.getByDisplayValue('My House');
|
||||
await userEvent.clear(input);
|
||||
await userEvent.type(input, 'New Name');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to update name')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('shows fallback error when non-Error thrown on regenerate', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
mockGenerateInviteCode.mockRejectedValue('regen failed');
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Regenerate' }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText('Failed to regenerate invite code')).toBeInTheDocument(),
|
||||
);
|
||||
});
|
||||
|
||||
it('shows validation error when saving empty name', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false, refreshProfile: vi.fn() });
|
||||
mockGetHousehold.mockResolvedValue(household);
|
||||
render(<SettingsPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('My House'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Edit' }));
|
||||
const input = screen.getByDisplayValue('My House');
|
||||
await userEvent.clear(input);
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
|
||||
|
||||
expect(screen.getByText('Name cannot be empty')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -48,7 +48,8 @@ describe('ShoppingListDetailPage', () => {
|
|||
name: 'Test List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{ id: 'item1', productId: 'p1', quantity: 5, checked: false, unit: 'pcs' },
|
||||
{ id: 'item1', productId: 'p1', quantity: 5, checked: false, unit: 'pcs', estimatedPrice: 3.50, addedToPantry: false },
|
||||
{ id: 'item2', customName: 'Generic Salt', quantity: 1, checked: true, unit: 'g', estimatedPrice: 1.20, addedToPantry: true },
|
||||
],
|
||||
createdAt: '2026-05-10',
|
||||
} as any);
|
||||
|
|
@ -264,4 +265,236 @@ describe('ShoppingListDetailPage', () => {
|
|||
expect(ShoppingListsService.syncToPantry).not.toHaveBeenCalled();
|
||||
confirmSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles persistent toggle sync failure gracefully', async () => {
|
||||
vi.mocked(ShoppingListsService.updateShoppingItem).mockRejectedValue(new Error('Toggle persist failed'));
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const checkbox = screen.getByRole('button', { name: /Toggle check for Apple/i });
|
||||
fireEvent.click(checkbox);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(consoleSpy).toHaveBeenCalled();
|
||||
});
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles add item failure gracefully', async () => {
|
||||
vi.mocked(ShoppingListsService.addShoppingItem).mockRejectedValue(new Error('Add item failed'));
|
||||
const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const customInput = screen.getByPlaceholderText(/e.g., Generic Flour/i);
|
||||
fireEvent.change(customInput, { target: { value: 'Banana' } });
|
||||
|
||||
const submitBtn = screen.getByRole('button', { name: /Add to List/i });
|
||||
fireEvent.click(submitBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(alertSpy).toHaveBeenCalledWith('Add item failed');
|
||||
});
|
||||
alertSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles sync to pantry failure gracefully', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingList).mockResolvedValue({
|
||||
id: 'list1',
|
||||
name: 'Test List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{ id: 'item1', productId: 'p1', quantity: 5, checked: true, addedToPantry: false },
|
||||
],
|
||||
createdAt: '2026-05-10',
|
||||
} as any);
|
||||
|
||||
vi.mocked(ShoppingListsService.syncToPantry).mockRejectedValue(new Error('Sync migration failed'));
|
||||
const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const syncBtn = screen.getByRole('button', { name: /Sync.*items to Pantry/i });
|
||||
fireEvent.click(syncBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(alertSpy).toHaveBeenCalledWith('Migration sync error: Sync migration failed');
|
||||
});
|
||||
alertSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles remote sync callback correctly', async () => {
|
||||
let capturedCallback: any;
|
||||
vi.mocked(useShoppingListSyncModule.useShoppingListSync).mockImplementation((hhId, lId, onSync) => {
|
||||
capturedCallback = onSync;
|
||||
return {
|
||||
isConnected: true,
|
||||
error: null,
|
||||
toggleItemCheck: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
expect(capturedCallback).toBeDefined();
|
||||
capturedCallback({ some: 'delta' });
|
||||
|
||||
expect(consoleSpy).toHaveBeenCalledWith('🔔 Remote state delta payload:', { some: 'delta' });
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles store comparison failure gracefully', async () => {
|
||||
vi.mocked(ShoppingListsService.getBasketStoreComparison).mockRejectedValue(new Error('Comparison failed'));
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const checkBtn = screen.getByRole('button', { name: /Check Lowest Store Options/i });
|
||||
fireEvent.click(checkBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(consoleSpy).toHaveBeenCalledWith('Comparison load fail', expect.any(Error));
|
||||
});
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles shopping list query error with fallback error message', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingList).mockRejectedValue({});
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/Shopping list not found/i)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders correctly when singleStoreOptions is missing in comparison', async () => {
|
||||
vi.mocked(ShoppingListsService.getBasketStoreComparison).mockResolvedValue({
|
||||
cheapestSingleStore: null,
|
||||
singleStoreOptions: undefined
|
||||
} as any);
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const checkBtn = screen.getByRole('button', { name: /Check Lowest Store Options/i });
|
||||
fireEvent.click(checkBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/Lowest Store Basket Rank/i)).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders and styles multiple store comparison options', async () => {
|
||||
vi.mocked(ShoppingListsService.getBasketStoreComparison).mockResolvedValue({
|
||||
cheapestSingleStore: { storeId: 'store1', storeName: 'Costco', estimatedTotal: 25.00 },
|
||||
singleStoreOptions: [
|
||||
{ storeId: 'store1', storeName: 'Costco', estimatedTotal: 25.00, itemsCovered: 1 },
|
||||
{ storeId: 'store2', storeName: 'Trader Joes', estimatedTotal: 35.00, itemsCovered: 1 }
|
||||
]
|
||||
} as any);
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const checkBtn = screen.getByRole('button', { name: /Check Lowest Store Options/i });
|
||||
fireEvent.click(checkBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Trader Joes')).toBeInTheDocument();
|
||||
expect(screen.getByText('$35.00')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('handles custom items input and blanking out custom name on product select', async () => {
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const select = screen.getAllByRole('combobox').find(el =>
|
||||
el.innerHTML.includes('-- Create Manual Custom Input --')
|
||||
) as HTMLSelectElement;
|
||||
expect(select).toBeDefined();
|
||||
|
||||
// Select product
|
||||
fireEvent.change(select, { target: { value: 'p1' } });
|
||||
expect(screen.queryByPlaceholderText(/e.g., Generic Flour/i)).not.toBeInTheDocument();
|
||||
|
||||
// Select manual custom input option
|
||||
fireEvent.change(select, { target: { value: '' } });
|
||||
const customInput = await screen.findByPlaceholderText(/e.g., Generic Flour/i);
|
||||
expect(customInput).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('falls back to zero when quantity input parsing fails', async () => {
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
const qtyInput = screen.getByDisplayValue('1');
|
||||
fireEvent.change(qtyInput, { target: { value: '' } });
|
||||
expect(qtyInput).toHaveValue(0);
|
||||
});
|
||||
|
||||
it('renders sync indicator in connecting state when offline', async () => {
|
||||
vi.mocked(useShoppingListSyncModule.useShoppingListSync).mockReturnValue({
|
||||
isConnected: false,
|
||||
error: null,
|
||||
toggleItemCheck: vi.fn(),
|
||||
});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getAllByText('Apple')[0]).toBeInTheDocument());
|
||||
|
||||
expect(screen.getByText('Connecting Sync...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not mark shopping list as completed if there are unchecked items left after sync', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingList).mockResolvedValue({
|
||||
id: 'list1',
|
||||
name: 'Test List',
|
||||
status: 'active',
|
||||
items: [
|
||||
{ id: 'item1', productId: 'p1', quantity: 5, checked: true, addedToPantry: false },
|
||||
{ id: 'item2', customName: 'Unchecked Item', quantity: 1, checked: false, addedToPantry: false }
|
||||
],
|
||||
createdAt: '2026-05-10',
|
||||
} as any);
|
||||
|
||||
vi.mocked(ShoppingListsService.syncToPantry).mockResolvedValue({ addedCount: 1 } as any);
|
||||
const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getByText('Unchecked Item')).toBeInTheDocument());
|
||||
|
||||
const syncBtn = screen.getByRole('button', { name: /Sync 1 items to Pantry/i });
|
||||
fireEvent.click(syncBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(alertSpy).toHaveBeenCalledWith('Success! Provisioned 1 items into Pantry stock.');
|
||||
});
|
||||
|
||||
expect(ShoppingListsService.updateShoppingList).not.toHaveBeenCalled();
|
||||
alertSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('renders completed list status badge when status is completed', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingList).mockResolvedValue({
|
||||
id: 'list1',
|
||||
name: 'Completed List',
|
||||
status: 'completed',
|
||||
items: [],
|
||||
createdAt: '2026-05-10',
|
||||
} as any);
|
||||
|
||||
render(<ShoppingListDetailPage />, { wrapper });
|
||||
await waitFor(() => expect(screen.getByText('Completed List')).toBeInTheDocument());
|
||||
|
||||
expect(screen.getByText('completed')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -19,7 +19,9 @@ describe('ShoppingListsPage', () => {
|
|||
token: '123',
|
||||
});
|
||||
vi.mocked(ShoppingListsService.getShoppingLists).mockResolvedValue([
|
||||
{ id: 'list2', name: 'Completed Costco', status: 'completed', items: [], totalEstimatedCost: 50, createdAt: '2026-05-09' } as any,
|
||||
{ id: 'list1', name: 'Groceries', status: 'active', items: [], totalEstimatedCost: 10, createdAt: '2026-05-10' } as any,
|
||||
{ id: 'list3', name: 'Completed Aldi', status: 'completed', items: [], totalEstimatedCost: 20, createdAt: '2026-05-08' } as any,
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -122,7 +124,17 @@ describe('ShoppingListsPage', () => {
|
|||
fireEvent.click(screen.getByRole('button', { name: /Cancel/i }));
|
||||
expect(screen.queryByText('Create Shopping List')).not.toBeInTheDocument();
|
||||
|
||||
// 3. Open Gap Modal and close via Close button
|
||||
// 3. Open Gap Modal and close via overlay click
|
||||
fireEvent.click(screen.getByRole('button', { name: /Generate from Meal Plan/i }));
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Scan Meal Plan Gaps')).toBeInTheDocument();
|
||||
});
|
||||
const gapHeading = screen.getByText('Scan Meal Plan Gaps');
|
||||
const gapOverlay = gapHeading.parentElement?.parentElement;
|
||||
if (gapOverlay) fireEvent.click(gapOverlay);
|
||||
expect(screen.queryByText('Scan Meal Plan Gaps')).not.toBeInTheDocument();
|
||||
|
||||
// 4. Re-open Gap Modal and close via Close button
|
||||
fireEvent.click(screen.getByRole('button', { name: /Generate from Meal Plan/i }));
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Scan Meal Plan Gaps')).toBeInTheDocument();
|
||||
|
|
@ -174,11 +186,13 @@ describe('ShoppingListsPage', () => {
|
|||
vi.mocked(ShoppingListsService.getShoppingLists).mockResolvedValue([
|
||||
{ id: 'list1', name: 'List 1', status: 'completed', createdAt: '2026-05-10', items: [], totalEstimatedCost: 10 } as any,
|
||||
{ id: 'list2', name: 'List 2', status: 'active', createdAt: '2026-05-12', items: [], totalEstimatedCost: 20 } as any,
|
||||
{ id: 'list3', name: 'List 3', status: 'active', createdAt: '2026-05-08', items: [], totalEstimatedCost: 30 } as any,
|
||||
]);
|
||||
|
||||
render(<ShoppingListsPage />);
|
||||
await screen.findByText('List 2');
|
||||
expect(screen.getByText('List 1')).toBeInTheDocument();
|
||||
expect(screen.getByText('List 3')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles create list failure', async () => {
|
||||
|
|
@ -217,4 +231,116 @@ describe('ShoppingListsPage', () => {
|
|||
});
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
it('renders list with shopping status and 100 percent progress', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingLists).mockResolvedValue([
|
||||
{
|
||||
_id: 'list_shop',
|
||||
name: 'Shopping List',
|
||||
status: 'shopping',
|
||||
items: [
|
||||
{ id: 'item1', checked: true },
|
||||
],
|
||||
totalEstimatedCost: 15,
|
||||
createdAt: '2026-05-12',
|
||||
} as any,
|
||||
]);
|
||||
|
||||
render(<ShoppingListsPage />);
|
||||
await screen.findByText('Shopping List');
|
||||
expect(screen.getByText('Live')).toBeInTheDocument();
|
||||
expect(screen.getByText('100%')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles list fetch failure with generic error fallback', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingLists).mockRejectedValue({});
|
||||
render(<ShoppingListsPage />);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Failed to load shopping lists')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('cancels create list if name is empty', async () => {
|
||||
render(<ShoppingListsPage />);
|
||||
await waitFor(() => expect(screen.getByRole('button', { name: /New Shopping List/i })).toBeInTheDocument());
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /New Shopping List/i }));
|
||||
const input = await screen.findByPlaceholderText(/e.g., Weekly Costco Run/i);
|
||||
fireEvent.change(input, { target: { value: ' ' } });
|
||||
|
||||
const submitBtn = screen.getByRole('button', { name: /^Create$/ });
|
||||
fireEvent.click(submitBtn);
|
||||
|
||||
expect(ShoppingListsService.createShoppingList).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles create list failure with generic error fallback', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingLists).mockResolvedValue([]);
|
||||
vi.mocked(ShoppingListsService.createShoppingList).mockRejectedValue({});
|
||||
const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListsPage />);
|
||||
|
||||
const openBtn = await screen.findByRole('button', { name: /Create First List/i });
|
||||
fireEvent.click(openBtn);
|
||||
|
||||
const input = screen.getByPlaceholderText(/e.g., Weekly Costco Run/i);
|
||||
fireEvent.change(input, { target: { value: 'New List' } });
|
||||
|
||||
const submitBtn = screen.getByRole('button', { name: /^Create$/ });
|
||||
fireEvent.click(submitBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(alertSpy).toHaveBeenCalledWith('Failed to create list');
|
||||
});
|
||||
alertSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles gap scan load failure with generic error fallback', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingLists).mockResolvedValue([]);
|
||||
vi.mocked(MealPlansService.listMealPlans).mockRejectedValue('Generic error');
|
||||
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListsPage />);
|
||||
const btn = await screen.findByRole('button', { name: /Generate from Meal Plan/i });
|
||||
fireEvent.click(btn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(spy).toHaveBeenCalledWith('Generic error');
|
||||
});
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
it('handles gap scanner generation failure with generic error fallback', async () => {
|
||||
vi.mocked(MealPlansService.listMealPlans).mockResolvedValue({
|
||||
data: [{ _id: 'mp1', status: 'active', weekStartDate: '2026-05-10', days: [] } as any],
|
||||
total: 1, page: 1, limit: 10
|
||||
});
|
||||
vi.mocked(ShoppingListsService.generateFromMealPlan).mockRejectedValue({});
|
||||
const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<ShoppingListsPage />);
|
||||
fireEvent.click(screen.getByRole('button', { name: /Generate from Meal Plan/i }));
|
||||
|
||||
const generateBtn = await screen.findByText(/Week of/i);
|
||||
fireEvent.click(generateBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(alertSpy).toHaveBeenCalledWith('Failed to generate groceries from meal plan');
|
||||
});
|
||||
alertSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('opens gap modal and handles missing plans data gracefully', async () => {
|
||||
vi.mocked(ShoppingListsService.getShoppingLists).mockResolvedValue([]);
|
||||
vi.mocked(MealPlansService.listMealPlans).mockResolvedValue({} as any);
|
||||
|
||||
render(<ShoppingListsPage />);
|
||||
const btn = await screen.findByRole('button', { name: /Generate from Meal Plan/i });
|
||||
fireEvent.click(btn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Scan Meal Plan Gaps')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -89,6 +89,32 @@ describe('ShoppingListPricesPage', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('handles fetch errors gracefully with default message fallback', async () => {
|
||||
vi.mocked(PricesService.getPriceAnalytics).mockRejectedValue({});
|
||||
render(<ShoppingListPricesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/Failed to load analytics/i)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders empty analytics fallbacks gracefully', async () => {
|
||||
vi.mocked(PricesService.getPriceAnalytics).mockResolvedValue({
|
||||
priceAlerts: [],
|
||||
spendingOverTime: [],
|
||||
spendingByCategory: [],
|
||||
averageBasketByStore: [],
|
||||
} as any);
|
||||
|
||||
render(<ShoppingListPricesPage />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('No historical spend records found.')).toBeInTheDocument();
|
||||
expect(screen.getByText('No categorized allocations recorded yet.')).toBeInTheDocument();
|
||||
expect(screen.getByText('Create multiple shopping trips to visualize basket trends.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('navigates back to checklists when button is clicked', async () => {
|
||||
render(<ShoppingListPricesPage />);
|
||||
await screen.findByText('Costco');
|
||||
|
|
|
|||
|
|
@ -399,6 +399,9 @@ describe('StoresPage', () => {
|
|||
fireEvent.change(screen.getByPlaceholderText('123 Main St'), {
|
||||
target: { value: '456 Oak Ave' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('https://walgreens.com'), {
|
||||
target: { value: 'https://newurl.com' },
|
||||
});
|
||||
fireEvent.change(screen.getByPlaceholderText('Any notes'), {
|
||||
target: { value: 'Good prices' },
|
||||
});
|
||||
|
|
@ -449,6 +452,73 @@ describe('StoresPage', () => {
|
|||
await waitFor(() => expect(screen.getByText('keytag')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('ignores custom tag via non-Enter keydown', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({ data: [], pagination: { cursor: null, hasMore: false } });
|
||||
|
||||
render(<StoresPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Store'));
|
||||
await userEvent.click(screen.getByText('Add Store'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Custom tag...'));
|
||||
fireEvent.change(screen.getByPlaceholderText('Custom tag...'), { target: { value: 'escapetag' } });
|
||||
fireEvent.keyDown(screen.getByPlaceholderText('Custom tag...'), { key: 'Escape' });
|
||||
|
||||
expect(screen.queryByText('escapetag')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders store card with missing optional parameters correctly', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{
|
||||
_id: 'st-addr',
|
||||
name: 'Address Only Store',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u-1',
|
||||
address: '123 Unique Way',
|
||||
url: undefined,
|
||||
notes: undefined
|
||||
},
|
||||
{
|
||||
_id: 'st-url',
|
||||
name: 'URL Only Store',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u-1',
|
||||
address: undefined,
|
||||
url: 'https://onlyurl.com',
|
||||
notes: undefined
|
||||
},
|
||||
{
|
||||
_id: 'st-notes',
|
||||
name: 'Notes Only Store',
|
||||
tags: [],
|
||||
isActive: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
householdId: 'hh1',
|
||||
createdBy: 'u-1',
|
||||
address: undefined,
|
||||
url: undefined,
|
||||
notes: 'Special instruction notes'
|
||||
}
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false }
|
||||
});
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => expect(screen.getByText('Address Only Store')).toBeInTheDocument());
|
||||
expect(screen.getByText('123 Unique Way')).toBeInTheDocument();
|
||||
expect(screen.getByText('https://onlyurl.com')).toBeInTheDocument();
|
||||
expect(screen.getByText('Special instruction notes')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('changes search filter', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({
|
||||
|
|
@ -548,4 +618,132 @@ describe('StoresPage', () => {
|
|||
|
||||
expect(screen.getByText('Old Store')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles create store generic error fallback', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({ data: [], pagination: { cursor: null, hasMore: false } });
|
||||
mockCreateStore.mockRejectedValue('Generic store save error');
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => screen.getByText('Add Store'));
|
||||
await userEvent.click(screen.getByText('Add Store'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Walgreens'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Walgreens'), { target: { value: 'Walmart' } });
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. Walgreens').closest('form')!);
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to save store')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('handles list stores generic error fallback', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockRejectedValue('Generic list error');
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => expect(screen.getByText('Failed to load stores')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('handles deactivate store generic error fallback', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'st-1', name: 'CVS', tags: [], isActive: true, createdAt: '2026-01-01T00:00:00.000Z', householdId: 'hh1', createdBy: 'u-1' }
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
mockDeactivateStore.mockRejectedValue('Generic deactivate error');
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => screen.getByTitle('Deactivate'));
|
||||
await userEvent.click(screen.getByTitle('Deactivate'));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Failed to deactivate store')).toBeInTheDocument());
|
||||
});
|
||||
|
||||
it('skips deactivation if confirmation is rejected', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({
|
||||
data: [
|
||||
{ _id: 'st-1', name: 'CVS', tags: [], isActive: true, createdAt: '2026-01-01T00:00:00.000Z', householdId: 'hh1', createdBy: 'u-1' }
|
||||
],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => screen.getByTitle('Deactivate'));
|
||||
await userEvent.click(screen.getByTitle('Deactivate'));
|
||||
|
||||
expect(mockDeactivateStore).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles empty optional fields creation gracefully', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({ data: [], pagination: { cursor: null, hasMore: false } });
|
||||
mockCreateStore.mockResolvedValue({} as any);
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => screen.getByText('Add Store'));
|
||||
await userEvent.click(screen.getByText('Add Store'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('e.g. Walgreens'));
|
||||
fireEvent.change(screen.getByPlaceholderText('e.g. Walgreens'), { target: { value: 'Walmart' } });
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('123 Main St'), { target: { value: ' ' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('https://walgreens.com'), { target: { value: ' ' } });
|
||||
fireEvent.change(screen.getByPlaceholderText('Any notes'), { target: { value: ' ' } });
|
||||
|
||||
fireEvent.submit(screen.getByPlaceholderText('e.g. Walgreens').closest('form')!);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockCreateStore).toHaveBeenCalledWith('hh1', {
|
||||
name: 'Walmart',
|
||||
address: undefined,
|
||||
url: undefined,
|
||||
notes: undefined,
|
||||
tags: [],
|
||||
isActive: true
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('renders empty search message when filters are applied with no match', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({ data: [], pagination: { cursor: null, hasMore: false } });
|
||||
|
||||
render(<StoresPage />);
|
||||
await waitFor(() => expect(screen.getByText(/No stores yet/)).toBeInTheDocument());
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Search stores...'), { target: { value: 'MissingName' } });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('No stores match your filters.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('prevents adding duplicate custom tags', async () => {
|
||||
mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
|
||||
mockListStores.mockResolvedValue({ data: [], pagination: { cursor: null, hasMore: false } });
|
||||
|
||||
render(<StoresPage />);
|
||||
|
||||
await waitFor(() => screen.getByText('Add Store'));
|
||||
await userEvent.click(screen.getByText('Add Store'));
|
||||
|
||||
await waitFor(() => screen.getByPlaceholderText('Custom tag...'));
|
||||
const input = screen.getByPlaceholderText('Custom tag...');
|
||||
|
||||
// Add tag once
|
||||
fireEvent.change(input, { target: { value: 'duplicated' } });
|
||||
fireEvent.keyDown(input, { key: 'Enter' });
|
||||
await waitFor(() => expect(screen.getByText('duplicated')).toBeInTheDocument());
|
||||
|
||||
// Attempt to add it again
|
||||
fireEvent.change(input, { target: { value: 'duplicated' } });
|
||||
fireEvent.keyDown(input, { key: 'Enter' });
|
||||
|
||||
// Should only exist once in the document
|
||||
expect(screen.getAllByText('duplicated')).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@ vi.mock('next/link', () => ({
|
|||
|
||||
vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
|
||||
|
||||
import { Sidebar } from '../../src/components/layout/Sidebar';
|
||||
import { Sidebar, NAV } from '../../src/components/layout/Sidebar';
|
||||
|
||||
describe('Sidebar', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockUsePathname.mockReturnValue('/dashboard');
|
||||
mockUsePathname.mockReturnValue('/shopping-lists');
|
||||
mockUseApi.mockReturnValue({ profile: { displayName: 'Alice' } });
|
||||
});
|
||||
|
||||
|
|
@ -34,33 +34,17 @@ describe('Sidebar', () => {
|
|||
expect(screen.getByText('MeshiTrack')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders navigation sections', () => {
|
||||
it('renders active nav items', () => {
|
||||
render(<Sidebar />);
|
||||
expect(screen.getByText('Dashboard')).toBeInTheDocument();
|
||||
expect(screen.getByText('Medicines')).toBeInTheDocument();
|
||||
expect(screen.getByText('Food')).toBeInTheDocument();
|
||||
expect(screen.getByText('Settings')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders medicines nav items', () => {
|
||||
render(<Sidebar />);
|
||||
expect(screen.getByText('Cabinet')).toBeInTheDocument();
|
||||
expect(screen.getByText('Schedule & Log')).toBeInTheDocument();
|
||||
expect(screen.getByText('Regimens')).toBeInTheDocument();
|
||||
expect(screen.getByText('Library')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders food nav items', () => {
|
||||
render(<Sidebar />);
|
||||
expect(screen.getByText('Recipes')).toBeInTheDocument();
|
||||
expect(screen.getByText('Pantry')).toBeInTheDocument();
|
||||
expect(screen.getByText('Shopping Lists')).toBeInTheDocument();
|
||||
expect(screen.getByText('Stores')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('highlights active route', () => {
|
||||
mockUsePathname.mockReturnValue('/medicines/cabinet');
|
||||
mockUsePathname.mockReturnValue('/shopping-lists');
|
||||
render(<Sidebar />);
|
||||
const cabinetLink = screen.getByText('Cabinet').closest('a');
|
||||
expect(cabinetLink).toHaveAttribute('href', '/medicines/cabinet');
|
||||
const link = screen.getByText('Shopping Lists').closest('a');
|
||||
expect(link).toHaveAttribute('href', '/shopping-lists');
|
||||
});
|
||||
|
||||
it('shows user avatar with display name', () => {
|
||||
|
|
@ -75,10 +59,17 @@ describe('Sidebar', () => {
|
|||
});
|
||||
|
||||
it('highlights nested route', () => {
|
||||
mockUsePathname.mockReturnValue('/medicines/cabinet/some-id');
|
||||
mockUsePathname.mockReturnValue('/shopping-lists/some-id');
|
||||
render(<Sidebar />);
|
||||
// Cabinet link should still match via startsWith
|
||||
const cabinetLink = screen.getByText('Cabinet').closest('a');
|
||||
expect(cabinetLink).toBeInTheDocument();
|
||||
const link = screen.getByText('Shopping Lists').closest('a');
|
||||
expect(link).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders item badge when present', () => {
|
||||
NAV.push({ id: 'test-badge', label: 'Test Badge', href: '/test-badge', icon: 'bell', badge: 5, section: 'Test Section' });
|
||||
render(<Sidebar />);
|
||||
expect(screen.getByText('5')).toBeInTheDocument();
|
||||
expect(screen.getByText('Test Section')).toBeInTheDocument();
|
||||
NAV.pop(); // Clean up NAV list mutation
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
const themeMock = vi.hoisted(() => ({
|
||||
theme: 'light',
|
||||
toggleTheme: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/components/ThemeProvider', () => ({
|
||||
useTheme: () => ({ theme: 'light', toggleTheme: vi.fn() }),
|
||||
useTheme: () => themeMock,
|
||||
}));
|
||||
|
||||
vi.mock('@/components/layout/PageHeaderContext', () => ({
|
||||
|
|
@ -39,4 +44,10 @@ describe('TopBar', () => {
|
|||
render(<TopBar />);
|
||||
expect(screen.getByLabelText('Toggle theme')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders sun icon when theme is dark', () => {
|
||||
themeMock.theme = 'dark';
|
||||
render(<TopBar />);
|
||||
expect(screen.getByLabelText('Toggle theme')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { Ring } from '../../src/components/ui/Ring';
|
|||
import { SparkBars } from '../../src/components/ui/SparkBars';
|
||||
import { SupplyBar } from '../../src/components/ui/SupplyBar';
|
||||
import { Card, CardHeader, CardBody } from '../../src/components/ui/Card';
|
||||
import { Icon } from '../../src/components/ui/Icon';
|
||||
|
||||
describe('Avatar', () => {
|
||||
it('renders initial from name', () => {
|
||||
|
|
@ -19,6 +20,11 @@ describe('Avatar', () => {
|
|||
const el = screen.getByLabelText('Bob');
|
||||
expect(el).toHaveStyle({ width: '48px', height: '48px' });
|
||||
});
|
||||
|
||||
it('falls back to ? for empty name', () => {
|
||||
render(<Avatar name="" />);
|
||||
expect(screen.getByText('?')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('IconButton', () => {
|
||||
|
|
@ -116,3 +122,17 @@ describe('Card', () => {
|
|||
expect(screen.getByText('Body Content')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Icon', () => {
|
||||
it('renders paths for valid icon', () => {
|
||||
const { container } = render(<Icon name="bell" />);
|
||||
expect(container.querySelector('svg')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders null for invalid icon name', () => {
|
||||
const { container } = render(<Icon name="nonexistent" as any />);
|
||||
const svg = container.querySelector('svg');
|
||||
expect(svg).toBeInTheDocument();
|
||||
expect(svg?.childNodes.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
82
packages/web/tests/lib/auth.test.ts
Normal file
82
packages/web/tests/lib/auth.test.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import { vi, describe, it, expect } from 'vitest';
|
||||
|
||||
// Define environment variables required statically by auth.ts
|
||||
process.env.KEYCLOAK_URL = 'http://keycloak-internal';
|
||||
process.env.NEXT_PUBLIC_KEYCLOAK_URL = 'https://keycloak-public';
|
||||
process.env.KEYCLOAK_REALM = 'meshitrack';
|
||||
process.env.KEYCLOAK_CLIENT_ID = 'web-client';
|
||||
process.env.KEYCLOAK_CLIENT_SECRET = 'secret';
|
||||
|
||||
vi.mock('next-auth', () => {
|
||||
return {
|
||||
default: vi.fn((config) => {
|
||||
(globalThis as any).__capturedConfig = config;
|
||||
return {
|
||||
handlers: { GET: vi.fn(), POST: vi.fn() },
|
||||
signIn: vi.fn(),
|
||||
signOut: vi.fn(),
|
||||
auth: vi.fn(),
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('next-auth/providers/keycloak', () => {
|
||||
return {
|
||||
default: vi.fn((config) => config),
|
||||
};
|
||||
});
|
||||
|
||||
// Import auth.ts to trigger the NextAuth config capture
|
||||
import { auth } from '../../src/lib/auth';
|
||||
|
||||
describe('auth library configuration', () => {
|
||||
it('initializes NextAuth with correct pages and structures', () => {
|
||||
expect(auth).toBeDefined();
|
||||
const config = (globalThis as any).__capturedConfig;
|
||||
expect(config).toBeDefined();
|
||||
expect(config.pages.signIn).toBe('/login');
|
||||
expect(config.callbacks).toBeDefined();
|
||||
});
|
||||
|
||||
it('jwt callback appends access tokens when account metadata is present', async () => {
|
||||
const config = (globalThis as any).__capturedConfig;
|
||||
const jwtCallback = config.callbacks.jwt;
|
||||
const token = { name: 'Admin' };
|
||||
const account = {
|
||||
access_token: 'jwt-access-token-xyz',
|
||||
refresh_token: 'jwt-refresh-token-abc',
|
||||
expires_at: 1999999999,
|
||||
};
|
||||
|
||||
const result = await jwtCallback({ token, account });
|
||||
expect(result).toEqual({
|
||||
name: 'Admin',
|
||||
accessToken: 'jwt-access-token-xyz',
|
||||
refreshToken: 'jwt-refresh-token-abc',
|
||||
expiresAt: 1999999999,
|
||||
});
|
||||
});
|
||||
|
||||
it('jwt callback returns standard token without change if account metadata is missing', async () => {
|
||||
const config = (globalThis as any).__capturedConfig;
|
||||
const jwtCallback = config.callbacks.jwt;
|
||||
const token = { name: 'Admin', accessToken: 'pre-existing-token' };
|
||||
|
||||
const result = await jwtCallback({ token, account: undefined });
|
||||
expect(result).toEqual({
|
||||
name: 'Admin',
|
||||
accessToken: 'pre-existing-token',
|
||||
});
|
||||
});
|
||||
|
||||
it('session callback populates access token from jwt metadata', async () => {
|
||||
const config = (globalThis as any).__capturedConfig;
|
||||
const sessionCallback = config.callbacks.session;
|
||||
const session = { user: { name: 'Admin' } } as any;
|
||||
const token = { accessToken: 'extracted-token-from-jwt' };
|
||||
|
||||
const result = await sessionCallback({ session, token });
|
||||
expect(result.accessToken).toBe('extracted-token-from-jwt');
|
||||
});
|
||||
});
|
||||
|
|
@ -12,6 +12,7 @@ const { mockUseSession, mockUseSWR, mockApiClient } = vi.hoisted(() => ({
|
|||
get hasToken() {
|
||||
return this._accessToken !== null;
|
||||
},
|
||||
get: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
|
|
@ -20,7 +21,14 @@ vi.mock('next-auth/react', () => ({
|
|||
}));
|
||||
|
||||
vi.mock('swr', () => ({
|
||||
default: mockUseSWR,
|
||||
default: vi.fn((key, fetcher) => {
|
||||
if (typeof fetcher === 'function') {
|
||||
try {
|
||||
fetcher();
|
||||
} catch (e) {}
|
||||
}
|
||||
return mockUseSWR(key, fetcher);
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/services/api-client', () => ({
|
||||
|
|
|
|||
|
|
@ -104,4 +104,92 @@ describe('useShoppingListSync', () => {
|
|||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('handles invalid onmessage payload gracefully', () => {
|
||||
const onSync = vi.fn();
|
||||
renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
const ws = createdSockets[0];
|
||||
act(() => { if (ws.onopen) ws.onopen(); });
|
||||
|
||||
// Should swallow invalid JSON parse error
|
||||
act(() => {
|
||||
if (ws.onmessage) ws.onmessage({ data: 'invalid-json' });
|
||||
});
|
||||
expect(onSync).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles ws error states', () => {
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
const ws = createdSockets[0];
|
||||
act(() => { if (ws.onerror) ws.onerror(); });
|
||||
expect(result.current.error).toBe('Connection interrupt');
|
||||
});
|
||||
|
||||
it('handles websocket initialization errors gracefully', () => {
|
||||
global.WebSocket = function() {
|
||||
throw new Error('WS Blocked');
|
||||
} as any;
|
||||
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
expect(result.current.error).toBe('Sync failed to initialize');
|
||||
});
|
||||
|
||||
it('does not connect if householdId or listId is missing', () => {
|
||||
const onSync = vi.fn();
|
||||
renderHook(() => useShoppingListSync('', 'list1', onSync));
|
||||
expect(createdSockets).toHaveLength(0);
|
||||
|
||||
renderHook(() => useShoppingListSync('hh1', '', onSync));
|
||||
expect(createdSockets).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('handles disconnect and uses default severed message when reason is missing', () => {
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
const ws = createdSockets[0];
|
||||
act(() => { if (ws.onopen) ws.onopen(); });
|
||||
act(() => { if (ws.onclose) ws.onclose({} as any); });
|
||||
expect(result.current.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('stops attempting to reconnect after 5 consecutive failures', () => {
|
||||
vi.useFakeTimers();
|
||||
const onSync = vi.fn();
|
||||
renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
|
||||
expect(createdSockets).toHaveLength(1);
|
||||
|
||||
for (let attempt = 1; attempt <= 5; attempt++) {
|
||||
const currentSocket = createdSockets[createdSockets.length - 1];
|
||||
act(() => { if (currentSocket.onclose) currentSocket.onclose({ reason: 'fail' }); });
|
||||
act(() => { vi.advanceTimersByTime(attempt * 1000); });
|
||||
}
|
||||
|
||||
const totalSocketsCreated = createdSockets.length;
|
||||
expect(totalSocketsCreated).toBe(6);
|
||||
|
||||
const finalSocket = createdSockets[totalSocketsCreated - 1];
|
||||
act(() => { if (finalSocket.onclose) finalSocket.onclose({ reason: 'fail' }); });
|
||||
act(() => { vi.advanceTimersByTime(6000); });
|
||||
|
||||
expect(createdSockets.length).toBe(totalSocketsCreated);
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('does not send toggle item message if socket is not open', () => {
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
|
||||
const ws = createdSockets[0];
|
||||
ws.readyState = 0; // CONNECTING
|
||||
|
||||
act(() => {
|
||||
result.current.toggleItemCheck('item1', true);
|
||||
});
|
||||
|
||||
expect(ws.send).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
19
packages/web/tests/proxy.test.ts
Normal file
19
packages/web/tests/proxy.test.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { vi, describe, it, expect } from 'vitest';
|
||||
|
||||
// Mock the auth module to prevent loading NextAuth server-side runtime dependencies
|
||||
vi.mock('@/lib/auth', () => {
|
||||
return {
|
||||
auth: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
import { proxy, config } from '../src/proxy';
|
||||
|
||||
describe('middleware proxy configuration', () => {
|
||||
it('exports proxy and route matcher configurations successfully', () => {
|
||||
expect(proxy).toBeDefined();
|
||||
expect(config).toBeDefined();
|
||||
expect(config.matcher).toBeDefined();
|
||||
expect(config.matcher[0]).toContain('api/auth');
|
||||
});
|
||||
});
|
||||
|
|
@ -53,6 +53,12 @@ describe('ApiClient', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('baseUrl', () => {
|
||||
it('returns the configured API base URL', () => {
|
||||
expect(apiClient.baseUrl).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('makes GET request to correct URL', async () => {
|
||||
const spy = mockFetch({ id: '1' });
|
||||
|
|
@ -130,6 +136,16 @@ describe('ApiClient', () => {
|
|||
expect.objectContaining({ method: 'DELETE' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('includes Authorization header in DELETE request when token is set', async () => {
|
||||
const spy = mockFetch(undefined, 204);
|
||||
apiClient.accessToken = 'delete-jwt';
|
||||
|
||||
await apiClient.delete('/stores/1');
|
||||
|
||||
const headers = spy.mock.calls[0][1]?.headers as Record<string, string>;
|
||||
expect(headers['Authorization']).toBe('Bearer delete-jwt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('error handling', () => {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,12 @@ describe('cabinet-events service', () => {
|
|||
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('startDate=2026-01-01'));
|
||||
});
|
||||
|
||||
it('getEventsByItem with no query', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await getEventsByItem('hh1', 'ci-1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/cabinet-events/by-item/ci-1');
|
||||
});
|
||||
|
||||
it('getEventsByItem builds URL with cabinetItemId', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await getEventsByItem('hh1', 'ci-1', { limit: 5 });
|
||||
|
|
|
|||
|
|
@ -69,6 +69,12 @@ describe('medicines service', () => {
|
|||
expect(mockDelete).toHaveBeenCalledWith('/households/hh1/medicines/med-1');
|
||||
});
|
||||
|
||||
it('listMedicineProducts with no query', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listMedicineProducts('hh1', 'med-1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/medicines/med-1/products');
|
||||
});
|
||||
|
||||
it('listMedicineProducts builds URL with medicineId', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listMedicineProducts('hh1', 'med-1', { limit: 5 });
|
||||
|
|
|
|||
|
|
@ -31,9 +31,10 @@ describe('purchases service', () => {
|
|||
|
||||
it('listPurchases builds query string', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listPurchases('hh1', { status: 'ordered', storeId: 'st-1', limit: 10 });
|
||||
await listPurchases('hh1', { status: 'ordered', storeId: 'st-1', limit: 10, cursor: 'curr-123' });
|
||||
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('status=ordered'));
|
||||
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('storeId=st-1'));
|
||||
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=curr-123'));
|
||||
});
|
||||
|
||||
it('getPurchase calls GET', async () => {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,12 @@ describe('refills service', () => {
|
|||
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('userId=u-1'));
|
||||
});
|
||||
|
||||
it('listRefillLists with no query', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listRefillLists('hh1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/refills/lists');
|
||||
});
|
||||
|
||||
it('listRefillLists with query', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listRefillLists('hh1', { status: 'active' });
|
||||
|
|
|
|||
|
|
@ -83,5 +83,10 @@ describe('shopping-lists service', () => {
|
|||
apiClient.baseUrl = 'https://api.meshitrack.com';
|
||||
const urlSecure = ShoppingListsService.getShoppingListSyncSocketUrl('hh1', 'list1');
|
||||
expect(urlSecure).toBe('wss://api.meshitrack.com/households/hh1/shopping-lists/list1/sync');
|
||||
|
||||
// Test falsy baseUrl fallback path
|
||||
(apiClient as any).baseUrl = '';
|
||||
const urlFalsy = ShoppingListsService.getShoppingListSyncSocketUrl('hh1', 'list1');
|
||||
expect(urlFalsy).toBe('ws:///households/hh1/shopping-lists/list1/sync');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue