Skip to content

Commit

Permalink
refactor(di): reorganise di tree directory to isolate new functional …
Browse files Browse the repository at this point in the history
…di API
  • Loading branch information
Romakita committed Oct 7, 2024
1 parent 1d10ac5 commit 35cdff0
Show file tree
Hide file tree
Showing 26 changed files with 446 additions and 336 deletions.
2 changes: 1 addition & 1 deletion packages/di/src/common/decorators/autoInjectable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {isArray, type Type} from "@tsed/core";

import {LocalsContainer} from "../domain/LocalsContainer.js";
import {$injector} from "../fn/injector.js";
import type {TokenProvider} from "../interfaces/TokenProvider.js";
import {getConstructorDependencies} from "../utils/getConstructorDependencies.js";
import {$injector} from "../utils/injector.js";

function resolveAutoInjectableArgs(token: Type, args: unknown[]) {
const injector = $injector();
Expand Down
108 changes: 40 additions & 68 deletions packages/di/src/common/decorators/constant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {DITest} from "../../node/index.js";
import {Constant, constant} from "./constant.js";
import {Constant} from "./constant.js";

describe("@Constant()", () => {
beforeEach(() =>
Expand All @@ -10,86 +10,58 @@ describe("@Constant()", () => {
})
);
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
it("should create a getter", async () => {
// WHEN
class Test {
@Constant("logger.level", "default value")
test: string;
}

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

// THEN
const test = await DITest.invoke<Test>(Test);

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;
}
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
// THEN

const test = await DITest.invoke<Test>(Test);
const test = await DITest.invoke<Test>(Test);

test.test = "new value";
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;
}

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

// THEN
const test = await DITest.invoke<Test>(Test);

const test = await DITest.invoke<Test>(Test);
test.test = "new value";

expect(test.test).toEqual("default prop");
});
expect(test.test).toEqual("off");
});
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");
}
it("should create a getter with native default value", async () => {
// WHEN
class Test {
@Constant("logger.test")
test: string = "default prop";
}

// THEN
// THEN

const test = await DITest.invoke<Test>(Test);
const test = await DITest.invoke<Test>(Test);

expect(test.test).toEqual("default value");
});
expect(test.test).toEqual("default prop");
});
});
8 changes: 1 addition & 7 deletions packages/di/src/common/decorators/constant.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {catchError, deepClone} from "@tsed/core";

import {injector as $injector} from "../utils/injector.js";

export function constant<Type>(expression: string): Type | undefined;
export function constant<Type>(expression: string, defaultValue: Type | undefined): Type;
export function constant<Type>(expression: string, defaultValue?: Type | undefined): Type | undefined {
return $injector().settings.get(expression, defaultValue);
}
import {constant} from "../fn/constant.js";

export function bindConstant(target: any, propertyKey: string | symbol, expression: string, defaultValue?: any) {
const symbol = Symbol();
Expand Down
Loading

0 comments on commit 35cdff0

Please sign in to comment.