Skip to content

Commit

Permalink
version 0.2.0 - add arguments injection
Browse files Browse the repository at this point in the history
  • Loading branch information
zheksoon committed Mar 28, 2024
1 parent 0a86f94 commit 93a1c1b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dioma",
"version": "0.1.3",
"version": "0.2.0",
"description": "Elegant dependency injection container for vanilla JavaScript and TypeScript",
"keywords": [
"di",
Expand Down
139 changes: 139 additions & 0 deletions src/__tests__/dioma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CycleDependencyError,
AsyncCycleDependencyError,
injectAsync,
ArgumentsError,
} from "../dioma";
import { describe, it, expect, beforeEach } from "vitest";

Expand Down Expand Up @@ -859,4 +860,142 @@ describe("Dioma", () => {
// expect(instance2.instanceA).toBeInstanceOf(CircularDependencyA);
});
});

describe("Arguments", () => {
it("should be able to inject transient with arguments", () => {
class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Transient();
}

const instance = inject(ArgumentClass, "test");

expect(instance).toBeInstanceOf(ArgumentClass);
expect(instance.value).toBe("test");
});

it("should be able to inject multiple transient with arguments", () => {
class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Transient();
}

const instance = inject(ArgumentClass, "test");

expect(instance).toBeInstanceOf(ArgumentClass);
expect(instance.value).toBe("test");

const instance2 = inject(ArgumentClass, "test2");

expect(instance2).toBeInstanceOf(ArgumentClass);
expect(instance2.value).toBe("test2");
});

it("should be able to inject resolution with arguments", () => {
class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Resolution();
}

const instance = inject(ArgumentClass, "test");

expect(instance).toBeInstanceOf(ArgumentClass);
expect(instance.value).toBe("test");
});

it("should cache first instance of resolution with arguments", () => {
class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Resolution();
}

class InjectClass {
constructor(
public instance1 = inject(ArgumentClass, "test"),
public instance2 = inject(ArgumentClass, "test2")
) {}

static scope = Scopes.Transient();
}

const instance = inject(InjectClass);

expect(instance).toBeInstanceOf(InjectClass);

expect(instance.instance1).toBeInstanceOf(ArgumentClass);
expect(instance.instance1.value).toBe("test");

expect(instance.instance2).toBeInstanceOf(ArgumentClass);
expect(instance.instance2.value).toBe("test");
});

it("should throw error for singleton with arguments", () => {
class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Singleton();
}

expect(() => inject(ArgumentClass, "test")).toThrowError(ArgumentsError);
});

it("should throw error for container with arguments", () => {
const container = new Container();

class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Container();
}

expect(() => container.inject(ArgumentClass, "test")).toThrowError(ArgumentsError);
});

it("should be able to inject async with arguments", async () => {
class ArgumentClass {
constructor(public value: string) {}

static scope = Scopes.Transient();
}

const instance = await injectAsync(ArgumentClass, "test");

expect(instance).toBeInstanceOf(ArgumentClass);
expect(instance.value).toBe("test");
});

it("should be able to inject async with arguments (loop)", async () => {
class ClassA {
constructor(public value: string, public instanceB = inject(ClassB, "hello")) {}

static scope = Scopes.Resolution();
}

class ClassB {
public instanceA: ClassA;

constructor(public value: string, aPromise = injectAsync(ClassA, "world")) {
aPromise.then((instance) => {
this.instanceA = instance;
});
}

static scope = Scopes.Transient();
}

const instanceB = inject(ClassB, "test");

await delay();

expect(instanceB).toBeInstanceOf(ClassB);
expect(instanceB.value).toBe("test");

expect(instanceB.instanceA).toBeInstanceOf(ClassA);
expect(instanceB.instanceA.value).toBe("world");
});
});
});

0 comments on commit 93a1c1b

Please sign in to comment.