1- import { Prop , Schema , SchemaFactory } from '../../lib' ;
1+ import { VirtualTypeOptions } from 'mongoose' ;
2+ import { Prop , Schema , SchemaFactory , Virtual } from '../../lib' ;
23
34@Schema ( { validateBeforeSave : false , _id : true , autoIndex : true } )
45class ChildClass {
@@ -9,6 +10,9 @@ class ChildClass {
910 name : string ;
1011}
1112
13+ const getterFunctionMock = jest . fn ( ) ;
14+ const setterFunctionMock = jest . fn ( ) ;
15+
1216@Schema ( {
1317 validateBeforeSave : false ,
1418 _id : true ,
@@ -24,6 +28,21 @@ class ExampleClass {
2428
2529 @Prop ( )
2630 array : Array < any > ;
31+
32+ @Virtual ( {
33+ options : {
34+ localField : 'array' ,
35+ ref : 'ChildClass' ,
36+ foreignField : 'id' ,
37+ } ,
38+ } )
39+ virtualPropsWithOptions : Array < ChildClass > ;
40+
41+ @Virtual ( {
42+ get : getterFunctionMock ,
43+ set : setterFunctionMock ,
44+ } )
45+ virtualPropsWithGetterSetterFunctions : Array < ChildClass > ;
2746}
2847
2948describe ( 'SchemaFactory' , ( ) => {
@@ -50,4 +69,38 @@ describe('SchemaFactory', () => {
5069 } ) ,
5170 ) ;
5271 } ) ;
72+
73+ it ( 'should define for schema a virtuals with options' , ( ) => {
74+ const {
75+ virtuals : { virtualPropsWithOptions } ,
76+ } = SchemaFactory . createForClass ( ExampleClass ) as any ;
77+
78+ expect ( virtualPropsWithOptions ) . toEqual (
79+ expect . objectContaining ( {
80+ path : 'virtualPropsWithOptions' ,
81+ setters : [ expect . any ( Function ) ] ,
82+ getters : [ ] ,
83+ options : expect . objectContaining ( {
84+ localField : 'array' ,
85+ ref : 'ChildClass' ,
86+ foreignField : 'id' ,
87+ } ) ,
88+ } ) ,
89+ ) ;
90+ } ) ;
91+
92+ it ( 'should define for schema a virtual with getter/setter functions' , ( ) => {
93+ const {
94+ virtuals : { virtualPropsWithGetterSetterFunctions } ,
95+ } = SchemaFactory . createForClass ( ExampleClass ) as any ;
96+
97+ expect ( virtualPropsWithGetterSetterFunctions ) . toEqual (
98+ expect . objectContaining ( {
99+ path : 'virtualPropsWithGetterSetterFunctions' ,
100+ setters : [ setterFunctionMock ] ,
101+ getters : [ getterFunctionMock ] ,
102+ options : { } ,
103+ } ) ,
104+ ) ;
105+ } ) ;
53106} ) ;
0 commit comments