@@ -3,7 +3,7 @@ import loadFixture from 'mongoose-fixture-loader';
33import Interface from 'forest-express' ;
44import FiltersParser from '../../../src/services/filters-parser' ;
55import mongooseConnect from '../../utils/mongoose-connect' ;
6- import { InvalidFiltersFormatError , NoMatchingOperatorError } from '../../../src/services/errors' ;
6+ import { NoMatchingOperatorError } from '../../../src/services/errors' ;
77
88describe ( 'service > filters-parser' , ( ) => {
99 let IslandModel ;
@@ -38,9 +38,19 @@ describe('service > filters-parser', () => {
3838 comment : 'String' ,
3939 } ,
4040 } ,
41+ {
42+ field : 'ships' ,
43+ type : {
44+ fields : [ {
45+ field : 'weapon' ,
46+ type : 'String' ,
47+ } ] ,
48+ } ,
49+ } ,
4150 ] ,
4251 } ;
4352
53+
4454 Interface . Schemas = {
4555 schemas : {
4656 Island : islandForestSchema ,
@@ -92,6 +102,33 @@ describe('service > filters-parser', () => {
92102
93103 afterAll ( ( ) => mongoose . connection . close ( ) ) ;
94104
105+ afterEach ( ( ) => jest . restoreAllMocks ( ) ) ;
106+
107+ describe ( 'getParserForField' , ( ) => {
108+ describe ( 'with an embedded field' , ( ) => {
109+ it ( 'should return the parser for the nested field' , async ( ) => {
110+ expect . assertions ( 6 ) ;
111+
112+ const fakeParser = jest . fn ( ) . mockReturnValue ( 'parsedValue' ) ;
113+ const spy = jest . spyOn ( defaultParser , 'getParserForType' ) . mockReturnValue ( fakeParser ) ;
114+
115+ const parserForField = await defaultParser . getParserForField ( 'ships:weapon' ) ;
116+
117+ expect ( defaultParser . getParserForType ) . toHaveBeenCalledTimes ( 1 ) ;
118+ expect ( defaultParser . getParserForType ) . toHaveBeenCalledWith ( 'String' ) ;
119+
120+ expect ( fakeParser ) . not . toHaveBeenCalled ( ) ;
121+
122+ expect ( parserForField ( 'myValue' ) ) . toStrictEqual ( 'parsedValue' ) ;
123+
124+ expect ( fakeParser ) . toHaveBeenCalledTimes ( 1 ) ;
125+ expect ( fakeParser ) . toHaveBeenCalledWith ( 'myValue' ) ;
126+
127+ spy . mockRestore ( ) ;
128+ } ) ;
129+ } ) ;
130+ } ) ;
131+
95132 describe ( 'formatAggregation function' , ( ) => {
96133 describe ( 'on aggregated conditions' , ( ) => {
97134 it ( 'should format correctly' , async ( ) => {
@@ -134,106 +171,23 @@ describe('service > filters-parser', () => {
134171 expect ( await defaultParser . formatCondition ( { field : 'name' , operator : 'in' , value : 'Pyk, Dragonstone ' } ) ) . toStrictEqual ( { name : { $in : [ 'Pyk' , 'Dragonstone' ] } } ) ;
135172 } ) ;
136173
137- describe ( 'on empty condition' , ( ) => {
138- it ( 'should throw an error' , async ( ) => {
139- expect . assertions ( 2 ) ;
140- await expect ( defaultParser . formatCondition ( ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
141- await expect ( defaultParser . formatCondition ( { } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
142- } ) ;
143- } ) ;
144-
145- describe ( 'on badly formated condition' , ( ) => {
146- it ( 'should throw an error' , async ( ) => {
147- expect . assertions ( 7 ) ;
148- await expect ( defaultParser . formatCondition ( { operator : 'contains' , value : 'it' } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
149- await expect ( defaultParser . formatCondition ( { field : 'name' , operator : 'contains' } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
150- await expect ( defaultParser . formatCondition ( { field : 'name' , value : 'it' } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
151- await expect ( defaultParser . formatCondition ( { field : 'name' , operator : 'con' , value : 'it' } ) ) . rejects . toThrow ( NoMatchingOperatorError ) ;
152- await expect ( defaultParser . formatCondition ( 'toto' ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
153- await expect ( defaultParser . formatCondition ( [ 'toto' ] ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
154- await expect ( defaultParser . formatCondition ( { field : 'toto' , operator : 'contains' , value : 'it' } ) ) . rejects . toThrow ( InvalidFiltersFormatError ) ;
155- } ) ;
156- } ) ;
157-
158174 describe ( 'on a smart field' , ( ) => {
159- describe ( 'with filter method not defined' , ( ) => {
160- it ( 'should throw an error' , async ( ) => {
161- expect . assertions ( 1 ) ;
162-
163- const oldFields = islandForestSchema . fields ;
164- islandForestSchema . fields = [ {
165- field : 'smart name' ,
166- type : 'String' ,
167- isVirtual : true ,
168- get ( ) { } ,
169- } ] ;
170-
171- await expect ( defaultParser . formatCondition ( {
172- field : 'smart name' ,
173- operator : 'present' ,
174- value : null ,
175- } ) ) . rejects . toThrow ( '"filter" method missing on smart field "smart name"' ) ;
176-
177- islandForestSchema . fields = oldFields ;
178- } ) ;
179- } ) ;
180-
181- describe ( 'with filter method defined' , ( ) => {
182- describe ( 'when filter method return null or undefined' , ( ) => {
183- it ( 'should throw an error' , async ( ) => {
184- expect . assertions ( 1 ) ;
175+ it ( 'should call formatOperatorValue' , async ( ) => {
176+ expect . assertions ( 3 ) ;
185177
186- const oldFields = islandForestSchema . fields ;
187- islandForestSchema . fields = [ {
188- field : 'smart name' ,
189- type : 'String' ,
190- isVirtual : true ,
191- get ( ) { } ,
192- filter ( ) { } ,
193- } ] ;
194-
195- await expect ( defaultParser . formatCondition ( {
196- field : 'smart name' ,
197- operator : 'present' ,
198- value : null ,
199- } ) ) . rejects . toThrow ( '"filter" method on smart field "smart name" must return a condition' ) ;
200-
201- islandForestSchema . fields = oldFields ;
202- } ) ;
203- } ) ;
178+ const formattedCondition = 'myFormattedCondition' ;
179+ jest . spyOn ( defaultParser , 'formatOperatorValue' ) . mockReturnValue ( formattedCondition ) ;
204180
205- describe ( 'when filter method return a condition' , ( ) => {
206- it ( 'should return the condition' , async ( ) => {
207- expect . assertions ( 4 ) ;
181+ const condition = {
182+ field : 'smart name' ,
183+ operator : 'present' ,
184+ value : null ,
185+ } ;
186+ expect ( await defaultParser . formatCondition ( condition , true ) )
187+ . toStrictEqual ( formattedCondition ) ;
208188
209- const where = { id : 1 } ;
210- const oldFields = islandForestSchema . fields ;
211- islandForestSchema . fields = [ {
212- field : 'smart name' ,
213- type : 'String' ,
214- isVirtual : true ,
215- get ( ) { } ,
216- filter : jest . fn ( ( ) => where ) ,
217- } ] ;
218-
219- const condition = {
220- field : 'smart name' ,
221- operator : 'present' ,
222- value : null ,
223- } ;
224- expect ( await defaultParser . formatCondition ( condition ) ) . toStrictEqual ( where ) ;
225- expect ( islandForestSchema . fields [ 0 ] . filter . mock . calls ) . toHaveLength ( 1 ) ;
226- expect ( islandForestSchema . fields [ 0 ] . filter . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
227- expect ( islandForestSchema . fields [ 0 ] . filter . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( {
228- where : {
229- $exists : true ,
230- $ne : null ,
231- } ,
232- condition,
233- } ) ;
234- islandForestSchema . fields = oldFields ;
235- } ) ;
236- } ) ;
189+ expect ( defaultParser . formatOperatorValue ) . toHaveBeenCalledTimes ( 1 ) ;
190+ expect ( defaultParser . formatOperatorValue ) . toHaveBeenCalledWith ( 'smart name' , 'present' , null ) ;
237191 } ) ;
238192 } ) ;
239193 } ) ;
@@ -286,45 +240,6 @@ describe('service > filters-parser', () => {
286240 } ) ;
287241 } ) ;
288242
289- describe ( 'isSmartField' , ( ) => {
290- describe ( 'on a unknown field' , ( ) => {
291- it ( 'should return false' , ( ) => {
292- expect . assertions ( 1 ) ;
293- const schemaToTest = { fields : [ ] } ;
294-
295- expect ( defaultParser . isSmartField ( schemaToTest , 'unknown' ) ) . toBeFalse ( ) ;
296- } ) ;
297- } ) ;
298-
299- describe ( 'on a non smart field' , ( ) => {
300- it ( 'should return false' , ( ) => {
301- expect . assertions ( 1 ) ;
302- const schemaToTest = {
303- fields : [ {
304- field : 'name' ,
305- isVirtual : false ,
306- } ] ,
307- } ;
308-
309- expect ( defaultParser . isSmartField ( schemaToTest , 'name' ) ) . toBeFalse ( ) ;
310- } ) ;
311- } ) ;
312-
313- describe ( 'on a smart field' , ( ) => {
314- it ( 'should return true' , ( ) => {
315- expect . assertions ( 1 ) ;
316- const schemaToTest = {
317- fields : [ {
318- field : 'name' ,
319- isVirtual : true ,
320- } ] ,
321- } ;
322-
323- expect ( defaultParser . isSmartField ( schemaToTest , 'name' ) ) . toBeTrue ( ) ;
324- } ) ;
325- } ) ;
326- } ) ;
327-
328243 describe ( 'formatField function' , ( ) => {
329244 it ( 'should format default field correctly' , ( ) => {
330245 expect . assertions ( 1 ) ;
0 commit comments