MeshiTrack/packages/web/src/services/__tests__/refills.test.ts

87 lines
3 KiB
TypeScript
Raw Normal View History

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('../api-client', () => ({
apiClient: { get: mockGet, post: mockPost, patch: mockPatch },
}));
import {
2026-04-26 18:44:59 +09:00
getRefillAlerts,
listRefillLists,
createRefillList,
getRefillList,
updateRefillList,
updateRefillListItem,
addToCabinet,
} from '../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 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);
2026-04-26 18:44:59 +09:00
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);
2026-04-26 18:44:59 +09:00
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', {});
});
});