Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kburov-sc committed Sep 16, 2024
1 parent 0470b79 commit 6b8b6d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/Injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ export function Injectable(
/**
* Creates an Injectable factory function for an InjectableClass.
*
* @example
* ```ts
* class InjectableClassService {
* static dependencies = ["service"] as const;
* constructor(public service: string) {}
* public print(): string {
* console.log(this.service);
* }
* }
*
* let container = Container.provides("service", "service value")
* .provides(ClassInjectable("classService", InjectableClassService));
*
* container.get("classService").print(); // prints "service value"
*
* // prefer using Container's provideClass method. Above is the equivalent of:
* container = Container.provides("service", "service value")
* .providesClass("classService", InjectableClassService);
*
* container.get("classService").print(); // prints "service value"
* ```
*
* @param token Token identifying the Service.
* @param cls InjectableClass to instantiate.
*/
Expand Down
18 changes: 13 additions & 5 deletions src/__tests__/PartialContainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe("PartialContainer", () => {
() => {
expect(() => {
new Container({}).provides(containerWithService).get("TestService");
}).toThrow();
}).toThrow(/Could not find Service for Token "TestService"/);

new Container({})
.provides(Injectable("TestService", () => "old service from container"))
Expand All @@ -155,12 +155,12 @@ describe("PartialContainer", () => {
describe("provide service using provideValue", () => {
const dependenciesContainer = Container.provides(Injectable("TestService", () => "old service"));

describe("and the new Service does not override", () => {
test("and the new Service does not override", () => {
const partialContainer = new PartialContainer({}).providesValue("NewTestService", "new service");
expect(dependenciesContainer.provides(partialContainer).get("NewTestService")).toEqual("new service");
});

describe("and the new Service does override", () => {
test("and the new Service does override", () => {
const partialContainer = new PartialContainer({}).providesValue("TestService", "new service");
expect(dependenciesContainer.provides(partialContainer).get("TestService")).toEqual("new service");
});
Expand All @@ -176,10 +176,18 @@ describe("PartialContainer", () => {

describe("and the new Service does not override", () => {
const partialContainer = new PartialContainer({}).providesClass("NewTestService", NewTestService);
expect(dependenciesContainer.provides(partialContainer).get("NewTestService")).toBeInstanceOf(NewTestService);
test("fails if parent missing dependency", () => {
// @ts-expect-error should be a compile error because nothing provides "TestService"
expect(() => Container.provides(partialContainer).get("NewTestService")).toThrow(
/Could not find Service for Token "TestService"/
);
});
test("succeeds if parent has dependency", () => {
expect(dependenciesContainer.provides(partialContainer).get("NewTestService")).toBeInstanceOf(NewTestService);
});
});

describe("and the new Service does override", () => {
test("and the new Service does override", () => {
const partialContainer = new PartialContainer({})
.providesValue("TestService", "old service")
.providesClass("TestService", NewTestService);
Expand Down

0 comments on commit 6b8b6d4

Please sign in to comment.