From 6b8b6d433c371d0482d31b249537278b7cae42f5 Mon Sep 17 00:00:00 2001 From: Konstantin Burov Date: Tue, 17 Sep 2024 09:34:51 +1000 Subject: [PATCH] PR feedback --- src/Injectable.ts | 22 ++++++++++++++++++++++ src/__tests__/PartialContainer.spec.ts | 18 +++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Injectable.ts b/src/Injectable.ts index e7e6109..01e5311 100644 --- a/src/Injectable.ts +++ b/src/Injectable.ts @@ -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. */ diff --git a/src/__tests__/PartialContainer.spec.ts b/src/__tests__/PartialContainer.spec.ts index 47f5459..9780d12 100644 --- a/src/__tests__/PartialContainer.spec.ts +++ b/src/__tests__/PartialContainer.spec.ts @@ -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")) @@ -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"); }); @@ -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);