Skip to content

Commit 207669a

Browse files
Feat: [EBE-168] Test Class for Autocomplete component (#99)
Co-authored-by: Emanuele Stazzi <>
1 parent 636f0a6 commit 207669a

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/components/Autocomplete/AutocompleteComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default function AutocompleteComponent({
5353

5454
const getHelperText = () => {
5555
if (!inputError) { return ''; }
56-
return errorText || MANDATORY_FIELD;
56+
return errorText ?? MANDATORY_FIELD;
5757
};
5858

5959
return (
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { render, screen, fireEvent} from '@testing-library/react';
2+
import "@testing-library/jest-dom";
3+
import AutocompleteComponent from "../AutocompleteComponent";
4+
5+
6+
jest.mock("react-i18next", () => ({
7+
useTranslation: () => ({
8+
t: (key: string) => {
9+
if (key === "pages.pointOfSales.noOptionsText") return "Nessuna opzione";
10+
if (key === "pages.pointOfSales.loadingText") return "Caricamento...";
11+
return key;
12+
},
13+
}),
14+
}));
15+
16+
jest.mock("../../../utils/constants", () => ({
17+
MANDATORY_FIELD: "Campo obbligatorio",
18+
}));
19+
20+
describe("AutocompleteComponent", () => {
21+
beforeEach(() => {
22+
jest.useFakeTimers();
23+
});
24+
25+
afterEach(() => {
26+
jest.useRealTimers();
27+
});
28+
29+
it("renders correctly with label", () => {
30+
render(<AutocompleteComponent options={[]} label="Cerca indirizzo" />);
31+
expect(screen.getByLabelText("Cerca indirizzo")).toBeInTheDocument();
32+
});
33+
34+
it("does not trigger onChangeDebounce for input shorter than 5 chars", () => {
35+
const onChangeDebounce = jest.fn();
36+
render(
37+
<AutocompleteComponent
38+
options={[]}
39+
onChangeDebounce={onChangeDebounce}
40+
label="Test"
41+
/>
42+
);
43+
const input = screen.getByLabelText("Test");
44+
45+
fireEvent.change(input, { target: { value: "abcd" } });
46+
jest.advanceTimersByTime(1000);
47+
48+
expect(onChangeDebounce).not.toHaveBeenCalled();
49+
expect(screen.queryByRole("progressbar")).toBeNull();
50+
});
51+
52+
53+
it("shows error message when inputError is true", () => {
54+
render(<AutocompleteComponent options={[]} inputError label="Campo" />);
55+
expect(screen.getByText("Campo obbligatorio")).toBeInTheDocument();
56+
});
57+
58+
it("shows custom error message if errorText is provided", () => {
59+
render(
60+
<AutocompleteComponent
61+
options={[]}
62+
inputError
63+
errorText="Errore custom"
64+
label="Campo"
65+
/>
66+
);
67+
expect(screen.getByText("Errore custom")).toBeInTheDocument();
68+
});
69+
70+
it("calls onChange when an option is selected", () => {
71+
const onChange = jest.fn();
72+
const options = [{ Address: { Label: "Via Roma 1" } }];
73+
render(
74+
<AutocompleteComponent options={options} onChange={onChange} label="Seleziona" />
75+
);
76+
77+
const input = screen.getByLabelText("Seleziona");
78+
fireEvent.mouseDown(input);
79+
80+
const option = screen.getByText("Via Roma 1");
81+
fireEvent.click(option);
82+
83+
expect(onChange).toHaveBeenCalledWith({ Address: { Label: "Via Roma 1" } });
84+
});
85+
});

0 commit comments

Comments
 (0)