Skip to content

Commit

Permalink
fix(mongoose): Fix plugin @prehook and @posthook registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jun 20, 2019
1 parent 2b48cff commit 7496397
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 74 deletions.
2 changes: 2 additions & 0 deletions packages/mongoose/src/decorators/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {MongooseModelOptions} from "../interfaces/MongooseModelOptions";
import {registerModel} from "../registries/MongooseModelRegistry";
import {createModel, getSchema} from "../utils";
import {applySchemaOptions} from "../utils/schemaOptions";

/**
* Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with
Expand Down Expand Up @@ -42,5 +43,6 @@ import {createModel, getSchema} from "../utils";
export function Model(options: MongooseModelOptions = {}) {
return (target: any) => {
registerModel(target, createModel(target, getSchema(target, options), options.name, options.collection, options.skipInit));
applySchemaOptions(target, {});
};
}
9 changes: 7 additions & 2 deletions packages/mongoose/src/utils/schemaOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import {MongooseSchemaOptions} from "../interfaces";
export function schemaOptions(target: any, options?: MongooseSchemaOptions) {
const store = Store.from(target);

options = deepExtends(store.get(MONGOOSE_SCHEMA_OPTIONS) || {}, options);
store.set(MONGOOSE_SCHEMA_OPTIONS, options);
if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {
store.set(MONGOOSE_SCHEMA_OPTIONS, {});
}

if (options) {
store.set(MONGOOSE_SCHEMA_OPTIONS, deepExtends(store.get(MONGOOSE_SCHEMA_OPTIONS), options));
}

return store.get(MONGOOSE_SCHEMA_OPTIONS);
}
Expand Down
55 changes: 22 additions & 33 deletions packages/mongoose/test/decorators/postHook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,47 @@
import {PostHook} from "../../src/decorators";
import * as mod from "../../src/utils/schemaOptions";
import * as Sinon from "sinon";
import {Model, PostHook} from "../../src/decorators";
import {schemaOptions} from "../../src/utils/schemaOptions";

const sandbox = Sinon.createSandbox();
describe("@PostHook()", () => {
describe("when decorator is used as class decorator", () => {
class Test {}
it("should call applySchemaOptions", () => {
// GIVEN
const fn = sandbox.stub();

before(() => {
this.applySchemaOptionsStub = Sinon.stub(mod, "applySchemaOptions");
this.fn = () => {};
PostHook("method", this.fn as any)(Test);
});
// WHEN
@Model({name: "TestPostHook"})
@PostHook("method", fn)
class Test {
}

after(() => {
this.applySchemaOptionsStub.restore();
});
// THEN
const options = schemaOptions(Test);

it("should call applySchemaOptions", () => {
this.applySchemaOptionsStub.should.have.been.calledWithExactly(Test, {
options.should.deep.eq({
name: "TestPostHook",
post: [
{
method: "method",
fn: this.fn
fn
}
]
});
});
});

describe("when decorator is used as method decorator", () => {
before(() => {
this.applySchemaOptionsStub = Sinon.stub(mod, "applySchemaOptions");

it("should call applySchemaOptions", () => {
class Test {
@PostHook("save")
static method() {}
static method() {
}
}

this.clazz = Test;
});
const {post: [options]} = schemaOptions(Test);

after(() => {
this.applySchemaOptionsStub.restore();
});

it("should call applySchemaOptions", () => {
this.applySchemaOptionsStub.should.have.been.calledWithExactly(this.clazz, {
post: [
{
method: "save",
fn: Sinon.match.func
}
]
});
options.method.should.eq("save");
options.fn.should.be.a("function");
});
});
});
68 changes: 30 additions & 38 deletions packages/mongoose/test/decorators/preHook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,55 @@
import {PreHook} from "../../src/decorators";
import * as mod from "../../src/utils/schemaOptions";
import * as Sinon from "sinon";
import {Model, PreHook} from "../../src/decorators";
import {schemaOptions} from "../../src/utils/schemaOptions";

const sandbox = Sinon.createSandbox();
describe("@PreHook()", () => {
describe("when decorator is used as class decorator", () => {
class Test {}
it("should call applySchemaOptions", () => {
// GIVEN
const fn = sandbox.stub();
const errorCb = sandbox.stub();

before(() => {
this.applySchemaOptionsStub = Sinon.stub(mod, "applySchemaOptions");
this.fn = () => {};
this.errorCb = () => {};
PreHook("method", this.fn as any, {parallel: true, errorCb: this.errorCb as any})(Test);
});
// WHEN
@Model({name: "TestPreHook"})
@PreHook("method", fn, {parallel: true, errorCb: errorCb as any})
class Test {
}

after(() => {
this.applySchemaOptionsStub.restore();
});
// THEN
const options = schemaOptions(Test);

it("should call applySchemaOptions", () => {
this.applySchemaOptionsStub.should.have.been.calledWithExactly(Test, {
options.should.deep.eq({
name: "TestPreHook",
pre: [
{
method: "method",
parallel: true,
fn: this.fn,
errorCb: this.errorCb
fn,
errorCb
}
]
});
});
});

describe("when decorator is used as method decorator", () => {
before(() => {
this.applySchemaOptionsStub = Sinon.stub(mod, "applySchemaOptions");

it("should call applySchemaOptions", () => {
class Test {
@PreHook("save", {parallel: true, errorCb: "errorCb" as any})
static method() {}
@PreHook("save", {
parallel: true, errorCb: () => {
}
})
static method() {
}
}

this.clazz = Test;
});
const {pre: [options]} = schemaOptions(Test);

after(() => {
this.applySchemaOptionsStub.restore();
});

it("should call applySchemaOptions", () => {
this.applySchemaOptionsStub.should.have.been.calledWithExactly(this.clazz, {
pre: [
{
method: "save",
parallel: true,
fn: Sinon.match.func,
errorCb: "errorCb"
}
]
});
options.method.should.eq("save");
options.parallel.should.eq(true);
options.fn.should.be.a("function");
options.errorCb.should.be.a("function");
});
});
});
2 changes: 1 addition & 1 deletion packages/mongoose/test/utils/schemaOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("schemaOptions", () => {
});

it("should return schema options (1)", () => {
expect(schemaOptions(Test)).to.eq(undefined);
expect(schemaOptions(Test)).to.deep.eq({});
});

it("should return schema options (2)", () => {
Expand Down

0 comments on commit 7496397

Please sign in to comment.