Browse Source

fix: inherited class lookup

e22m4u 2 months ago
parent
commit
6a9a9728f7
3 changed files with 45 additions and 3 deletions
  1. 1 1
      dist/cjs/index.cjs
  2. 4 2
      src/service-container.js
  3. 40 0
      src/service-container.spec.js

+ 1 - 1
dist/cjs/index.cjs

@@ -146,10 +146,10 @@ var _ServiceContainer = class _ServiceContainer {
    */
   has(ctor) {
     if (this._services.has(ctor)) return true;
-    if (this._parent) return this._parent.has(ctor);
     const ctors = this._services.keys();
     const inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
     if (inheritedCtor) return true;
+    if (this._parent) return this._parent.has(ctor);
     return false;
   }
   /**

+ 4 - 2
src/service-container.js

@@ -148,12 +148,14 @@ export class ServiceContainer {
    */
   has(ctor) {
     if (this._services.has(ctor)) return true;
-    if (this._parent) return this._parent.has(ctor);
     // если не удалось найти указанный конструктор,
-    // то пытаемся найти его наследника
+    // то выполняется поиск его наследника
     const ctors = this._services.keys();
     const inheritedCtor = ctors.find(v => v.prototype instanceof ctor);
     if (inheritedCtor) return true;
+    // если определен родительский контейнер,
+    // то выполняется поиск в родителе
+    if (this._parent) return this._parent.has(ctor);
     return false;
   }
 

+ 40 - 0
src/service-container.spec.js

@@ -595,6 +595,46 @@ describe('ServiceContainer', function () {
           const res = container.has(ChildService);
           expect(res).to.be.false;
         });
+
+        it('returns true for the child container if the child class is registered in the parent container', function () {
+          class ParentService extends Service {}
+          class ChildService extends ParentService {}
+          const parentContainer = new ServiceContainer();
+          const childContainer = new ServiceContainer(parentContainer);
+          parentContainer.add(ChildService);
+          const res = childContainer.has(ParentService);
+          expect(res).to.be.true;
+        });
+
+        it('returns false for the child container if the parent class is registered in the parent container', function () {
+          class ParentService extends Service {}
+          class ChildService extends ParentService {}
+          const parentContainer = new ServiceContainer();
+          const childContainer = new ServiceContainer(parentContainer);
+          parentContainer.add(ParentService);
+          const res = childContainer.has(ChildService);
+          expect(res).to.be.false;
+        });
+
+        it('returns true for the child container if the child class is registered in the child container', function () {
+          class ParentService extends Service {}
+          class ChildService extends ParentService {}
+          const parentContainer = new ServiceContainer();
+          const childContainer = new ServiceContainer(parentContainer);
+          childContainer.add(ChildService);
+          const res = childContainer.has(ParentService);
+          expect(res).to.be.true;
+        });
+
+        it('returns false for the child container if the parent class is registered in the child container', function () {
+          class ParentService extends Service {}
+          class ChildService extends ParentService {}
+          const parentContainer = new ServiceContainer();
+          const childContainer = new ServiceContainer(parentContainer);
+          childContainer.add(ParentService);
+          const res = childContainer.has(ChildService);
+          expect(res).to.be.false;
+        });
       });
 
       describe('in case of a parent container', function () {