Skip to content

Commit

Permalink
fix(mongoose): Add unit test for DynamicRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jul 20, 2019
1 parent 4bf5daa commit 5b9205f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/mongoose/src/decorators/dynamicRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import {applyDecorators, Store, StoreFn, StoreMerge} from "@tsed/core";
import {Schema as MongooseSchema} from "mongoose";
import {MONGOOSE_SCHEMA} from "../constants";

export type DynamicRef<T> = T | string;
/**
* Define a property as mongoose reference to other Model (decorated with @Model).
*
* ### Example
*
* ```typescript
*
* @Model()
* class FooModel {
*
* @DynamicRef('type')
* field: Ref<OtherFooModel | OtherModel>
* field: DynamicRef<OtherFooModel | OtherModel>
*
* @Enum(['OtherFooModel', 'OtherModel'])
* type: string
Expand All @@ -29,7 +29,7 @@ import {MONGOOSE_SCHEMA} from "../constants";
* }
* ```
*
* @param type
* @param refPath
* @returns {Function}
* @decorator
* @mongoose
Expand Down
30 changes: 30 additions & 0 deletions packages/mongoose/test/decorators/dynamicRef.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {descriptorOf, Store} from "@tsed/core";
import {expect} from "chai";
import {Schema} from "mongoose";
import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../../src/constants";
import {DynamicRef} from "../../src/decorators";

describe("@Ref()", () => {
it("should set metadata", () => {
// GIVEN

class RefTest {
}

Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest");

// WHEN
class Test {
@DynamicRef("RefTest")
test: DynamicRef<RefTest>;
}

// THEN
const store = Store.from(Test, "test", descriptorOf(Test, "test"));

expect(store.get(MONGOOSE_SCHEMA)).to.deep.eq({
type: Schema.Types.ObjectId,
refPath: "RefTest"
});
});
});

0 comments on commit 5b9205f

Please sign in to comment.