1- import { render , fireEvent } from '@testing-library/react' ;
1+ import { render } from '@testing-library/react' ;
22import '@testing-library/jest-dom' ;
33import DataViewTextFilter , { DataViewTextFilterProps } from './DataViewTextFilter' ;
44import DataViewToolbar from '../DataViewToolbar' ;
@@ -23,7 +23,7 @@ describe('DataViewTextFilter component', () => {
2323 } ) ;
2424
2525 it ( 'should focus the search input when "/" key is pressed and filter is visible' , ( ) => {
26- const { container } = render ( < DataViewToolbar
26+ render ( < DataViewToolbar
2727 filters = {
2828 < DataViewTextFilter { ...defaultProps } showToolbarItem = { true } />
2929 }
@@ -46,7 +46,7 @@ describe('DataViewTextFilter component', () => {
4646 } ) ;
4747
4848 it ( 'should not focus the search input when "/" key is pressed if filter is not visible' , ( ) => {
49- const { container } = render ( < DataViewToolbar
49+ render ( < DataViewToolbar
5050 filters = {
5151 < DataViewTextFilter { ...defaultProps } showToolbarItem = { false } />
5252 }
@@ -81,7 +81,6 @@ describe('DataViewTextFilter component', () => {
8181 ) ;
8282
8383 const otherInput = container . querySelector ( '[data-testid="other-input"]' ) as HTMLInputElement ;
84- const searchInput = document . getElementById ( 'test-filter' ) as HTMLInputElement ;
8584
8685 // Focus the other input first
8786 otherInput . focus ( ) ;
@@ -104,4 +103,50 @@ describe('DataViewTextFilter component', () => {
104103 // The search input should not be focused since we're already in an input field
105104 expect ( document . activeElement ) . toBe ( otherInput ) ;
106105 } ) ;
106+
107+ it ( 'should not focus the search input when enableShortcut is false' , ( ) => {
108+ render ( < DataViewToolbar
109+ filters = {
110+ < DataViewTextFilter { ...defaultProps } showToolbarItem = { true } enableShortcut = { false } />
111+ }
112+ /> ) ;
113+
114+ const input = document . getElementById ( 'test-filter' ) as HTMLInputElement ;
115+ expect ( input ) . toBeInTheDocument ( ) ;
116+
117+ // Simulate pressing "/" key
118+ const keyEvent = new KeyboardEvent ( 'keydown' , {
119+ key : '/' ,
120+ code : 'Slash' ,
121+ bubbles : true ,
122+ cancelable : true ,
123+ } ) ;
124+ window . dispatchEvent ( keyEvent ) ;
125+
126+ // The input should not be focused since the shortcut is disabled
127+ expect ( document . activeElement ) . not . toBe ( input ) ;
128+ } ) ;
129+
130+ it ( 'should focus the search input when enableShortcut is true (default)' , ( ) => {
131+ render ( < DataViewToolbar
132+ filters = {
133+ < DataViewTextFilter { ...defaultProps } showToolbarItem = { true } />
134+ }
135+ /> ) ;
136+
137+ const input = document . getElementById ( 'test-filter' ) as HTMLInputElement ;
138+ expect ( input ) . toBeInTheDocument ( ) ;
139+
140+ // Simulate pressing "/" key
141+ const keyEvent = new KeyboardEvent ( 'keydown' , {
142+ key : '/' ,
143+ code : 'Slash' ,
144+ bubbles : true ,
145+ cancelable : true ,
146+ } ) ;
147+ window . dispatchEvent ( keyEvent ) ;
148+
149+ // The input should be focused since the shortcut is enabled by default
150+ expect ( document . activeElement ) . toBe ( input ) ;
151+ } ) ;
107152} ) ;
0 commit comments