77 waitFor ,
88 userEvent ,
99} from '@mongodb-js/testing-library-compass' ;
10- import { OptionEditor } from './option-editor' ;
10+ import { OptionEditor , getOptionBasedQueries } from './option-editor' ;
1111import type { SinonSpy } from 'sinon' ;
1212import { applyFromHistory } from '../stores/query-bar-reducer' ;
1313import sinon from 'sinon' ;
@@ -167,7 +167,7 @@ describe('OptionEditor', function () {
167167 } ) ;
168168 } ) ;
169169
170- describe ( 'when render filter bar with the query history autocompleter ' , function ( ) {
170+ describe ( 'when rendering filter option ' , function ( ) {
171171 let onApplySpy : SinonSpy ;
172172 let preferencesAccess : PreferencesAccess ;
173173
@@ -198,48 +198,42 @@ describe('OptionEditor', function () {
198198 sort : { a : - 1 } ,
199199 } ,
200200 } ,
201- {
202- type : 'recent' ,
203- lastExecuted : new Date ( ) ,
204- queryProperties : {
205- filter : { a : 2 } ,
206- sort : { a : - 1 } ,
207- update : { a : 10 } ,
208- } ,
209- } ,
210201 ] }
211202 onApplyQuery = { onApplySpy }
212203 />
213204 </ PreferencesProvider >
214205 ) ;
206+ userEvent . click ( screen . getByRole ( 'textbox' ) ) ;
207+ await waitFor ( ( ) => {
208+ screen . getByLabelText ( 'Completions' ) ;
209+ } ) ;
215210 } ) ;
216211
217212 afterEach ( function ( ) {
218213 cleanup ( ) ;
219214 } ) ;
220215
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- } ) ;
216+ it ( 'renders autocomplete options' , function ( ) {
217+ expect ( screen . getAllByText ( '{ a: 1 }' ) [ 0 ] ) . to . be . visible ;
218+ expect ( screen . getByText ( '{ a: 2 }, sort: { a: -1 }' ) ) . to . be . visible ;
219+ } ) ;
230220
221+ it ( 'calls onApply with correct params' , async function ( ) {
231222 // Simulate selecting the autocomplete option
232223 userEvent . click ( screen . getByText ( '{ a: 2 }, sort: { a: -1 }' ) ) ;
233224 await waitFor ( ( ) => {
234- expect ( onApplySpy . lastCall ) . to . be . calledWithExactly ( {
235- filter : { a : 2 } ,
236- sort : { a : - 1 } ,
237- } ) ;
225+ expect ( onApplySpy . lastCall ) . to . be . calledWithExactly (
226+ {
227+ filter : { a : 2 } ,
228+ sort : { a : - 1 } ,
229+ } ,
230+ [ ]
231+ ) ;
238232 } ) ;
239233 } ) ;
240234 } ) ;
241235
242- describe ( 'when render project bar with the query history autocompleter ' , function ( ) {
236+ describe ( 'when rendering project option ' , function ( ) {
243237 let onApplySpy : SinonSpy ;
244238 let preferencesAccess : PreferencesAccess ;
245239
@@ -262,20 +256,10 @@ describe('OptionEditor', function () {
262256 project : { a : 1 } ,
263257 } ,
264258 } ,
265- {
266- type : 'favorite' ,
267- lastExecuted : new Date ( ) ,
268- queryProperties : {
269- filter : { a : 2 } ,
270- sort : { a : - 1 } ,
271- } ,
272- } ,
273259 {
274260 type : 'recent' ,
275261 lastExecuted : new Date ( ) ,
276262 queryProperties : {
277- filter : { a : 2 } ,
278- sort : { a : - 1 } ,
279263 project : { a : 0 } ,
280264 } ,
281265 } ,
@@ -284,32 +268,108 @@ describe('OptionEditor', function () {
284268 />
285269 </ PreferencesProvider >
286270 ) ;
271+ userEvent . click ( screen . getByRole ( 'textbox' ) ) ;
272+ await waitFor ( ( ) => {
273+ screen . getByLabelText ( 'Completions' ) ;
274+ } ) ;
287275 } ) ;
288276
289277 afterEach ( function ( ) {
290278 cleanup ( ) ;
291279 } ) ;
292280
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- } ) ;
281+ it ( 'renders autocomplete options' , function ( ) {
282+ expect ( screen . getAllByText ( 'project: { a: 1 }' ) [ 0 ] ) . to . be . visible ;
283+ expect ( screen . getAllByText ( 'project: { a: 0 }' ) [ 0 ] ) . to . be . visible ;
284+ } ) ;
301285
286+ it ( 'calls onApply with correct params' , async function ( ) {
302287 // Simulate selecting the autocomplete option
303- userEvent . click (
304- screen . getByText ( '{ a: 2 }, sort: { a: -1 }, project: { a: 0 }' )
305- ) ;
288+ userEvent . click ( screen . getByText ( 'project: { a: 0 }' ) ) ;
306289 await waitFor ( ( ) => {
307- expect ( onApplySpy . lastCall ) . to . be . calledWithExactly ( {
308- filter : { a : 2 } ,
309- sort : { a : - 1 } ,
310- project : { a : 0 } ,
311- } ) ;
290+ expect ( onApplySpy ) . to . have . been . calledOnceWithExactly (
291+ {
292+ project : { a : 0 } ,
293+ } ,
294+ [ 'filter' , 'collation' , 'sort' , 'hint' , 'skip' , 'limit' , 'maxTimeMS' ]
295+ ) ;
312296 } ) ;
313297 } ) ;
314298 } ) ;
299+
300+ describe ( 'getOptionBasedQueries' , function ( ) {
301+ const savedQueries = [
302+ {
303+ _lastExecuted : new Date ( ) ,
304+ filter : { a : 1 } ,
305+ project : { b : 1 } ,
306+ sort : { c : 1 } ,
307+ collation : { locale : 'en' } ,
308+ hint : { a : 1 } ,
309+ skip : 1 ,
310+ limit : 1 ,
311+ } ,
312+ ] ;
313+
314+ it ( 'filters out update queries' , function ( ) {
315+ const queries = getOptionBasedQueries ( 'filter' , 'recent' , [
316+ ...savedQueries ,
317+ { _lastExecuted : new Date ( ) , update : { a : 1 } , filter : { a : 2 } } ,
318+ ] ) ;
319+ expect ( queries . length ) . to . equal ( 1 ) ;
320+ } ) ;
321+
322+ it ( 'filters out empty queries' , function ( ) {
323+ const queries = getOptionBasedQueries ( 'filter' , 'recent' , [
324+ ...savedQueries ,
325+ { _lastExecuted : new Date ( ) } ,
326+ ] ) ;
327+ expect ( queries . length ) . to . equal ( 1 ) ;
328+ } ) ;
329+
330+ it ( 'filters out duplicate queries' , function ( ) {
331+ const queries = getOptionBasedQueries ( 'filter' , 'recent' , [
332+ ...savedQueries ,
333+ ...savedQueries ,
334+ ...savedQueries ,
335+ { _lastExecuted : new Date ( ) } ,
336+ { _lastExecuted : new Date ( ) } ,
337+ ] ) ;
338+ expect ( queries . length ) . to . equal ( 1 ) ;
339+ } ) ;
340+
341+ const optionNames = [
342+ 'filter' ,
343+ 'project' ,
344+ 'sort' ,
345+ 'collation' ,
346+ 'hint' ,
347+ ] as const ;
348+ for ( const name of optionNames ) {
349+ it ( `maps query for ${ name } ` , function ( ) {
350+ const queries = getOptionBasedQueries ( name , 'recent' , savedQueries ) ;
351+
352+ // For filter, we include all the query properties and for the rest
353+ // we only include that specific option.
354+ const queryProperties =
355+ name === 'filter'
356+ ? Object . fromEntries (
357+ Object . entries ( savedQueries [ 0 ] ) . filter (
358+ ( [ key ] ) => key !== '_lastExecuted'
359+ )
360+ )
361+ : {
362+ [ name ] : savedQueries [ 0 ] [ name ] ,
363+ } ;
364+
365+ expect ( queries ) . to . deep . equal ( [
366+ {
367+ type : 'recent' ,
368+ lastExecuted : savedQueries [ 0 ] . _lastExecuted ,
369+ queryProperties,
370+ } ,
371+ ] ) ;
372+ } ) ;
373+ }
374+ } ) ;
315375} ) ;
0 commit comments