@@ -3,23 +3,27 @@ import Interface from 'forest-express';
3
3
import QueryOptions from '../../src/services/query-options' ;
4
4
5
5
describe ( 'services > query-options' , ( ) => {
6
+ const resetSchema = ( ) => {
7
+ Interface . Schemas = {
8
+ schemas : {
9
+ actor : {
10
+ idField : 'id' ,
11
+ fields : [ { field : 'smartField' } , { field : 'secondSmartField' } ] ,
12
+ } ,
13
+ } ,
14
+ } ;
15
+ } ;
16
+
6
17
const buildModelMock = ( dialect ) => {
7
18
// Sequelize is created here without connection to a database
8
19
const sequelize = new Sequelize ( { dialect } ) ;
9
20
10
21
const modelActor = sequelize . define ( 'actor' , { } ) ;
11
22
const modelMovie = sequelize . define ( 'movie' , { } ) ;
12
23
13
- modelActor . belongsTo ( modelMovie ) ;
24
+ resetSchema ( ) ;
14
25
15
- Interface . Schemas = {
16
- schemas : {
17
- actor : {
18
- idField : 'id' ,
19
- fields : [ { field : 'smartField' , search : ( query ) => { query . include = 'movie' ; } } ] ,
20
- } ,
21
- } ,
22
- } ;
26
+ modelActor . belongsTo ( modelMovie ) ;
23
27
24
28
return modelActor ;
25
29
} ;
@@ -83,20 +87,27 @@ describe('services > query-options', () => {
83
87
expect ( loggerErrorSpy ) . toHaveBeenCalledWith ( 'Cannot search properly on Smart Field smartField' , errorThrown ) ;
84
88
85
89
loggerErrorSpy . mockClear ( ) ;
90
+ resetSchema ( ) ;
86
91
} ) ;
87
92
} ) ;
88
93
89
- it ( 'should add the search query ' , async ( ) => {
94
+ it ( 'should add the search includes ' , async ( ) => {
90
95
expect . assertions ( 1 ) ;
91
96
92
97
Interface . Schemas . schemas . actor . fields [ 0 ] . search = async ( query ) => {
93
98
await Promise . resolve ( ) ;
94
- query . include = [ 'movie' ] ;
99
+ query . include . push ( 'movie' ) ;
100
+ } ;
101
+ Interface . Schemas . schemas . actor . fields [ 1 ] . search = async ( query ) => {
102
+ await Promise . resolve ( ) ;
103
+ query . include . push ( 'toto' ) ;
95
104
} ;
96
105
97
106
const options = new QueryOptions ( model ) ;
98
107
await options . search ( 'search string' , null ) ;
99
- expect ( options . _customerIncludes ) . toStrictEqual ( [ 'movie' ] ) ;
108
+ expect ( options . _customerIncludes ) . toStrictEqual ( [ 'movie' , 'toto' ] ) ;
109
+
110
+ resetSchema ( ) ;
100
111
} ) ;
101
112
} ) ;
102
113
@@ -114,6 +125,21 @@ describe('services > query-options', () => {
114
125
expect ( loggerErrorSpy ) . toHaveBeenCalledWith ( 'Cannot search properly on Smart Field smartField' , errorThrown ) ;
115
126
116
127
loggerErrorSpy . mockClear ( ) ;
128
+ resetSchema ( ) ;
129
+ } ) ;
130
+ } ) ;
131
+
132
+ describe ( 'when smartField return none array include' , ( ) => {
133
+ it ( 'should transform include to array' , async ( ) => {
134
+ expect . assertions ( 1 ) ;
135
+
136
+ Interface . Schemas . schemas . actor . fields [ 0 ] . search = ( query ) => { query . include = 'movie' ; } ;
137
+
138
+ const options = new QueryOptions ( model ) ;
139
+ await options . search ( 'search string' , null ) ;
140
+ expect ( options . _customerIncludes ) . toStrictEqual ( [ 'movie' ] ) ;
141
+
142
+ resetSchema ( ) ;
117
143
} ) ;
118
144
} ) ;
119
145
} ) ;
0 commit comments