Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(mongoose): Add unit test to be sure the schemaOptions is correc… #752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/mongoose/test/decorators/schemas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Property} from "@tsed/common/src";
import {expect} from "chai";
import {getSchema, Model, MongooseSchema, ObjectID} from "../../src";

describe("@MongooseSchema", () => {
it("should declare a schema with some options", () => {
// GIVEN
@MongooseSchema({schemaOptions: {_id: false}})
class MySchema {

@Property()
someProperty: string;
}

@Model()
class ModelWithSchema {
@ObjectID("id")
_id: string;

@Property()
mySchema: MySchema;
}

const schema = getSchema(MySchema);

// @ts-ignore
expect(schema.options._id).to.deep.eq(false);
});
});
11 changes: 10 additions & 1 deletion packages/mongoose/test/helpers/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {Property, Required} from "@tsed/common";
import {Model, ObjectID, PostHook, PreHook, Unique} from "../../../src/decorators";
import {Model, MongooseSchema, ObjectID, PostHook, PreHook, Unique} from "../../../src/decorators";

@MongooseSchema({schemaOptions: {_id: false}})
export class TestRole {
@Property()
name: string;
}

@Model({schemaOptions: {timestamps: true}})
@PreHook("save", (user: TestUser, next: any) => {
Expand All @@ -26,4 +32,7 @@ export class TestUser {

@Property()
post: string;

@Property()
role: TestRole;
}
18 changes: 13 additions & 5 deletions packages/mongoose/test/user.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {TestMongooseContext} from "@tsed/testing-mongoose";
import {expect} from "chai";
import {MongooseModel} from "../src/interfaces";
import {TestUser} from "./helpers/models/User";
import {TestRole, TestUser} from "./helpers/models/User";
import {Server} from "./helpers/Server";

describe("UserModel", () => {
describe("UserModel with context", () => {
beforeEach(TestMongooseContext.create);
afterEach(TestMongooseContext.reset);

it("should run pre and post hook", TestMongooseContext.inject([TestUser], async (userModel: MongooseModel<TestUser>) => {
// GIVEN
const role = new TestRole();
role.name = "name";
const user = new userModel({
email: "test@test.fr"
email: "test@test.fr",
role
});

// WHEN
Expand All @@ -20,18 +23,23 @@ describe("UserModel", () => {
// THEN
expect(user.pre).to.equal("hello pre");
expect(user.post).to.equal("hello post");
// @ts-ignore
expect(user.role.toObject()).to.deep.equal({name: "name"});
}));
});

describe("UserModel", () => {
describe("UserModel with bootstrap", () => {
beforeEach(TestMongooseContext.bootstrap(Server));
afterEach(TestMongooseContext.clearDatabase);
afterEach(TestMongooseContext.reset);

it("should run pre and post hook", TestMongooseContext.inject([TestUser], async (userModel: MongooseModel<TestUser>) => {
// GIVEN
const user = new userModel({
email: "test@test.fr"
email: "test@test.fr",
role: {
name: "test"
}
});

// WHEN
Expand Down