@@ -2,7 +2,13 @@ import { expect } from 'chai';
2
2
import { createQueryWithHistoryAutocompleter } from './query-autocompleter-with-history' ;
3
3
import { setupCodemirrorCompleter } from '../../test/completer' ;
4
4
import 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
+ }
6
12
7
13
describe ( 'query history autocompleter' , function ( ) {
8
14
const { getCompletions, cleanup } = setupCodemirrorCompleter (
@@ -46,7 +52,7 @@ describe('query history autocompleter', function () {
46
52
type : 'recent' ,
47
53
lastExecuted : new Date ( '2023-06-04T18:00:00Z' ) ,
48
54
queryProperties : {
49
- filter : { isActive : true } ,
55
+ filter : { isActive : true , score : { $exists : false } } ,
50
56
project : { userId : 1 , isActive : 1 } ,
51
57
collation : { locale : 'simple' } ,
52
58
sort : { userId : 1 } ,
@@ -71,32 +77,105 @@ describe('query history autocompleter', function () {
71
77
72
78
const mockOnApply : ( query : SavedQuery [ 'queryProperties' ] ) => any = ( ) => { } ;
73
79
74
- it ( 'returns all saved queries as completions on click ' , async function ( ) {
80
+ it ( 'returns all saved queries as completions with default {} ' , async function ( ) {
75
81
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
+ } )
77
89
) . to . have . lengthOf ( 5 ) ;
78
90
} ) ;
79
91
80
- it ( 'returns combined completions when user starts typing ' , async function ( ) {
92
+ it ( 'returns all saved queries as completions with empty entry ' , async function ( ) {
81
93
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
+ }` ) ;
84
136
} ) ;
85
137
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
+ } )
89
147
) ;
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 ( ) {
90
169
expect (
91
170
(
92
- await getCompletions (
93
- '{ bar: 1, buz: 2, foo: "b' ,
171
+ await getCompletions ( '{ bar: 1, buz: 2, foo: "b' , {
94
172
savedQueries,
95
- undefined ,
96
- mockOnApply ,
97
- 'light'
98
- )
173
+ options : undefined ,
174
+ queryProperty : '' ,
175
+ onApply : mockOnApply ,
176
+ theme : 'light' ,
177
+ } )
99
178
) . map ( ( completion ) => completion . label )
100
- ) . to . deep . eq ( [ 'bar' , '1' , 'buz' , '2' , 'foo' , ... prettifiedSavedQueries ] ) ;
179
+ ) . to . deep . eq ( [ 'bar' , '1' , 'buz' , '2' , 'foo' ] ) ;
101
180
} ) ;
102
181
} ) ;
0 commit comments