1
1
import React from 'react' ;
2
2
import type { ComponentProps } from 'react' ;
3
- import { cleanup , render , screen } from '@testing-library/react' ;
3
+ import { cleanup , render , screen , within } from '@testing-library/react' ;
4
4
import userEvent from '@testing-library/user-event' ;
5
5
import { expect } from 'chai' ;
6
6
import sinon from 'sinon' ;
@@ -10,6 +10,7 @@ import { Provider } from '../stores/context';
10
10
import { configureStore } from '../stores/query-bar-store' ;
11
11
import type { QueryBarExtraArgs , RootState } from '../stores/query-bar-store' ;
12
12
import { toggleQueryOptions } from '../stores/query-bar-reducer' ;
13
+ import { AIQueryActionTypes } from '../stores/ai-query-reducer' ;
13
14
import type { PreferencesAccess } from 'compass-preferences-model' ;
14
15
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model' ;
15
16
import { mapQueryToFormFields } from '../utils/query' ;
@@ -51,7 +52,7 @@ describe('QueryBar Component', function () {
51
52
} as QueryBarExtraArgs ) ;
52
53
store . dispatch ( toggleQueryOptions ( expanded ) ) ;
53
54
54
- render (
55
+ const component = (
55
56
< PreferencesProvider value = { preferences } >
56
57
< FavoriteQueryStorageProvider value = { compassFavoriteQueryStorageAccess } >
57
58
< RecentQueryStorageProvider value = { compassRecentQueryStorageAccess } >
@@ -60,15 +61,25 @@ describe('QueryBar Component', function () {
60
61
buttonLabel = "Apply"
61
62
onApply = { noop }
62
63
onReset = { noop }
63
- showExportToLanguageButton
64
64
resultId = "123"
65
+ showExportToLanguageButton
65
66
{ ...props }
66
67
/>
67
68
</ Provider >
68
69
</ RecentQueryStorageProvider >
69
70
</ FavoriteQueryStorageProvider >
70
71
</ PreferencesProvider >
71
72
) ;
73
+
74
+ const result = render ( component ) ;
75
+
76
+ return {
77
+ ...result ,
78
+ store,
79
+ rerender : ( ) => {
80
+ result . rerender ( component ) ;
81
+ } ,
82
+ } ;
72
83
} ;
73
84
74
85
beforeEach ( async function ( ) {
@@ -126,23 +137,158 @@ describe('QueryBar Component', function () {
126
137
} ) ;
127
138
} ) ;
128
139
129
- describe ( 'with one query option ' , function ( ) {
140
+ describe ( 'when rendered ' , function ( ) {
130
141
beforeEach ( function ( ) {
131
142
renderQueryBar ( {
132
- queryOptionsLayout : [ 'project' ] ,
133
- expanded : true ,
134
143
onApply : onApplySpy ,
135
144
onReset : onResetSpy ,
136
145
} ) ;
137
146
} ) ;
138
147
139
- it ( 'renders the expanded inputs' , function ( ) {
148
+ it ( 'renders the filter input' , function ( ) {
149
+ const filterInput = screen . getByTestId ( 'query-bar-option-filter-input' ) ;
150
+ expect ( filterInput ) . to . exist ;
151
+ expect ( filterInput ) . to . have . attribute (
152
+ 'id' ,
153
+ 'query-bar-option-input-filter'
154
+ ) ;
155
+
140
156
const queryInputs = screen . getAllByRole ( 'textbox' ) ;
141
- expect ( queryInputs . length ) . to . equal ( 2 ) ;
157
+ expect ( queryInputs . length ) . to . equal ( 1 ) ;
158
+ } ) ;
159
+
160
+ it ( 'renders the query history button' , function ( ) {
161
+ const queryHistoryButton = screen . queryByTestId ( queryHistoryButtonId ) ;
162
+ expect ( queryHistoryButton ) . to . be . visible ;
163
+ } ) ;
164
+
165
+ it ( 'does not render the query history popover' , function ( ) {
166
+ const queryHistory = screen . queryByTestId ( queryHistoryComponentTestId ) ;
167
+ expect ( queryHistory ) . to . not . exist ;
168
+ } ) ;
169
+ } ) ;
170
+
171
+ describe ( 'when ai is ready' , function ( ) {
172
+ beforeEach ( function ( ) {
173
+ renderQueryBar (
174
+ {
175
+ queryOptionsLayout : [ 'project' ] ,
176
+ expanded : true ,
177
+ onApply : onApplySpy ,
178
+ onReset : onResetSpy ,
179
+ } ,
180
+ { }
181
+ ) ;
182
+ } ) ;
183
+
184
+ it ( 'query controls are enabled' , function ( ) {
185
+ expect (
186
+ screen
187
+ . getByTestId ( 'query-bar-open-export-to-language-button' )
188
+ . getAttribute ( 'aria-disabled' )
189
+ ) . to . equal ( 'false' ) ;
190
+ expect (
191
+ screen
192
+ . getByTestId ( 'query-bar-apply-filter-button' )
193
+ . getAttribute ( 'aria-disabled' )
194
+ ) . to . equal ( 'false' ) ;
195
+ expect (
196
+ screen
197
+ . getByTestId ( 'query-bar-open-export-to-language-button' )
198
+ . getAttribute ( 'aria-disabled' )
199
+ ) . to . equal ( 'false' ) ;
200
+ expect (
201
+ screen
202
+ . getByTestId ( 'query-bar-open-export-to-language-button' )
203
+ . getAttribute ( 'aria-disabled' )
204
+ ) . to . equal ( 'false' ) ;
205
+ } ) ;
206
+
207
+ it ( 'editors are not readonly' , function ( ) {
208
+ expect (
209
+ within ( screen . getByTestId ( 'query-bar-option-filter-input' ) )
210
+ . getByRole ( 'textbox' )
211
+ . getAttribute ( 'aria-readonly' )
212
+ ) . to . not . exist ;
213
+ expect (
214
+ within ( screen . getByTestId ( 'query-bar-option-project-input' ) )
215
+ . getByRole ( 'textbox' )
216
+ . getAttribute ( 'aria-readonly' )
217
+ ) . to . not . exist ;
218
+ } ) ;
219
+ } ) ;
220
+
221
+ describe ( 'while ai is fetching' , function ( ) {
222
+ it ( 'query controls are disabled' , function ( ) {
223
+ const { store, rerender } = renderQueryBar (
224
+ {
225
+ queryOptionsLayout : [ 'project' ] ,
226
+ expanded : true ,
227
+ onApply : onApplySpy ,
228
+ onReset : onResetSpy ,
229
+ } ,
230
+ { }
231
+ ) ;
232
+
233
+ store . dispatch ( {
234
+ type : AIQueryActionTypes . AIQueryStarted ,
235
+ requestId : 'pineapples' ,
236
+ } ) ;
237
+ rerender ( ) ;
238
+
239
+ expect (
240
+ screen
241
+ . getByTestId ( 'query-bar-open-export-to-language-button' )
242
+ . getAttribute ( 'aria-disabled' )
243
+ ) . to . equal ( 'true' ) ;
244
+ expect (
245
+ screen
246
+ . getByTestId ( 'query-bar-apply-filter-button' )
247
+ . getAttribute ( 'aria-disabled' )
248
+ ) . to . equal ( 'true' ) ;
249
+ expect (
250
+ screen
251
+ . getByTestId ( 'query-bar-open-export-to-language-button' )
252
+ . getAttribute ( 'aria-disabled' )
253
+ ) . to . equal ( 'true' ) ;
254
+ expect (
255
+ screen
256
+ . getByTestId ( 'query-bar-open-export-to-language-button' )
257
+ . getAttribute ( 'aria-disabled' )
258
+ ) . to . equal ( 'true' ) ;
259
+ } ) ;
260
+
261
+ it ( 'editors are readonly' , function ( ) {
262
+ const store = configureStore ( { } , {
263
+ preferences,
264
+ logger : createNoopLoggerAndTelemetry ( ) ,
265
+ } as QueryBarExtraArgs ) ;
266
+
267
+ store . dispatch ( {
268
+ type : AIQueryActionTypes . AIQueryStarted ,
269
+ requestId : 'pineapples' ,
270
+ } ) ;
271
+
272
+ render (
273
+ < Provider store = { store } >
274
+ < QueryBar
275
+ buttonLabel = "Apply"
276
+ onApply = { noop }
277
+ onReset = { noop }
278
+ resultId = "123"
279
+ />
280
+ </ Provider >
281
+ ) ;
282
+
283
+ expect (
284
+ within ( screen . getByTestId ( 'query-bar-option-filter-input' ) )
285
+ . getByRole ( 'textbox' )
286
+ . getAttribute ( 'aria-readonly' )
287
+ ) . to . equal ( 'true' ) ;
142
288
} ) ;
143
289
} ) ;
144
290
145
- describe ( 'with ai enabled' , function ( ) {
291
+ describe ( 'with ai is enabled' , function ( ) {
146
292
beforeEach ( async function ( ) {
147
293
await preferences . savePreferences ( {
148
294
enableGenAIFeatures : true ,
0 commit comments