From 3a39486468acd4ec40dc3c2e99751914bd31ad0e Mon Sep 17 00:00:00 2001 From: R Midhun Suresh Date: Fri, 21 Mar 2025 16:16:20 +0530 Subject: [PATCH] RoomListViewModel: Reset any primary filter on secondary filter change (#29562) * Reset primary filter when secondary filter is applied * Add test --- .../viewmodels/roomlist/useFilteredRooms.tsx | 20 +++++----------- .../roomlist/RoomListViewModel-test.tsx | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/components/viewmodels/roomlist/useFilteredRooms.tsx b/src/components/viewmodels/roomlist/useFilteredRooms.tsx index 68f8e3e380..5e1554fcdc 100644 --- a/src/components/viewmodels/roomlist/useFilteredRooms.tsx +++ b/src/components/viewmodels/roomlist/useFilteredRooms.tsx @@ -145,22 +145,14 @@ export function useFilteredRooms(): FilteredRooms { // SecondaryFilter is an enum for the UI, let's convert it to something // that the store will understand. const secondary = secondaryFiltersToFilterKeyMap.get(filter); - - // Active primary filter may need to be toggled off when applying this secondary filer. - let primary = primaryFilter; - if ( - primaryFilter !== undefined && - secondary !== undefined && - !isPrimaryFilterCompatible(primaryFilter, secondary) - ) { - primary = undefined; - } - setActiveSecondaryFilter(filter); - setPrimaryFilter(primary); - updateRoomsFromStore(filterUndefined([primary, secondary])); + + // Reset any active primary filters. + setPrimaryFilter(undefined); + + updateRoomsFromStore(filterUndefined([secondary])); }, - [activeSecondaryFilter, primaryFilter, updateRoomsFromStore], + [activeSecondaryFilter, updateRoomsFromStore], ); /** diff --git a/test/unit-tests/components/viewmodels/roomlist/RoomListViewModel-test.tsx b/test/unit-tests/components/viewmodels/roomlist/RoomListViewModel-test.tsx index f14a7e0acd..07421fc0b3 100644 --- a/test/unit-tests/components/viewmodels/roomlist/RoomListViewModel-test.tsx +++ b/test/unit-tests/components/viewmodels/roomlist/RoomListViewModel-test.tsx @@ -162,6 +162,29 @@ describe("RoomListViewModel", () => { expect(vm.current.activePrimaryFilter).toEqual(vm.current.primaryFilters[i]); }); + it("should remove any active primary filters when secondary filter is changed", async () => { + const { fn } = mockAndCreateRooms(); + const { result: vm } = renderHook(() => useRoomListViewModel()); + + // Let's first toggle the People filter + const i = vm.current.primaryFilters.findIndex((f) => f.name === "People"); + act(() => { + vm.current.primaryFilters[i].toggle(); + }); + expect(vm.current.primaryFilters[i].active).toEqual(true); + + // Let's say we toggle the mentions secondary filter + act(() => { + vm.current.activateSecondaryFilter(SecondaryFilters.MentionsOnly); + }); + + // Primary filer should have been unapplied + expect(vm.current.primaryFilters[i].active).toEqual(false); + + // RLS call must include only the secondary filter + expect(fn).toHaveBeenLastCalledWith(expect.arrayContaining([FilterKey.MentionsFilter])); + }); + const testcases: Array<[string, { secondary: SecondaryFilters; filterKey: FilterKey }, string]> = [ [ "Mentions only",