-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(di): add inject(), constant(), value() function to inject servic…
…e or value in injectable property value defined by Constant and Value decorator are now available on constructor injectable services on property are now available in the constructor BREAKING CHANGE: This change require to set ` "useDefineForClassFields": false` in your tsconfig Some DI methods are removed like bindInjectableProperties() which is not necessary.
- Loading branch information
Showing
41 changed files
with
945 additions
and
945 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const INJECTABLE_PROP = "DI:INJECTABLE_PROP"; | ||
export const DI_PARAMS = "DI:PARAMS"; | ||
export const DI_PARAM_OPTIONS = "DI:PARAM:OPTIONS"; | ||
export const DI_INVOKE_OPTIONS = Symbol("DI_INVOKE_OPTIONS"); | ||
export const DI_INJECTABLE_PROPS = Symbol("DI_INJECTABLE_PROPS"); | ||
export const DI_USE_OPTIONS = "DI_USE_OPTIONS"; | ||
export const DI_USE_PARAM_OPTIONS = "DI_USE_PARAM_OPTIONS"; | ||
export const DI_INTERCEPTOR_OPTIONS = "DI_INTERCEPTOR_OPTIONS"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,95 @@ | ||
import {Store} from "@tsed/core"; | ||
import {INJECTABLE_PROP} from "../constants/constants.js"; | ||
import {Constant} from "./constant.js"; | ||
|
||
class Test {} | ||
import {DITest} from "../../node/index.js"; | ||
import {constant, Constant} from "./constant.js"; | ||
|
||
describe("@Constant()", () => { | ||
it("should store metadata", () => { | ||
// WHEN | ||
Constant("expression")(Test, "test"); | ||
|
||
// THEN | ||
const store = Store.from(Test).get(INJECTABLE_PROP); | ||
|
||
expect(store).toEqual({ | ||
test: { | ||
bindingType: "constant", | ||
propertyKey: "test", | ||
expression: "expression", | ||
defaultValue: undefined | ||
beforeEach(() => | ||
DITest.create({ | ||
logger: { | ||
level: "off" | ||
} | ||
}) | ||
); | ||
afterEach(() => DITest.reset()); | ||
describe("when decorator is used as property decorator", () => { | ||
it("should create a getter", async () => { | ||
// WHEN | ||
class Test { | ||
@Constant("logger.level", "default value") | ||
test: string; | ||
} | ||
|
||
// THEN | ||
|
||
const test = await DITest.invoke<Test>(Test); | ||
|
||
expect(test.test).toEqual("off"); | ||
}); | ||
it("should create a getter with default value", async () => { | ||
// WHEN | ||
class Test { | ||
@Constant("logger.test", "default value") | ||
test: string; | ||
} | ||
|
||
// THEN | ||
|
||
const test = await DITest.invoke<Test>(Test); | ||
|
||
expect(test.test).toEqual("default value"); | ||
}); | ||
it("shouldn't be possible to modify injected value from injector.settings", async () => { | ||
// WHEN | ||
class Test { | ||
@Constant("logger.level") | ||
test: string; | ||
} | ||
|
||
// THEN | ||
|
||
const test = await DITest.invoke<Test>(Test); | ||
|
||
test.test = "new value"; | ||
|
||
expect(test.test).toEqual("off"); | ||
}); | ||
it("should create a getter with native default value", async () => { | ||
// WHEN | ||
class Test { | ||
@Constant("logger.test") | ||
test: string = "default prop"; | ||
} | ||
|
||
// THEN | ||
|
||
const test = await DITest.invoke<Test>(Test); | ||
|
||
expect(test.test).toEqual("default prop"); | ||
}); | ||
}); | ||
describe("when constant is used as default value initializer", () => { | ||
it("should inject constant to the property", async () => { | ||
// WHEN | ||
class Test { | ||
test: string = constant("logger.level", "default value"); | ||
} | ||
|
||
// THEN | ||
|
||
const test = await DITest.invoke<Test>(Test); | ||
|
||
expect(test.test).toEqual("off"); | ||
}); | ||
it("should return the default value if expression is undefined", async () => { | ||
// WHEN | ||
class Test { | ||
test: string = constant("logger.test", "default value"); | ||
} | ||
|
||
// THEN | ||
|
||
const test = await DITest.invoke<Test>(Test); | ||
|
||
expect(test.test).toEqual("default value"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.