2026-04-18 12:36:29 +09:00
|
|
|
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(),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-05-19 11:06:03 +09:00
|
|
|
vi.mock('../../src/services/api-client', () => ({
|
2026-04-18 12:36:29 +09:00
|
|
|
apiClient: { get: mockGet, post: mockPost, patch: mockPatch, delete: mockDelete },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
import {
|
2026-04-26 18:44:59 +09:00
|
|
|
listMedicines,
|
|
|
|
|
getMedicine,
|
|
|
|
|
createMedicine,
|
|
|
|
|
updateMedicine,
|
|
|
|
|
deleteMedicine,
|
|
|
|
|
listMedicineProducts,
|
|
|
|
|
createMedicineProduct,
|
|
|
|
|
updateMedicineProduct,
|
|
|
|
|
deleteMedicineProduct,
|
2026-05-19 11:06:03 +09:00
|
|
|
} from '../../src/services/medicines';
|
2026-04-18 12:36:29 +09:00
|
|
|
|
|
|
|
|
beforeEach(() => vi.clearAllMocks());
|
|
|
|
|
|
|
|
|
|
describe('medicines service', () => {
|
|
|
|
|
it('listMedicines with no query', async () => {
|
|
|
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
|
|
|
await listMedicines('hh1');
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith('/households/hh1/medicines');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('listMedicines builds query string', async () => {
|
|
|
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
|
|
|
await listMedicines('hh1', { q: 'aspirin', category: 'pain', form: 'tablet', limit: 10 });
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('q=aspirin'));
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('category=pain'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('listMedicines with cursor', async () => {
|
|
|
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
|
|
|
await listMedicines('hh1', { cursor: 'cur1' });
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=cur1'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('getMedicine calls GET', async () => {
|
|
|
|
|
mockGet.mockResolvedValue({ _id: 'med-1' });
|
|
|
|
|
await getMedicine('hh1', 'med-1');
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith('/households/hh1/medicines/med-1');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('createMedicine calls POST', async () => {
|
|
|
|
|
mockPost.mockResolvedValue({ _id: 'med-1' });
|
|
|
|
|
await createMedicine('hh1', { name: 'Aspirin' } as never);
|
|
|
|
|
expect(mockPost).toHaveBeenCalledWith('/households/hh1/medicines', { name: 'Aspirin' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updateMedicine calls PATCH', async () => {
|
|
|
|
|
mockPatch.mockResolvedValue({ _id: 'med-1' });
|
|
|
|
|
await updateMedicine('hh1', 'med-1', { name: 'Updated' } as never);
|
|
|
|
|
expect(mockPatch).toHaveBeenCalledWith('/households/hh1/medicines/med-1', { name: 'Updated' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('deleteMedicine calls DELETE', async () => {
|
|
|
|
|
mockDelete.mockResolvedValue(undefined);
|
|
|
|
|
await deleteMedicine('hh1', 'med-1');
|
|
|
|
|
expect(mockDelete).toHaveBeenCalledWith('/households/hh1/medicines/med-1');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('listMedicineProducts builds URL with medicineId', async () => {
|
|
|
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
|
|
|
await listMedicineProducts('hh1', 'med-1', { limit: 5 });
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('/medicines/med-1/products'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('listMedicineProducts with cursor', async () => {
|
|
|
|
|
mockGet.mockResolvedValue({ data: [] });
|
|
|
|
|
await listMedicineProducts('hh1', 'med-1', { cursor: 'cur1' });
|
|
|
|
|
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=cur1'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('createMedicineProduct calls POST', async () => {
|
|
|
|
|
mockPost.mockResolvedValue({ _id: 'mp-1' });
|
|
|
|
|
await createMedicineProduct('hh1', 'med-1', { brand: 'Bayer' } as never);
|
2026-04-26 18:44:59 +09:00
|
|
|
expect(mockPost).toHaveBeenCalledWith('/households/hh1/medicines/med-1/products', {
|
|
|
|
|
brand: 'Bayer',
|
|
|
|
|
});
|
2026-04-18 12:36:29 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updateMedicineProduct uses medicine-products path', async () => {
|
|
|
|
|
mockPatch.mockResolvedValue({ _id: 'mp-1' });
|
|
|
|
|
await updateMedicineProduct('hh1', 'mp-1', { brand: 'Updated' } as never);
|
2026-04-26 18:44:59 +09:00
|
|
|
expect(mockPatch).toHaveBeenCalledWith('/households/hh1/medicine-products/mp-1', {
|
|
|
|
|
brand: 'Updated',
|
|
|
|
|
});
|
2026-04-18 12:36:29 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('deleteMedicineProduct calls DELETE', async () => {
|
|
|
|
|
mockDelete.mockResolvedValue(undefined);
|
|
|
|
|
await deleteMedicineProduct('hh1', 'mp-1');
|
|
|
|
|
expect(mockDelete).toHaveBeenCalledWith('/households/hh1/medicine-products/mp-1');
|
|
|
|
|
});
|
|
|
|
|
});
|