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 groups CRUD', () => { it('getGroups sends GET /groups and unwraps data', async () => { const { client, mockInstance } = makeClient(); const payload = { groups: [{ id: 1, name: 'Downtown', property_ids: [] }], total: 1 }; mockInstance.request.mockResolvedValue({ data: { error_code: 200, data: payload } }); const res = await client.getGroups({ id: 1 }); expect(mockInstance.request).toHaveBeenCalledWith({ method: 'GET', url: '/groups', params: { id: 1 }, }); expect(res).toEqual(payload); }); it('createGroup sends POST /groups', async () => { const { client, mockInstance } = makeClient(); mockInstance.post.mockResolvedValue({ data: { error_code: 200, data: { group: { id: 2, name: 'Downtown', property_ids: [] } } }, }); await client.createGroup({ name: 'Downtown' }); expect(mockInstance.post).toHaveBeenCalledWith('/groups', { name: 'Downtown' }); }); it('createGroup throws when name is missing', async () => { const { client } = makeClient(); await expect(client.createGroup({} as any)).rejects.toThrow('name is required'); }); it('updateGroup sends PATCH /groups/:id without id in body', async () => { const { client, mockInstance } = makeClient(); mockInstance.patch.mockResolvedValue({ data: { error_code: 200 } }); await client.updateGroup({ id: 2, name: 'Uptown', property_ids: [3] }); expect(mockInstance.patch).toHaveBeenCalledWith('/groups/2', { name: 'Uptown', property_ids: [3], }); }); it('deleteGroup sends DELETE /groups/:id', async () => { const { client, mockInstance } = makeClient(); mockInstance.delete.mockResolvedValue({ data: { error_code: 200 } }); await client.deleteGroup(2); expect(mockInstance.delete).toHaveBeenCalledWith('/groups/2'); }); });