diff --git a/__test__/features/Search/EventSearchBar.test.tsx b/__test__/features/Search/EventSearchBar.test.tsx index 1c06c64..042cfc4 100644 --- a/__test__/features/Search/EventSearchBar.test.tsx +++ b/__test__/features/Search/EventSearchBar.test.tsx @@ -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(, 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(, 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(); + }); + }); }); diff --git a/src/components/Menubar/EventSearchBar.tsx b/src/components/Menubar/EventSearchBar.tsx index d4d988c..5c26232 100644 --- a/src/components/Menubar/EventSearchBar.tsx +++ b/src/components/Menubar/EventSearchBar.tsx @@ -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, }) );