Skip to content

Commit

Permalink
Merge pull request #80 from dxc-technology/Mil4n0r/improve_tests_cove…
Browse files Browse the repository at this point in the history
…rage

Added improvements to coverage for the existing tests in HalAutocomplete
  • Loading branch information
GomezIvann authored Dec 5, 2024
2 parents f4c5c7c + 994c42b commit fbeede7
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions lib/src/tests/halAutoComplete.test.jsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
Expand All @@ -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(
<HalAutocomplete propertyName="baseCompany" label="Company name" />
);
const list = queryByRole("listbox");
expect(list).not.toBeTruthy();
});

test("HalAutocomplete calls asyncHeadersHandler", async () => {
const mockAsyncHeadersHandler = jest.fn();

render(
<HalAutocomplete
collectionUrl="http://localhost:3000/response"
propertyName="baseCompany"
label="Company name"
asyncHeadersHandler={mockAsyncHeadersHandler}
/>
);

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(
<HalAutocomplete
collectionUrl="http://example.com?existing=true"
propertyName="baseCompany"
label="Company"
/>
);

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(
<HalAutocomplete
collectionUrl="http://example.com"
propertyName="baseCompany"
label="Company"
/>
);

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",
})
);
});
});
});

0 comments on commit fbeede7

Please sign in to comment.