@@ -7,6 +7,7 @@ import mongooseConnect from '../../utils/mongoose-connect';
7
7
describe ( 'service > resources-getter' , ( ) => {
8
8
let OrderModel ;
9
9
let UserModel ;
10
+ let FilmModel ;
10
11
11
12
const options = {
12
13
mongoose,
@@ -43,6 +44,19 @@ describe('service > resources-getter', () => {
43
44
{ field : 'age' , type : 'Number' } ,
44
45
] ,
45
46
} ,
47
+ Film : {
48
+ name : 'Film' ,
49
+ fields : [
50
+ { field : '_id' , type : 'String' } ,
51
+ { field : 'title' , type : 'String' } ,
52
+ { field : 'duration' , type : 'Number' } ,
53
+ {
54
+ field : 'description' ,
55
+ type : 'String' ,
56
+ get : ( film ) => `${ film . title } ${ film . duration } ` ,
57
+ } ,
58
+ ] ,
59
+ } ,
46
60
} ,
47
61
} ;
48
62
@@ -60,11 +74,17 @@ describe('service > resources-getter', () => {
60
74
name : { type : String } ,
61
75
age : { type : Number } ,
62
76
} ) ;
77
+ const FilmSchema = mongoose . Schema ( {
78
+ _id : { type : 'ObjectId' } ,
79
+ title : { type : String } ,
80
+ duration : { type : Number } ,
81
+ } ) ;
63
82
64
83
OrderModel = mongoose . model ( 'Order' , OrderSchema ) ;
65
84
UserModel = mongoose . model ( 'User' , UserSchema ) ;
85
+ FilmModel = mongoose . model ( 'Film' , FilmSchema ) ;
66
86
67
- return Promise . all ( [ OrderModel . remove ( { } ) , UserModel . remove ( { } ) ] ) ;
87
+ return Promise . all ( [ OrderModel . remove ( { } ) , UserModel . remove ( { } ) , FilmModel . remove ( { } ) ] ) ;
68
88
} )
69
89
. then ( ( ) =>
70
90
Promise . all ( [
@@ -95,6 +115,23 @@ describe('service > resources-getter', () => {
95
115
name : 'Jacco Gardner' ,
96
116
} ,
97
117
] ) ,
118
+ loadFixture ( FilmModel , [
119
+ {
120
+ _id : '41224d776a326fb40f000011' ,
121
+ duration : 149 ,
122
+ title : 'Terminator' ,
123
+ } ,
124
+ {
125
+ _id : '41224d776a326fb40f000012' ,
126
+ duration : 360 ,
127
+ title : 'Titanic' ,
128
+ } ,
129
+ {
130
+ _id : '41224d776a326fb40f000013' ,
131
+ duration : 125 ,
132
+ title : 'Matrix' ,
133
+ } ,
134
+ ] ) ,
98
135
] ) ) ;
99
136
} ) ;
100
137
@@ -273,4 +310,44 @@ describe('service > resources-getter', () => {
273
310
} ) ;
274
311
} ) ;
275
312
} ) ;
313
+
314
+ describe ( 'projection feature' , ( ) => {
315
+ describe ( 'with selected smartfield' , ( ) => {
316
+ it ( 'should return all fields' , async ( ) => {
317
+ expect . assertions ( 3 ) ;
318
+ const parameters = {
319
+ fields : { films : 'description' } ,
320
+ page : { number : '1' , size : '15' } ,
321
+ searchExtended : '0' ,
322
+ timezone : '+01:00' ,
323
+ } ;
324
+
325
+ const [ result ] = await new ResourcesGetter ( FilmModel , options , parameters ) . perform ( ) ;
326
+ expect ( result ) . toHaveLength ( 3 ) ;
327
+ const titles = result . filter ( ( film ) => ! ! film . title ) ;
328
+ expect ( titles ) . toHaveLength ( 3 ) ;
329
+ const durations = result . filter ( ( film ) => ! ! film . duration ) ;
330
+ expect ( durations ) . toHaveLength ( 3 ) ;
331
+ } ) ;
332
+ } ) ;
333
+
334
+ describe ( 'without selected smartfield' , ( ) => {
335
+ it ( 'should return only selected fields' , async ( ) => {
336
+ expect . assertions ( 3 ) ;
337
+ const parameters = {
338
+ fields : { films : 'title' } ,
339
+ page : { number : '1' , size : '15' } ,
340
+ searchExtended : '0' ,
341
+ timezone : '+01:00' ,
342
+ } ;
343
+
344
+ const [ result ] = await new ResourcesGetter ( FilmModel , options , parameters ) . perform ( ) ;
345
+ expect ( result ) . toHaveLength ( 3 ) ;
346
+ const titles = result . filter ( ( film ) => ! ! film . title ) ;
347
+ expect ( titles ) . toHaveLength ( 3 ) ;
348
+ const durations = result . filter ( ( film ) => ! ! film . duration ) ;
349
+ expect ( durations ) . toHaveLength ( 0 ) ;
350
+ } ) ;
351
+ } ) ;
352
+ } ) ;
276
353
} ) ;
0 commit comments