File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
static/app/components/searchQueryBuilder Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -520,6 +520,35 @@ describe('SearchQueryBuilder', function () {
520
520
) . toBeInTheDocument ( ) ;
521
521
} ) ;
522
522
523
+ it ( 'switches to keys menu when recent searches no longer exist' , async function ( ) {
524
+ const { rerender} = render (
525
+ < SearchQueryBuilder
526
+ { ...defaultProps }
527
+ recentSearches = { SavedSearchType . ISSUE }
528
+ initialQuery = ""
529
+ />
530
+ ) ;
531
+
532
+ await userEvent . click ( getLastInput ( ) ) ;
533
+
534
+ // Recent should be selected
535
+ expect ( screen . getByRole ( 'button' , { name : 'Recent' } ) ) . toHaveAttribute (
536
+ 'aria-selected' ,
537
+ 'true'
538
+ ) ;
539
+
540
+ // Rerender without recent searches
541
+ rerender ( < SearchQueryBuilder { ...defaultProps } /> ) ;
542
+
543
+ // Recent should not exist anymore
544
+ expect ( screen . queryByRole ( 'button' , { name : 'Recent' } ) ) . not . toBeInTheDocument ( ) ;
545
+ // All should be selected
546
+ expect ( screen . getByRole ( 'button' , { name : 'All' } ) ) . toHaveAttribute (
547
+ 'aria-selected' ,
548
+ 'true'
549
+ ) ;
550
+ } ) ;
551
+
523
552
it ( 'when selecting a recent search, should reset query and call onSearch' , async function ( ) {
524
553
const mockOnSearch = jest . fn ( ) ;
525
554
const mockCreateRecentSearch = MockApiClient . addMockResponse ( {
Original file line number Diff line number Diff line change @@ -178,6 +178,28 @@ function useHighlightFirstOptionOnSectionChange({
178
178
] ) ;
179
179
}
180
180
181
+ // If the selected section no longer exists, switch to the first valid section
182
+ function useSwitchToValidSection ( {
183
+ sections,
184
+ selectedSection,
185
+ setSelectedSection,
186
+ } : {
187
+ sections : Section [ ] ;
188
+ selectedSection : Key | null ;
189
+ setSelectedSection : ( section : string ) => void ;
190
+ } ) {
191
+ useEffect ( ( ) => {
192
+ if ( ! selectedSection || ! sections . length ) {
193
+ return ;
194
+ }
195
+
196
+ const section = sections . find ( s => s . value === selectedSection ) ;
197
+ if ( ! section ) {
198
+ setSelectedSection ( sections [ 0 ] . value ) ;
199
+ }
200
+ } , [ sections , selectedSection , setSelectedSection ] ) ;
201
+ }
202
+
181
203
function FilterKeyMenuContent < T extends SelectOptionOrSectionWithKey < string > > ( {
182
204
recentFilters,
183
205
selectedSection,
@@ -287,6 +309,8 @@ export function FilterKeyListBox<T extends SelectOptionOrSectionWithKey<string>>
287
309
isOpen,
288
310
} ) ;
289
311
312
+ useSwitchToValidSection ( { sections, selectedSection, setSelectedSection} ) ;
313
+
290
314
const fullWidth = ! query ;
291
315
const showDetailsPane = fullWidth && selectedSection !== RECENT_SEARCH_CATEGORY_VALUE ;
292
316
You can’t perform that action at this time.
0 commit comments