11import { render } from '@testing-library/react' ;
2+ import '@testing-library/jest-dom' ;
23import DataViewTextFilter , { DataViewTextFilterProps } from './DataViewTextFilter' ;
34import DataViewToolbar from '../DataViewToolbar' ;
45
@@ -20,4 +21,132 @@ describe('DataViewTextFilter component', () => {
2021 /> ) ;
2122 expect ( container ) . toMatchSnapshot ( ) ;
2223 } ) ;
24+
25+ it ( 'should focus the search input when "/" key is pressed and filter is visible' , ( ) => {
26+ render ( < DataViewToolbar
27+ filters = {
28+ < DataViewTextFilter { ...defaultProps } showToolbarItem = { true } />
29+ }
30+ /> ) ;
31+
32+ const input = document . getElementById ( 'test-filter' ) as HTMLInputElement ;
33+ expect ( input ) . toBeInTheDocument ( ) ;
34+
35+ // Simulate pressing "/" key by creating and dispatching a KeyboardEvent
36+ const keyEvent = new KeyboardEvent ( 'keydown' , {
37+ key : '/' ,
38+ code : 'Slash' ,
39+ bubbles : true ,
40+ cancelable : true ,
41+ } ) ;
42+ window . dispatchEvent ( keyEvent ) ;
43+
44+ // Check that the input has focus
45+ expect ( document . activeElement ) . toBe ( input ) ;
46+ } ) ;
47+
48+ it ( 'should not focus the search input when "/" key is pressed if filter is not visible' , ( ) => {
49+ render ( < DataViewToolbar
50+ filters = {
51+ < DataViewTextFilter { ...defaultProps } showToolbarItem = { false } />
52+ }
53+ /> ) ;
54+
55+ const input = document . getElementById ( 'test-filter' ) as HTMLInputElement ;
56+
57+ // Simulate pressing "/" key
58+ const keyEvent = new KeyboardEvent ( 'keydown' , {
59+ key : '/' ,
60+ code : 'Slash' ,
61+ bubbles : true ,
62+ cancelable : true ,
63+ } ) ;
64+ window . dispatchEvent ( keyEvent ) ;
65+
66+ if ( input ) {
67+ expect ( document . activeElement ) . not . toBe ( input ) ;
68+ }
69+ } ) ;
70+
71+ it ( 'should not focus the search input when "/" key is pressed while typing in another input' , ( ) => {
72+ const { container } = render (
73+ < div >
74+ < input data-testid = "other-input" />
75+ < DataViewToolbar
76+ filters = {
77+ < DataViewTextFilter { ...defaultProps } showToolbarItem = { true } />
78+ }
79+ />
80+ </ div >
81+ ) ;
82+
83+ const otherInput = container . querySelector ( '[data-testid="other-input"]' ) as HTMLInputElement ;
84+
85+ // Focus the other input first
86+ otherInput . focus ( ) ;
87+ expect ( document . activeElement ) . toBe ( otherInput ) ;
88+
89+ // Simulate pressing "/" key while focused on the other input
90+ // The event target should be the input element
91+ const keyEvent = new KeyboardEvent ( 'keydown' , {
92+ key : '/' ,
93+ code : 'Slash' ,
94+ bubbles : true ,
95+ cancelable : true ,
96+ } ) ;
97+ Object . defineProperty ( keyEvent , 'target' , {
98+ value : otherInput ,
99+ enumerable : true ,
100+ } ) ;
101+ window . dispatchEvent ( keyEvent ) ;
102+
103+ // The search input should not be focused since we're already in an input field
104+ expect ( document . activeElement ) . toBe ( otherInput ) ;
105+ } ) ;
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+ } ) ;
23152} ) ;
0 commit comments