92 lines
3.2 KiB
TypeScript
92 lines
3.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
const { mockGet, mockPost, mockPatch } = vi.hoisted(() => ({
|
|
mockGet: vi.fn(),
|
|
mockPost: vi.fn(),
|
|
mockPatch: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../src/services/api-client', () => ({
|
|
apiClient: { get: mockGet, post: mockPost, patch: mockPatch },
|
|
}));
|
|
|
|
import {
|
|
getRefillAlerts,
|
|
listRefillLists,
|
|
createRefillList,
|
|
getRefillList,
|
|
updateRefillList,
|
|
updateRefillListItem,
|
|
addToCabinet,
|
|
} from '../../src/services/refills';
|
|
|
|
beforeEach(() => vi.clearAllMocks());
|
|
|
|
describe('refills service', () => {
|
|
it('getRefillAlerts with no query', async () => {
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
await getRefillAlerts('hh1');
|
|
expect(mockGet).toHaveBeenCalledWith('/households/hh1/refills/alerts');
|
|
});
|
|
|
|
it('getRefillAlerts builds query string', async () => {
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
await getRefillAlerts('hh1', { thresholdDays: 14, userId: 'u-1' });
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('thresholdDays=14'));
|
|
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' });
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('status=active'));
|
|
});
|
|
|
|
it('listRefillLists with cursor and limit', async () => {
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
await listRefillLists('hh1', { cursor: 'cur1', limit: 25 });
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=cur1'));
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('limit=25'));
|
|
});
|
|
|
|
it('createRefillList calls POST', async () => {
|
|
mockPost.mockResolvedValue({ _id: 'rl-1' });
|
|
const data = { name: 'Weekly' } as never;
|
|
await createRefillList('hh1', data);
|
|
expect(mockPost).toHaveBeenCalledWith('/households/hh1/refills/lists', data);
|
|
});
|
|
|
|
it('getRefillList calls GET', async () => {
|
|
mockGet.mockResolvedValue({ _id: 'rl-1' });
|
|
await getRefillList('hh1', 'rl-1');
|
|
expect(mockGet).toHaveBeenCalledWith('/households/hh1/refills/lists/rl-1');
|
|
});
|
|
|
|
it('updateRefillList calls PATCH', async () => {
|
|
mockPatch.mockResolvedValue({ _id: 'rl-1' });
|
|
await updateRefillList('hh1', 'rl-1', { name: 'Updated' } as never);
|
|
expect(mockPatch).toHaveBeenCalledWith('/households/hh1/refills/lists/rl-1', {
|
|
name: 'Updated',
|
|
});
|
|
});
|
|
|
|
it('updateRefillListItem calls PATCH with nested path', async () => {
|
|
mockPatch.mockResolvedValue({ _id: 'rl-1' });
|
|
await updateRefillListItem('hh1', 'rl-1', 'item-1', { purchased: true } as never);
|
|
expect(mockPatch).toHaveBeenCalledWith('/households/hh1/refills/lists/rl-1/items/item-1', {
|
|
purchased: true,
|
|
});
|
|
});
|
|
|
|
it('addToCabinet calls POST with empty body', async () => {
|
|
mockPost.mockResolvedValue({});
|
|
await addToCabinet('hh1', 'rl-1');
|
|
expect(mockPost).toHaveBeenCalledWith('/households/hh1/refills/lists/rl-1/add-to-cabinet', {});
|
|
});
|
|
});
|