Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added improvements to coverage for the existing tests in HalAutocomplete #80

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
})
);
});
});
});
Loading