@@ -7,6 +7,7 @@ import { InvalidFiltersFormatError, NoMatchingOperatorError } from '../../../src
77
88describe ( 'service > filters-parser' , ( ) => {
99 let IslandModel ;
10+ let islandForestSchema ;
1011 let defaultParser ;
1112 const timezone = 'Europe/Paris' ;
1213
@@ -16,22 +17,24 @@ describe('service > filters-parser', () => {
1617 } ;
1718
1819 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+
1935 Interface . Schemas = {
2036 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 ,
3538 } ,
3639 } ;
3740
@@ -132,6 +135,88 @@ describe('service > filters-parser', () => {
132135 await expect ( defaultParser . formatCondition ( { field : 'toto' , operator : 'contains' , value : 'it' } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
133136 } ) ;
134137 } ) ;
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+ } ) ;
135220 } ) ;
136221
137222 describe ( 'formatAggregatorOperator function' , ( ) => {
@@ -182,6 +267,45 @@ describe('service > filters-parser', () => {
182267 } ) ;
183268 } ) ;
184269
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+
185309 describe ( 'formatField function' , ( ) => {
186310 it ( 'should format default field correctly' , ( ) => {
187311 expect . assertions ( 1 ) ;
0 commit comments