Phases 6-7
This commit is contained in:
parent
76a516a417
commit
029940b079
111 changed files with 17247 additions and 447 deletions
124
packages/web/src/services/__tests__/pantry.test.ts
Normal file
124
packages/web/src/services/__tests__/pantry.test.ts
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { mockGet, mockPost, mockPatch, mockDelete } = vi.hoisted(() => ({
|
||||
mockGet: vi.fn(),
|
||||
mockPost: vi.fn(),
|
||||
mockPatch: vi.fn(),
|
||||
mockDelete: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../api-client', () => ({
|
||||
apiClient: { get: mockGet, post: mockPost, patch: mockPatch, delete: mockDelete },
|
||||
}));
|
||||
|
||||
import {
|
||||
listPantryItems,
|
||||
getPantryItem,
|
||||
createPantryItem,
|
||||
updatePantryItem,
|
||||
transitionPantryItem,
|
||||
batchTransitionPantryItems,
|
||||
getExpiringSoon,
|
||||
getWasteStats,
|
||||
deletePantryItem,
|
||||
} from '../pantry';
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
describe('pantry service', () => {
|
||||
it('listPantryItems with no query', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listPantryItems('hh1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/pantry');
|
||||
});
|
||||
|
||||
it('listPantryItems builds query string', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listPantryItems('hh1', {
|
||||
storageLocation: 'fridge',
|
||||
status: 'sealed',
|
||||
urgency: 'urgent',
|
||||
productId: 'p1',
|
||||
limit: 10,
|
||||
});
|
||||
const url = mockGet.mock.calls[0][0] as string;
|
||||
expect(url).toContain('storageLocation=fridge');
|
||||
expect(url).toContain('status=sealed');
|
||||
expect(url).toContain('urgency=urgent');
|
||||
expect(url).toContain('productId=p1');
|
||||
expect(url).toContain('limit=10');
|
||||
});
|
||||
|
||||
it('listPantryItems with cursor', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await listPantryItems('hh1', { cursor: 'cur1' });
|
||||
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=cur1'));
|
||||
});
|
||||
|
||||
it('getPantryItem calls GET', async () => {
|
||||
mockGet.mockResolvedValue({ _id: 'item1' });
|
||||
await getPantryItem('hh1', 'item1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/pantry/item1');
|
||||
});
|
||||
|
||||
it('createPantryItem calls POST', async () => {
|
||||
mockPost.mockResolvedValue({ _id: 'item1' });
|
||||
const data = { productId: 'p1', storageLocation: 'fridge', quantity: 1, unit: 'piece' };
|
||||
await createPantryItem('hh1', data as never);
|
||||
expect(mockPost).toHaveBeenCalledWith('/households/hh1/pantry', data);
|
||||
});
|
||||
|
||||
it('updatePantryItem calls PATCH', async () => {
|
||||
mockPatch.mockResolvedValue({ _id: 'item1' });
|
||||
await updatePantryItem('hh1', 'item1', { quantity: 2 });
|
||||
expect(mockPatch).toHaveBeenCalledWith('/households/hh1/pantry/item1', { quantity: 2 });
|
||||
});
|
||||
|
||||
it('transitionPantryItem calls POST', async () => {
|
||||
mockPost.mockResolvedValue({ _id: 'item1' });
|
||||
await transitionPantryItem('hh1', 'item1', { status: 'opened' as never });
|
||||
expect(mockPost).toHaveBeenCalledWith('/households/hh1/pantry/item1/transition', {
|
||||
status: 'opened',
|
||||
});
|
||||
});
|
||||
|
||||
it('batchTransitionPantryItems calls POST', async () => {
|
||||
mockPost.mockResolvedValue({ transitioned: 2, failed: 0 });
|
||||
const data = { itemIds: ['a', 'b'], status: 'consumed' as const };
|
||||
await batchTransitionPantryItems('hh1', data as never);
|
||||
expect(mockPost).toHaveBeenCalledWith('/households/hh1/pantry/batch-transition', data);
|
||||
});
|
||||
|
||||
it('getExpiringSoon with no query', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await getExpiringSoon('hh1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/pantry/expiring-soon');
|
||||
});
|
||||
|
||||
it('getExpiringSoon builds query string', async () => {
|
||||
mockGet.mockResolvedValue({ data: [] });
|
||||
await getExpiringSoon('hh1', { days: 3, cursor: 'c1', limit: 5 });
|
||||
const url = mockGet.mock.calls[0][0] as string;
|
||||
expect(url).toContain('days=3');
|
||||
expect(url).toContain('cursor=c1');
|
||||
expect(url).toContain('limit=5');
|
||||
});
|
||||
|
||||
it('getWasteStats with no period', async () => {
|
||||
mockGet.mockResolvedValue({ wastePercentage: 10 });
|
||||
await getWasteStats('hh1');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/pantry/stats');
|
||||
});
|
||||
|
||||
it('getWasteStats with period', async () => {
|
||||
mockGet.mockResolvedValue({ wastePercentage: 10 });
|
||||
await getWasteStats('hh1', 'week');
|
||||
expect(mockGet).toHaveBeenCalledWith('/households/hh1/pantry/stats?period=week');
|
||||
});
|
||||
|
||||
it('deletePantryItem calls DELETE', async () => {
|
||||
mockDelete.mockResolvedValue(undefined);
|
||||
await deletePantryItem('hh1', 'item1');
|
||||
expect(mockDelete).toHaveBeenCalledWith('/households/hh1/pantry/item1');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue