@@ -2,7 +2,13 @@ import { expect } from 'chai';
22import { createQueryWithHistoryAutocompleter } from './query-autocompleter-with-history' ;
33import { setupCodemirrorCompleter } from '../../test/completer' ;
44import type { SavedQuery } from '../../dist/codemirror/query-history-autocompleter' ;
5- import { createQuery } from './query-history-autocompleter' ;
5+ import type { Completion } from '@codemirror/autocomplete' ;
6+
7+ function getQueryHistoryAutocompletions ( completions : Readonly < Completion [ ] > ) {
8+ return completions . filter (
9+ ( { type } ) => type === 'favorite' || type === 'query-history'
10+ ) ;
11+ }
612
713describe ( 'query history autocompleter' , function ( ) {
814 const { getCompletions, cleanup } = setupCodemirrorCompleter (
@@ -46,7 +52,7 @@ describe('query history autocompleter', function () {
4652 type : 'recent' ,
4753 lastExecuted : new Date ( '2023-06-04T18:00:00Z' ) ,
4854 queryProperties : {
49- filter : { isActive : true } ,
55+ filter : { isActive : true , score : { $exists : false } } ,
5056 project : { userId : 1 , isActive : 1 } ,
5157 collation : { locale : 'simple' } ,
5258 sort : { userId : 1 } ,
@@ -71,32 +77,105 @@ describe('query history autocompleter', function () {
7177
7278 const mockOnApply : ( query : SavedQuery [ 'queryProperties' ] ) => any = ( ) => { } ;
7379
74- it ( 'returns all saved queries as completions on click ' , async function ( ) {
80+ it ( 'returns all saved queries as completions with default {} ' , async function ( ) {
7581 expect (
76- await getCompletions ( '{}' , savedQueries , undefined , mockOnApply , 'light' )
82+ await getCompletions ( '{}' , {
83+ savedQueries,
84+ options : undefined ,
85+ queryProperty : '' ,
86+ onApply : mockOnApply ,
87+ theme : 'light' ,
88+ } )
7789 ) . to . have . lengthOf ( 5 ) ;
7890 } ) ;
7991
80- it ( 'returns combined completions when user starts typing ' , async function ( ) {
92+ it ( 'returns all saved queries as completions with empty entry ' , async function ( ) {
8193 expect (
82- await getCompletions ( 'foo' , savedQueries , undefined , mockOnApply , 'light' )
83- ) . to . have . lengthOf ( 50 ) ;
94+ await getCompletions ( '' , {
95+ savedQueries,
96+ options : undefined ,
97+ queryProperty : '' ,
98+ onApply : mockOnApply ,
99+ theme : 'light' ,
100+ } )
101+ ) . to . have . lengthOf ( 5 ) ;
102+ } ) ;
103+
104+ it ( 'returns combined completions that match the prefix of the field' , async function ( ) {
105+ const completions = await getCompletions ( 'scor' , {
106+ savedQueries,
107+ options : undefined ,
108+ queryProperty : 'filter' ,
109+ onApply : mockOnApply ,
110+ theme : 'light' ,
111+ } ) ;
112+ const queryHistoryCompletions = getQueryHistoryAutocompletions ( completions ) ;
113+
114+ expect ( queryHistoryCompletions ) . to . have . lengthOf ( 2 ) ;
115+ expect ( completions ) . to . have . length . greaterThan (
116+ queryHistoryCompletions . length
117+ ) ;
118+ } ) ;
119+
120+ it ( 'returns completions that match with multiple fields' , async function ( ) {
121+ const completions = getQueryHistoryAutocompletions (
122+ await getCompletions ( '{ price: 1, category: 1' , {
123+ savedQueries,
124+ options : undefined ,
125+ queryProperty : 'project' ,
126+ onApply : mockOnApply ,
127+ theme : 'light' ,
128+ } )
129+ ) ;
130+ expect ( completions ) . to . have . lengthOf ( 1 ) ;
131+ expect ( completions [ 0 ] . label ) . to . equal ( `{
132+ productId: 1,
133+ category: 1,
134+ price: 1
135+ }` ) ;
84136 } ) ;
85137
86- it ( 'completes "any text" when inside a string' , async function ( ) {
87- const prettifiedSavedQueries = savedQueries . map ( ( query ) =>
88- createQuery ( query )
138+ it ( 'does not return fields that match in other query properties' , async function ( ) {
139+ const completions = getQueryHistoryAutocompletions (
140+ await getCompletions ( 'local' , {
141+ savedQueries,
142+ options : undefined ,
143+ queryProperty : 'project' ,
144+ onApply : mockOnApply ,
145+ theme : 'light' ,
146+ } )
89147 ) ;
148+
149+ const queryHistoryCompletions = getQueryHistoryAutocompletions ( completions ) ;
150+ expect ( queryHistoryCompletions ) . to . have . lengthOf ( 0 ) ;
151+ } ) ;
152+
153+ it ( 'completes regular query autocompletion items' , async function ( ) {
154+ // 'foo' matches > 45 methods and fields in the query autocompletion.
155+ const completions = (
156+ await getCompletions ( 'foo' , {
157+ savedQueries,
158+ options : undefined ,
159+ queryProperty : '' ,
160+ onApply : mockOnApply ,
161+ theme : 'light' ,
162+ } )
163+ ) . filter ( ( { type } ) => type !== 'favorite' && type !== 'query-history' ) ;
164+
165+ expect ( completions ) . to . have . length . greaterThan ( 40 ) ;
166+ } ) ;
167+
168+ it ( 'completes fields inside a string' , async function ( ) {
90169 expect (
91170 (
92- await getCompletions (
93- '{ bar: 1, buz: 2, foo: "b' ,
171+ await getCompletions ( '{ bar: 1, buz: 2, foo: "b' , {
94172 savedQueries,
95- undefined ,
96- mockOnApply ,
97- 'light'
98- )
173+ options : undefined ,
174+ queryProperty : '' ,
175+ onApply : mockOnApply ,
176+ theme : 'light' ,
177+ } )
99178 ) . map ( ( completion ) => completion . label )
100- ) . to . deep . eq ( [ 'bar' , '1' , 'buz' , '2' , 'foo' , ... prettifiedSavedQueries ] ) ;
179+ ) . to . deep . eq ( [ 'bar' , '1' , 'buz' , '2' , 'foo' ] ) ;
101180 } ) ;
102181} ) ;
0 commit comments