7
7
waitFor ,
8
8
userEvent ,
9
9
} from '@mongodb-js/testing-library-compass' ;
10
- import { OptionEditor } from './option-editor' ;
10
+ import { OptionEditor , getOptionBasedQueries } from './option-editor' ;
11
11
import type { SinonSpy } from 'sinon' ;
12
12
import { applyFromHistory } from '../stores/query-bar-reducer' ;
13
13
import sinon from 'sinon' ;
@@ -47,7 +47,8 @@ describe('OptionEditor', function () {
47
47
insertEmptyDocOnFocus
48
48
onChange = { ( ) => { } }
49
49
value = ""
50
- savedQueries = { [ ] }
50
+ recentQueries = { [ ] }
51
+ favoriteQueries = { [ ] }
51
52
onApplyQuery = { applyFromHistory }
52
53
> </ OptionEditor >
53
54
) ;
@@ -69,7 +70,8 @@ describe('OptionEditor', function () {
69
70
insertEmptyDocOnFocus
70
71
onChange = { ( ) => { } }
71
72
value = "{ foo: 1 }"
72
- savedQueries = { [ ] }
73
+ recentQueries = { [ ] }
74
+ favoriteQueries = { [ ] }
73
75
onApplyQuery = { applyFromHistory }
74
76
> </ OptionEditor >
75
77
) ;
@@ -91,7 +93,8 @@ describe('OptionEditor', function () {
91
93
insertEmptyDocOnFocus
92
94
onChange = { ( ) => { } }
93
95
value = ""
94
- savedQueries = { [ ] }
96
+ favoriteQueries = { [ ] }
97
+ recentQueries = { [ ] }
95
98
onApplyQuery = { applyFromHistory }
96
99
> </ OptionEditor >
97
100
) ;
@@ -119,7 +122,8 @@ describe('OptionEditor', function () {
119
122
insertEmptyDocOnFocus
120
123
onChange = { ( ) => { } }
121
124
value = ""
122
- savedQueries = { [ ] }
125
+ favoriteQueries = { [ ] }
126
+ recentQueries = { [ ] }
123
127
onApplyQuery = { applyFromHistory }
124
128
> </ OptionEditor >
125
129
) ;
@@ -149,7 +153,8 @@ describe('OptionEditor', function () {
149
153
insertEmptyDocOnFocus
150
154
onChange = { ( ) => { } }
151
155
value = ""
152
- savedQueries = { [ ] }
156
+ favoriteQueries = { [ ] }
157
+ recentQueries = { [ ] }
153
158
onApplyQuery = { applyFromHistory }
154
159
> </ OptionEditor >
155
160
) ;
@@ -167,7 +172,7 @@ describe('OptionEditor', function () {
167
172
} ) ;
168
173
} ) ;
169
174
170
- describe ( 'when render filter bar with the query history autocompleter ' , function ( ) {
175
+ describe ( 'when rendering filter option ' , function ( ) {
171
176
let onApplySpy : SinonSpy ;
172
177
let preferencesAccess : PreferencesAccess ;
173
178
@@ -182,64 +187,54 @@ describe('OptionEditor', function () {
182
187
insertEmptyDocOnFocus
183
188
onChange = { ( ) => { } }
184
189
value = ""
185
- savedQueries = { [
190
+ recentQueries = { [
186
191
{
187
- type : 'recent' ,
188
- lastExecuted : new Date ( ) ,
189
- queryProperties : {
190
- filter : { a : 1 } ,
191
- } ,
192
- } ,
193
- {
194
- type : 'favorite' ,
195
- lastExecuted : new Date ( ) ,
196
- queryProperties : {
197
- filter : { a : 2 } ,
198
- sort : { a : - 1 } ,
199
- } ,
192
+ _lastExecuted : new Date ( ) ,
193
+ filter : { a : 1 } ,
200
194
} ,
195
+ ] }
196
+ favoriteQueries = { [
201
197
{
202
- type : 'recent' ,
203
- lastExecuted : new Date ( ) ,
204
- queryProperties : {
205
- filter : { a : 2 } ,
206
- sort : { a : - 1 } ,
207
- update : { a : 10 } ,
208
- } ,
198
+ _lastExecuted : new Date ( ) ,
199
+ filter : { a : 2 } ,
200
+ sort : { a : - 1 } ,
209
201
} ,
210
202
] }
211
203
onApplyQuery = { onApplySpy }
212
204
/>
213
205
</ PreferencesProvider >
214
206
) ;
207
+ userEvent . click ( screen . getByRole ( 'textbox' ) ) ;
208
+ await waitFor ( ( ) => {
209
+ screen . getByLabelText ( 'Completions' ) ;
210
+ } ) ;
215
211
} ) ;
216
212
217
213
afterEach ( function ( ) {
218
214
cleanup ( ) ;
219
215
} ) ;
220
216
221
- it ( 'filter applied correctly when autocomplete option is clicked' , async function ( ) {
222
- userEvent . click ( screen . getByRole ( 'textbox' ) ) ;
223
- await waitFor ( ( ) => {
224
- expect ( screen . getAllByText ( '{ a: 1 }' ) [ 0 ] ) . to . be . visible ;
225
- expect ( screen . getByText ( '{ a: 2 }, sort: { a: -1 }' ) ) . to . be . visible ;
226
- expect (
227
- screen . queryByText ( '{ a: 2 }, sort: { a: -1 }, update: { a: 10 }' )
228
- ) . to . be . null ;
229
- } ) ;
217
+ it ( 'renders autocomplete options' , function ( ) {
218
+ expect ( screen . getAllByText ( '{ a: 1 }' ) [ 0 ] ) . to . be . visible ;
219
+ expect ( screen . getByText ( '{ a: 2 }, sort: { a: -1 }' ) ) . to . be . visible ;
220
+ } ) ;
230
221
222
+ it ( 'calls onApply with correct params' , async function ( ) {
231
223
// Simulate selecting the autocomplete option
232
224
userEvent . click ( screen . getByText ( '{ a: 2 }, sort: { a: -1 }' ) ) ;
233
225
await waitFor ( ( ) => {
234
- expect ( onApplySpy . lastCall ) . to . be . calledWithExactly ( {
235
- filter : { a : 2 } ,
236
- sort : { a : - 1 } ,
237
- } ) ;
226
+ expect ( onApplySpy . lastCall ) . to . be . calledWithExactly (
227
+ {
228
+ filter : { a : 2 } ,
229
+ sort : { a : - 1 } ,
230
+ } ,
231
+ [ ]
232
+ ) ;
238
233
} ) ;
239
234
} ) ;
240
235
} ) ;
241
236
242
- describe ( 'when render project bar with the query history autocompleter ' , function ( ) {
237
+ describe ( 'when rendering project option ' , function ( ) {
243
238
let onApplySpy : SinonSpy ;
244
239
let preferencesAccess : PreferencesAccess ;
245
240
@@ -254,62 +249,124 @@ describe('OptionEditor', function () {
254
249
insertEmptyDocOnFocus
255
250
onChange = { ( ) => { } }
256
251
value = ""
257
- savedQueries = { [
258
- {
259
- type : 'favorite' ,
260
- lastExecuted : new Date ( ) ,
261
- queryProperties : {
262
- project : { a : 1 } ,
263
- } ,
264
- } ,
252
+ favoriteQueries = { [
265
253
{
266
- type : 'favorite' ,
267
- lastExecuted : new Date ( ) ,
268
- queryProperties : {
269
- filter : { a : 2 } ,
270
- sort : { a : - 1 } ,
271
- } ,
254
+ _lastExecuted : new Date ( ) ,
255
+ project : { a : 1 } ,
272
256
} ,
257
+ ] }
258
+ recentQueries = { [
273
259
{
274
- type : 'recent' ,
275
- lastExecuted : new Date ( ) ,
276
- queryProperties : {
277
- filter : { a : 2 } ,
278
- sort : { a : - 1 } ,
279
- project : { a : 0 } ,
280
- } ,
260
+ _lastExecuted : new Date ( ) ,
261
+ project : { a : 0 } ,
281
262
} ,
282
263
] }
283
264
onApplyQuery = { onApplySpy }
284
265
/>
285
266
</ PreferencesProvider >
286
267
) ;
268
+ userEvent . click ( screen . getByRole ( 'textbox' ) ) ;
269
+ await waitFor ( ( ) => {
270
+ screen . getByLabelText ( 'Completions' ) ;
271
+ } ) ;
287
272
} ) ;
288
273
289
274
afterEach ( function ( ) {
290
275
cleanup ( ) ;
291
276
} ) ;
292
277
293
- it ( 'only queries with project property are shown in project editor' , async function ( ) {
294
- userEvent . click ( screen . getByRole ( 'textbox' ) ) ;
295
- await waitFor ( ( ) => {
296
- expect ( screen . getAllByText ( 'project: { a: 1 }' ) [ 0 ] ) . to . be . visible ;
297
- expect ( screen . queryByText ( '{ a: 2 }, sort: { a: -1 }' ) ) . to . be . null ;
298
- expect ( screen . getByText ( '{ a: 2 }, sort: { a: -1 }, project: { a: 0 }' ) )
299
- . to . be . visible ;
300
- } ) ;
278
+ it ( 'renders autocomplete options' , function ( ) {
279
+ expect ( screen . getAllByText ( 'project: { a: 1 }' ) [ 0 ] ) . to . be . visible ;
280
+ expect ( screen . getAllByText ( 'project: { a: 0 }' ) [ 0 ] ) . to . be . visible ;
281
+ } ) ;
301
282
283
+ it ( 'calls onApply with correct params' , async function ( ) {
302
284
// Simulate selecting the autocomplete option
303
- userEvent . click (
304
- screen . getByText ( '{ a: 2 }, sort: { a: -1 }, project: { a: 0 }' )
305
- ) ;
285
+ userEvent . click ( screen . getByText ( 'project: { a: 0 }' ) ) ;
306
286
await waitFor ( ( ) => {
307
- expect ( onApplySpy . lastCall ) . to . be . calledWithExactly ( {
308
- filter : { a : 2 } ,
309
- sort : { a : - 1 } ,
310
- project : { a : 0 } ,
311
- } ) ;
287
+ expect ( onApplySpy ) . to . have . been . calledOnceWithExactly (
288
+ {
289
+ project : { a : 0 } ,
290
+ } ,
291
+ [ 'filter' , 'collation' , 'sort' , 'hint' , 'skip' , 'limit' , 'maxTimeMS' ]
292
+ ) ;
312
293
} ) ;
313
294
} ) ;
314
295
} ) ;
296
+
297
+ describe ( 'getOptionBasedQueries' , function ( ) {
298
+ const savedQueries = [
299
+ {
300
+ _lastExecuted : new Date ( ) ,
301
+ filter : { a : 1 } ,
302
+ project : { b : 1 } ,
303
+ sort : { c : 1 } ,
304
+ collation : { locale : 'en' } ,
305
+ hint : { a : 1 } ,
306
+ skip : 1 ,
307
+ limit : 1 ,
308
+ } ,
309
+ ] ;
310
+
311
+ it ( 'filters out update queries' , function ( ) {
312
+ const queries = getOptionBasedQueries ( 'filter' , 'recent' , [
313
+ ...savedQueries ,
314
+ { _lastExecuted : new Date ( ) , update : { a : 1 } , filter : { a : 2 } } ,
315
+ ] ) ;
316
+ expect ( queries . length ) . to . equal ( 1 ) ;
317
+ } ) ;
318
+
319
+ it ( 'filters out empty queries' , function ( ) {
320
+ const queries = getOptionBasedQueries ( 'filter' , 'recent' , [
321
+ ...savedQueries ,
322
+ { _lastExecuted : new Date ( ) } ,
323
+ ] ) ;
324
+ expect ( queries . length ) . to . equal ( 1 ) ;
325
+ } ) ;
326
+
327
+ it ( 'filters out duplicate queries' , function ( ) {
328
+ const queries = getOptionBasedQueries ( 'filter' , 'recent' , [
329
+ ...savedQueries ,
330
+ ...savedQueries ,
331
+ ...savedQueries ,
332
+ { _lastExecuted : new Date ( ) } ,
333
+ { _lastExecuted : new Date ( ) } ,
334
+ ] ) ;
335
+ expect ( queries . length ) . to . equal ( 1 ) ;
336
+ } ) ;
337
+
338
+ const optionNames = [
339
+ 'filter' ,
340
+ 'project' ,
341
+ 'sort' ,
342
+ 'collation' ,
343
+ 'hint' ,
344
+ ] as const ;
345
+ for ( const name of optionNames ) {
346
+ it ( `maps query for ${ name } ` , function ( ) {
347
+ const queries = getOptionBasedQueries ( name , 'recent' , savedQueries ) ;
348
+
349
+ // For filter, we include all the query properties and for the rest
350
+ // we only include that specific option.
351
+ const queryProperties =
352
+ name === 'filter'
353
+ ? Object . fromEntries (
354
+ Object . entries ( savedQueries [ 0 ] ) . filter (
355
+ ( [ key ] ) => key !== '_lastExecuted'
356
+ )
357
+ )
358
+ : {
359
+ [ name ] : savedQueries [ 0 ] [ name ] ,
360
+ } ;
361
+
362
+ expect ( queries ) . to . deep . equal ( [
363
+ {
364
+ lastExecuted : savedQueries [ 0 ] . _lastExecuted ,
365
+ queryProperties,
366
+ type : 'recent' ,
367
+ } ,
368
+ ] ) ;
369
+ } ) ;
370
+ }
371
+ } ) ;
315
372
} ) ;
0 commit comments