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 tasks CRUD', () => { it('getTasks sends GET /tasks and unwraps data', async () => { const { client, mockInstance } = makeClient(); const payload = { tasks: [{ id: 8, type: 'cleaning', status: 'pending' }], total: 1 }; mockInstance.request.mockResolvedValue({ data: { error_code: 200, data: payload } }); const res = await client.getTasks({ property_id: 1, status: 'pending' }); expect(mockInstance.request).toHaveBeenCalledWith({ method: 'GET', url: '/tasks', params: { property_id: 1, status: 'pending' }, }); expect(res).toEqual(payload); }); it('createTask sends POST /tasks', async () => { const { client, mockInstance } = makeClient(); mockInstance.post.mockResolvedValue({ data: { error_code: 200, data: { task_id: 8 } }, }); await client.createTask({ type: 'cleaning', expected_date: '2026-06-12', currency: 'USD' }); expect(mockInstance.post).toHaveBeenCalledWith('/tasks', { type: 'cleaning', expected_date: '2026-06-12', currency: 'USD', }); }); it('createTask throws when expected_date is missing', async () => { const { client } = makeClient(); await expect( client.createTask({ type: 'cleaning', currency: 'USD' } as any) ).rejects.toThrow('expected_date is required'); }); it('updateTask sends PATCH /tasks/:id without id in body', async () => { const { client, mockInstance } = makeClient(); mockInstance.patch.mockResolvedValue({ data: { error_code: 200 } }); await client.updateTask({ id: 8, status: 'completed' }); expect(mockInstance.patch).toHaveBeenCalledWith('/tasks/8', { status: 'completed' }); }); it('deleteTask sends DELETE /tasks/:id', async () => { const { client, mockInstance } = makeClient(); mockInstance.delete.mockResolvedValue({ data: { error_code: 200 } }); await client.deleteTask(8); expect(mockInstance.delete).toHaveBeenCalledWith('/tasks/8'); }); });