@@ -3,21 +3,28 @@ import Interface from 'forest-express';
3
3
import QueryOptions from '../../src/services/query-options' ;
4
4
5
5
describe ( 'services > query-options' , ( ) => {
6
- describe ( 'order' , ( ) => {
7
- const buildModelMock = ( dialect ) => {
8
- // Sequelize is created here without connection to a database
9
- const sequelize = new Sequelize ( { dialect } ) ;
10
-
11
- const modelActor = sequelize . define ( 'actor' , { } ) ;
12
- const modelMovie = sequelize . define ( 'movie' , { } ) ;
6
+ const buildModelMock = ( dialect ) => {
7
+ // Sequelize is created here without connection to a database
8
+ const sequelize = new Sequelize ( { dialect } ) ;
13
9
14
- modelActor . belongsTo ( modelMovie ) ;
10
+ const modelActor = sequelize . define ( 'actor' , { } ) ;
11
+ const modelMovie = sequelize . define ( 'movie' , { } ) ;
15
12
16
- Interface . Schemas = { schemas : { actor : { idField : 'id' } } } ;
13
+ modelActor . belongsTo ( modelMovie ) ;
17
14
18
- return modelActor ;
15
+ Interface . Schemas = {
16
+ schemas : {
17
+ actor : {
18
+ idField : 'id' ,
19
+ fields : [ { field : 'smartField' , search : ( query ) => { query . include = 'movie' ; } } ] ,
20
+ } ,
21
+ } ,
19
22
} ;
20
23
24
+ return modelActor ;
25
+ } ;
26
+
27
+ describe ( 'order' , ( ) => {
21
28
describe ( 'with mssql' , ( ) => {
22
29
const model = buildModelMock ( 'mssql' ) ;
23
30
@@ -56,4 +63,58 @@ describe('services > query-options', () => {
56
63
} ) ;
57
64
} ) ;
58
65
} ) ;
66
+
67
+ describe ( 'search' , ( ) => {
68
+ const model = buildModelMock ( 'postgres' ) ;
69
+
70
+ describe ( 'when search on smart field is async' , ( ) => {
71
+ describe ( 'when promise reject' , ( ) => {
72
+ it ( 'should display an error message' , async ( ) => {
73
+ expect . assertions ( 1 ) ;
74
+
75
+ const loggerErrorSpy = jest . spyOn ( Interface . logger , 'error' ) ;
76
+
77
+ const errorThrown = new Error ( 'unexpected error' ) ;
78
+ Interface . Schemas . schemas . actor . fields [ 0 ] . search = async ( ) =>
79
+ Promise . reject ( errorThrown ) ;
80
+
81
+ const options = new QueryOptions ( model ) ;
82
+ await options . search ( 'search string' , null ) ;
83
+ expect ( loggerErrorSpy ) . toHaveBeenCalledWith ( 'Cannot search properly on Smart Field smartField' , errorThrown ) ;
84
+
85
+ loggerErrorSpy . mockClear ( ) ;
86
+ } ) ;
87
+ } ) ;
88
+
89
+ it ( 'should add the search query' , async ( ) => {
90
+ expect . assertions ( 1 ) ;
91
+
92
+ Interface . Schemas . schemas . actor . fields [ 0 ] . search = async ( query ) => {
93
+ await Promise . resolve ( ) ;
94
+ query . include = [ 'movie' ] ;
95
+ } ;
96
+
97
+ const options = new QueryOptions ( model ) ;
98
+ await options . search ( 'search string' , null ) ;
99
+ expect ( options . _customerIncludes ) . toStrictEqual ( [ 'movie' ] ) ;
100
+ } ) ;
101
+ } ) ;
102
+
103
+ describe ( 'when search on smart field throw an error' , ( ) => {
104
+ it ( 'should display an error message' , async ( ) => {
105
+ expect . assertions ( 1 ) ;
106
+
107
+ const loggerErrorSpy = jest . spyOn ( Interface . logger , 'error' ) ;
108
+
109
+ const errorThrown = new Error ( 'unexpected error' ) ;
110
+ Interface . Schemas . schemas . actor . fields [ 0 ] . search = ( ) => { throw errorThrown ; } ;
111
+
112
+ const options = new QueryOptions ( model ) ;
113
+ await options . search ( 'search string' , null ) ;
114
+ expect ( loggerErrorSpy ) . toHaveBeenCalledWith ( 'Cannot search properly on Smart Field smartField' , errorThrown ) ;
115
+
116
+ loggerErrorSpy . mockClear ( ) ;
117
+ } ) ;
118
+ } ) ;
119
+ } ) ;
59
120
} ) ;
0 commit comments