Skip to content

Commit

Permalink
test: add test for schema factory
Browse files Browse the repository at this point in the history
  • Loading branch information
LotfiMEZIANI committed May 22, 2022
1 parent f92c7de commit 7f4df5b
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion tests/e2e/schema.factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Prop, Schema, SchemaFactory } from '../../lib';
import { VirtualTypeOptions } from 'mongoose';
import { Prop, Schema, SchemaFactory, Virtual } from '../../lib';

@Schema({ validateBeforeSave: false, _id: true, autoIndex: true })
class ChildClass {
Expand All @@ -9,6 +10,9 @@ class ChildClass {
name: string;
}

const getterFunctionMock = jest.fn();
const setterFunctionMock = jest.fn();

@Schema({
validateBeforeSave: false,
_id: true,
Expand All @@ -24,6 +28,21 @@ class ExampleClass {

@Prop()
array: Array<any>;

@Virtual({
options: {
localField: 'array',
ref: 'ChildClass',
foreignField: 'id',
},
})
virtualPropsWithOptions: Array<ChildClass>;

@Virtual({
get: getterFunctionMock,
set: setterFunctionMock,
})
virtualPropsWithGetterSetterFunctions: Array<ChildClass>;
}

describe('SchemaFactory', () => {
Expand All @@ -50,4 +69,38 @@ describe('SchemaFactory', () => {
}),
);
});

it('should define for schema a virtuals with options', () => {
const {
virtuals: { virtualPropsWithOptions },
} = SchemaFactory.createForClass(ExampleClass) as any;

expect(virtualPropsWithOptions).toEqual(
expect.objectContaining({
path: 'virtualPropsWithOptions',
setters: [expect.any(Function)],
getters: [],
options: expect.objectContaining({
localField: 'array',
ref: 'ChildClass',
foreignField: 'id',
}),
}),
);
});

it('should define for schema a virtual with getter/setter functions', () => {
const {
virtuals: { virtualPropsWithGetterSetterFunctions },
} = SchemaFactory.createForClass(ExampleClass) as any;

expect(virtualPropsWithGetterSetterFunctions).toEqual(
expect.objectContaining({
path: 'virtualPropsWithGetterSetterFunctions',
setters: [setterFunctionMock],
getters: [getterFunctionMock],
options: {},
}),
);
});
});

0 comments on commit 7f4df5b

Please sign in to comment.