Skip to content

Commit 7f4df5b

Browse files
committed
test: add test for schema factory
1 parent f92c7de commit 7f4df5b

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

tests/e2e/schema.factory.spec.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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 })
45
class 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

2948
describe('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

Comments
 (0)