@@ -48,6 +48,28 @@ describe('AdHocFiltersRecommendations', () => {
4848 } ) ;
4949 } ) ;
5050
51+ it ( 'should deduplicate recentFilters loaded from browser storage' , async ( ) => {
52+ const duplicatedFilters = [
53+ { key : 'pod' , operator : '=' , value : 'abc' } ,
54+ { key : 'cluster' , operator : '=' , value : 'us-east' } ,
55+ { key : 'pod' , operator : '=' , value : 'abc' } ,
56+ ] ;
57+ localStorage . setItem ( RECENT_FILTERS_KEY , JSON . stringify ( duplicatedFilters ) ) ;
58+
59+ const { filtersVar } = setup ( {
60+ drilldownRecommendationsEnabled : true ,
61+ } ) ;
62+
63+ await waitFor ( ( ) => {
64+ const recommendations = filtersVar . getRecommendations ( ) ;
65+ expect ( recommendations ) . toBeDefined ( ) ;
66+ expect ( recommendations ?. state . recentFilters ) . toEqual ( [
67+ { key : 'cluster' , operator : '=' , value : 'us-east' } ,
68+ { key : 'pod' , operator : '=' , value : 'abc' } ,
69+ ] ) ;
70+ } ) ;
71+ } ) ;
72+
5173 it ( 'should set empty recentFilters when browser storage is empty' , async ( ) => {
5274 const { filtersVar } = setup ( {
5375 drilldownRecommendationsEnabled : true ,
@@ -113,6 +135,59 @@ describe('AdHocFiltersRecommendations', () => {
113135 expect ( JSON . parse ( storedFilters ! ) ) . toHaveLength ( MAX_STORED_RECENT_DRILLDOWNS ) ;
114136 } ) ;
115137
138+ it ( 'should deduplicate when the same filter is stored multiple times' , async ( ) => {
139+ const { filtersVar } = setup ( {
140+ drilldownRecommendationsEnabled : true ,
141+ filters : [ { key : 'cluster' , value : 'us-east' , operator : '=' } ] ,
142+ } ) ;
143+
144+ let recommendations : AdHocFiltersRecommendations | undefined ;
145+ await waitFor ( ( ) => {
146+ recommendations = filtersVar . getRecommendations ( ) ;
147+ expect ( recommendations ) . toBeDefined ( ) ;
148+ } ) ;
149+
150+ const filter : AdHocFilterWithLabels = { key : 'cluster' , value : 'us-east' , operator : '=' } ;
151+
152+ act ( ( ) => {
153+ recommendations ! . storeRecentFilter ( filter ) ;
154+ recommendations ! . storeRecentFilter ( filter ) ;
155+ recommendations ! . storeRecentFilter ( filter ) ;
156+ } ) ;
157+
158+ const storedFilters = JSON . parse ( localStorage . getItem ( RECENT_FILTERS_KEY ) ! ) ;
159+ expect ( storedFilters ) . toHaveLength ( 1 ) ;
160+ expect ( storedFilters [ 0 ] ) . toEqual ( filter ) ;
161+ } ) ;
162+
163+ it ( 'should move re-added filter to the most recent position and not duplicate' , async ( ) => {
164+ const { filtersVar } = setup ( {
165+ drilldownRecommendationsEnabled : true ,
166+ filters : [ { key : 'cluster' , value : '1' , operator : '=' } ] ,
167+ } ) ;
168+
169+ let recommendations : AdHocFiltersRecommendations | undefined ;
170+ await waitFor ( ( ) => {
171+ recommendations = filtersVar . getRecommendations ( ) ;
172+ expect ( recommendations ) . toBeDefined ( ) ;
173+ } ) ;
174+
175+ const filterA : AdHocFilterWithLabels = { key : 'cluster' , value : 'a' , operator : '=' } ;
176+ const filterB : AdHocFilterWithLabels = { key : 'cluster' , value : 'b' , operator : '=' } ;
177+ const filterC : AdHocFilterWithLabels = { key : 'cluster' , value : 'c' , operator : '=' } ;
178+
179+ act ( ( ) => {
180+ recommendations ! . storeRecentFilter ( filterA ) ;
181+ recommendations ! . storeRecentFilter ( filterB ) ;
182+ recommendations ! . storeRecentFilter ( filterC ) ;
183+ recommendations ! . storeRecentFilter ( filterA ) ;
184+ } ) ;
185+
186+ const storedFilters = JSON . parse ( localStorage . getItem ( RECENT_FILTERS_KEY ) ! ) ;
187+ expect ( storedFilters ) . toHaveLength ( 3 ) ;
188+ expect ( storedFilters ) . toEqual ( [ filterB , filterC , filterA ] ) ;
189+ } ) ;
190+
116191 it ( 'should update state with limited recent filters for display' , async ( ) => {
117192 const { filtersVar } = setup ( {
118193 drilldownRecommendationsEnabled : true ,
0 commit comments