394 search block empty field search on enter (#401)
Co-authored-by: Camille Moussu <cmoussu@linagora.com>
This commit is contained in:
@@ -182,10 +182,7 @@ describe("EventSearchBar", () => {
|
||||
fireEvent.click(searchButton);
|
||||
|
||||
// Click tune icon
|
||||
const tuneButtons = screen.getAllByRole("button");
|
||||
const tuneButton = tuneButtons.find((btn) =>
|
||||
btn.querySelector('[data-testid="TuneIcon"]')
|
||||
);
|
||||
const tuneButton = screen.getByTestId("TuneIcon");
|
||||
if (tuneButton) fireEvent.click(tuneButton);
|
||||
|
||||
await waitFor(() => {
|
||||
@@ -232,4 +229,38 @@ describe("EventSearchBar", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
it("should not trigger search on Enter key when search is empty", async () => {
|
||||
const searchSpy = jest.spyOn(searchThunk, "searchEventsAsync");
|
||||
|
||||
renderWithProviders(<SearchBar />, preloadedState);
|
||||
|
||||
const searchButton = screen.getByRole("button");
|
||||
fireEvent.click(searchButton);
|
||||
|
||||
const searchInput = screen.getByPlaceholderText("common.search");
|
||||
fireEvent.keyDown(searchInput, { key: "Enter" });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(searchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("should not trigger search when filter keywords is empty", async () => {
|
||||
const searchSpy = jest.spyOn(searchThunk, "searchEventsAsync");
|
||||
|
||||
renderWithProviders(<SearchBar />, preloadedState);
|
||||
|
||||
const searchButton = screen.getByRole("button");
|
||||
fireEvent.click(searchButton);
|
||||
|
||||
// Click tune icon
|
||||
const tuneButton = screen.getByTestId("TuneIcon");
|
||||
if (tuneButton) fireEvent.click(tuneButton);
|
||||
|
||||
fireEvent.click(screen.getByText("common.search"));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(searchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -84,6 +84,20 @@ export default function SearchBar() {
|
||||
};
|
||||
|
||||
const handleSearch = async () => {
|
||||
const trimmedSearch = search.trim();
|
||||
const trimmedKeywords = filters.keywords.trim();
|
||||
|
||||
// Block search if all search criteria are empty
|
||||
const hasSearchCriteria =
|
||||
trimmedSearch ||
|
||||
trimmedKeywords ||
|
||||
filters.organizers.length > 0 ||
|
||||
filters.attendees.length > 0;
|
||||
|
||||
if (!hasSearchCriteria) {
|
||||
return;
|
||||
}
|
||||
|
||||
let searchInCalendars: string[];
|
||||
|
||||
if (filters.searchIn === "" || !filters.searchIn) {
|
||||
@@ -97,7 +111,7 @@ export default function SearchBar() {
|
||||
}
|
||||
|
||||
const cleanedFilters = {
|
||||
...filters,
|
||||
keywords: trimmedKeywords,
|
||||
organizers: filters.organizers.map((u) => u.cal_address),
|
||||
attendees: filters.attendees.map((u) => u.cal_address),
|
||||
searchIn: searchInCalendars,
|
||||
@@ -105,7 +119,7 @@ export default function SearchBar() {
|
||||
|
||||
dispatch(
|
||||
searchEventsAsync({
|
||||
search,
|
||||
search: trimmedSearch,
|
||||
filters: cleanedFilters,
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user