diff --git a/lib/src/tests/halAutoComplete.test.jsx b/lib/src/tests/halAutoComplete.test.jsx index 09938b7..31a3c50 100644 --- a/lib/src/tests/halAutoComplete.test.jsx +++ b/lib/src/tests/halAutoComplete.test.jsx @@ -1,5 +1,6 @@ import { fireEvent, render, waitFor } from "@testing-library/react"; import HalAutocomplete from "../components/HalAutocomplete"; +import { HalApiCaller } from "@dxc-technology/halstack-client"; // Mocking DOMRect for Radix Primitive Popover global.globalThis = global; @@ -11,6 +12,7 @@ global.ResizeObserver = class ResizeObserver { unobserve() {} disconnect() {} }; + describe("HalAutocomplete component tests", () => { test("HalAutocomplete renders with correct options", async () => { const { getByRole, getByText } = render( @@ -26,4 +28,85 @@ describe("HalAutocomplete component tests", () => { expect(list).toBeTruthy(); await waitFor(() => expect(getByText("CGU AUTO PA / SCOB mandatory")).toBeTruthy()); }); + + test("HalAutocomplete renders empty without collectionUrl", async () => { + const { queryByRole } = render( + + ); + const list = queryByRole("listbox"); + expect(list).not.toBeTruthy(); + }); + + test("HalAutocomplete calls asyncHeadersHandler", async () => { + const mockAsyncHeadersHandler = jest.fn(); + + render( + + ); + + await waitFor(() => expect(mockAsyncHeadersHandler).toHaveBeenCalled()); + + expect(mockAsyncHeadersHandler).toHaveBeenCalledTimes(1); + }); +}); + +describe("HalAutocomplete component URL handling tests", () => { + test("API call adds '&' if URL includes '?'", async () => { + const mockedGet = jest.fn(); + HalApiCaller.get = mockedGet.mockResolvedValue({ + halResource: { getItems: () => [{ summary: { baseCompany: "Test Company" } }] }, + }); + + const { getByRole } = render( + + ); + + const input = getByRole("combobox"); + fireEvent.change(input, { target: { value: "test" } }); + fireEvent.focus(input); + + await waitFor(() => { + expect(mockedGet).toHaveBeenCalledWith( + expect.objectContaining({ + url: "http://example.com?existing=true&baseCompany=test", + }) + ); + }); + }); + + test("API call adds '?' if URL does not include '?'", async () => { + const mockedGet = jest.fn(); + HalApiCaller.get = mockedGet.mockResolvedValue({ + halResource: { getItems: () => [{ summary: { baseCompany: "Test Company" } }] }, + }); + + const { getByRole } = render( + + ); + + const input = getByRole("combobox"); + fireEvent.change(input, { target: { value: "test" } }); + fireEvent.focus(input); + + await waitFor(() => { + expect(mockedGet).toHaveBeenCalledWith( + expect.objectContaining({ + url: "http://example.com?baseCompany=test", + }) + ); + }); + }); });