@@ -7,6 +7,7 @@ import { InvalidFiltersFormatError, NoMatchingOperatorError } from '../../../src
7
7
8
8
describe ( 'service > filters-parser' , ( ) => {
9
9
let IslandModel ;
10
+ let islandForestSchema ;
10
11
let defaultParser ;
11
12
const timezone = 'Europe/Paris' ;
12
13
@@ -16,22 +17,24 @@ describe('service > filters-parser', () => {
16
17
} ;
17
18
18
19
beforeAll ( ( ) => {
20
+ islandForestSchema = {
21
+ name : 'Island' ,
22
+ idField : 'id' ,
23
+ primaryKeys : [ 'id' ] ,
24
+ isCompositePrimary : false ,
25
+ searchFields : [ 'name' ] ,
26
+ fields : [
27
+ { field : 'id' , type : 'Number' } ,
28
+ { field : 'name' , type : 'String' } ,
29
+ { field : 'size' , type : 'Number' } ,
30
+ { field : 'isBig' , type : 'Boolean' } ,
31
+ { field : 'inhabitedOn' , type : 'Date' } ,
32
+ ] ,
33
+ } ;
34
+
19
35
Interface . Schemas = {
20
36
schemas : {
21
- Island : {
22
- name : 'Island' ,
23
- idField : 'id' ,
24
- primaryKeys : [ 'id' ] ,
25
- isCompositePrimary : false ,
26
- searchFields : [ 'name' ] ,
27
- fields : [
28
- { field : 'id' , type : 'Number' } ,
29
- { field : 'name' , type : 'String' } ,
30
- { field : 'size' , type : 'Number' } ,
31
- { field : 'isBig' , type : 'Boolean' } ,
32
- { field : 'inhabitedOn' , type : 'Date' } ,
33
- ] ,
34
- } ,
37
+ Island : islandForestSchema ,
35
38
} ,
36
39
} ;
37
40
@@ -132,6 +135,88 @@ describe('service > filters-parser', () => {
132
135
await expect ( defaultParser . formatCondition ( { field : 'toto' , operator : 'contains' , value : 'it' } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
133
136
} ) ;
134
137
} ) ;
138
+
139
+ describe ( 'on a smart field' , ( ) => {
140
+ describe ( 'with filter method not defined' , ( ) => {
141
+ it ( 'should throw an error' , async ( ) => {
142
+ expect . assertions ( 1 ) ;
143
+
144
+ const oldFields = islandForestSchema . fields ;
145
+ islandForestSchema . fields = [ {
146
+ field : 'smart name' ,
147
+ type : 'String' ,
148
+ isVirtual : true ,
149
+ get ( ) { } ,
150
+ } ] ;
151
+
152
+ await expect ( defaultParser . formatCondition ( {
153
+ field : 'smart name' ,
154
+ operator : 'present' ,
155
+ value : null ,
156
+ } ) ) . rejects . toThrow ( '"filter" method missing on smart field "smart name"' ) ;
157
+
158
+ islandForestSchema . fields = oldFields ;
159
+ } ) ;
160
+ } ) ;
161
+
162
+ describe ( 'with filter method defined' , ( ) => {
163
+ describe ( 'when filter method return null or undefined' , ( ) => {
164
+ it ( 'should throw an error' , async ( ) => {
165
+ expect . assertions ( 1 ) ;
166
+
167
+ const oldFields = islandForestSchema . fields ;
168
+ islandForestSchema . fields = [ {
169
+ field : 'smart name' ,
170
+ type : 'String' ,
171
+ isVirtual : true ,
172
+ get ( ) { } ,
173
+ filter ( ) { } ,
174
+ } ] ;
175
+
176
+ await expect ( defaultParser . formatCondition ( {
177
+ field : 'smart name' ,
178
+ operator : 'present' ,
179
+ value : null ,
180
+ } ) ) . rejects . toThrow ( '"filter" method on smart field "smart name" must return a condition' ) ;
181
+
182
+ islandForestSchema . fields = oldFields ;
183
+ } ) ;
184
+ } ) ;
185
+
186
+ describe ( 'when filter method return a condition' , ( ) => {
187
+ it ( 'should return the condition' , async ( ) => {
188
+ expect . assertions ( 4 ) ;
189
+
190
+ const where = { id : 1 } ;
191
+ const oldFields = islandForestSchema . fields ;
192
+ islandForestSchema . fields = [ {
193
+ field : 'smart name' ,
194
+ type : 'String' ,
195
+ isVirtual : true ,
196
+ get ( ) { } ,
197
+ filter : jest . fn ( ( ) => where ) ,
198
+ } ] ;
199
+
200
+ const condition = {
201
+ field : 'smart name' ,
202
+ operator : 'present' ,
203
+ value : null ,
204
+ } ;
205
+ expect ( await defaultParser . formatCondition ( condition ) ) . toStrictEqual ( where ) ;
206
+ expect ( islandForestSchema . fields [ 0 ] . filter . mock . calls ) . toHaveLength ( 1 ) ;
207
+ expect ( islandForestSchema . fields [ 0 ] . filter . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
208
+ expect ( islandForestSchema . fields [ 0 ] . filter . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( {
209
+ where : {
210
+ $exists : true ,
211
+ $ne : null ,
212
+ } ,
213
+ condition,
214
+ } ) ;
215
+ islandForestSchema . fields = oldFields ;
216
+ } ) ;
217
+ } ) ;
218
+ } ) ;
219
+ } ) ;
135
220
} ) ;
136
221
137
222
describe ( 'formatAggregatorOperator function' , ( ) => {
@@ -182,6 +267,45 @@ describe('service > filters-parser', () => {
182
267
} ) ;
183
268
} ) ;
184
269
270
+ describe ( 'isSmartField' , ( ) => {
271
+ describe ( 'on a unknown field' , ( ) => {
272
+ it ( 'should return false' , ( ) => {
273
+ expect . assertions ( 1 ) ;
274
+ const schemaToTest = { fields : [ ] } ;
275
+
276
+ expect ( defaultParser . isSmartField ( schemaToTest , 'unknown' ) ) . toBeFalse ( ) ;
277
+ } ) ;
278
+ } ) ;
279
+
280
+ describe ( 'on a non smart field' , ( ) => {
281
+ it ( 'should return false' , ( ) => {
282
+ expect . assertions ( 1 ) ;
283
+ const schemaToTest = {
284
+ fields : [ {
285
+ field : 'name' ,
286
+ isVirtual : false ,
287
+ } ] ,
288
+ } ;
289
+
290
+ expect ( defaultParser . isSmartField ( schemaToTest , 'name' ) ) . toBeFalse ( ) ;
291
+ } ) ;
292
+ } ) ;
293
+
294
+ describe ( 'on a smart field' , ( ) => {
295
+ it ( 'should return true' , ( ) => {
296
+ expect . assertions ( 1 ) ;
297
+ const schemaToTest = {
298
+ fields : [ {
299
+ field : 'name' ,
300
+ isVirtual : true ,
301
+ } ] ,
302
+ } ;
303
+
304
+ expect ( defaultParser . isSmartField ( schemaToTest , 'name' ) ) . toBeTrue ( ) ;
305
+ } ) ;
306
+ } ) ;
307
+ } ) ;
308
+
185
309
describe ( 'formatField function' , ( ) => {
186
310
it ( 'should format default field correctly' , ( ) => {
187
311
expect . assertions ( 1 ) ;
0 commit comments