import axios from 'axios'; import { HostexApiClient } from '../client/index.js'; jest.mock('axios'); const mockedAxios = axios as jest.Mocked; function makeClient() { const mockInstance = { request: jest.fn(), post: jest.fn(), patch: jest.fn(), delete: jest.fn(), interceptors: { request: { use: jest.fn() }, response: { use: jest.fn() } }, }; mockedAxios.create.mockReturnValue(mockInstance as any); return { client: new HostexApiClient({ accessToken: 'test-token' }), mockInstance }; } describe('HostexApiClient staffs CRUD', () => { it('getStaffs sends GET /staffs and unwraps data', async () => { const { client, mockInstance } = makeClient(); const payload = { staffs: [{ id: 5, name: 'Ana', is_active: true }], total: 1 }; mockInstance.request.mockResolvedValue({ data: { error_code: 200, data: payload } }); const res = await client.getStaffs({ is_active: 1 }); expect(mockInstance.request).toHaveBeenCalledWith({ method: 'GET', url: '/staffs', params: { is_active: 1 }, }); expect(res).toEqual(payload); }); it('createStaff sends POST /staffs', async () => { const { client, mockInstance } = makeClient(); mockInstance.post.mockResolvedValue({ data: { error_code: 200, data: { staff_id: 5 } }, }); await client.createStaff({ name: 'Ana', mobile: '+1 5551234' }); expect(mockInstance.post).toHaveBeenCalledWith('/staffs', { name: 'Ana', mobile: '+1 5551234', }); }); it('createStaff throws when mobile is missing', async () => { const { client } = makeClient(); await expect(client.createStaff({ name: 'Ana' } as any)).rejects.toThrow('mobile is required'); }); it('updateStaff sends PATCH /staffs/:id without id in body', async () => { const { client, mockInstance } = makeClient(); mockInstance.patch.mockResolvedValue({ data: { error_code: 200 } }); await client.updateStaff({ id: 5, is_active: false }); expect(mockInstance.patch).toHaveBeenCalledWith('/staffs/5', { is_active: false }); }); it('deleteStaff sends DELETE /staffs/:id', async () => { const { client, mockInstance } = makeClient(); mockInstance.delete.mockResolvedValue({ data: { error_code: 200 } }); await client.deleteStaff(5); expect(mockInstance.delete).toHaveBeenCalledWith('/staffs/5'); }); });