Ver código fonte

Initializable decorator improvements

sanex3339 7 anos atrás
pai
commit
eb68e7ea57

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 1967
dist/index.js


+ 7 - 3
src/decorators/Initializable.ts

@@ -53,7 +53,7 @@ export function initializable (
  * @param metadataValue
  * @param {IInitializable} target
  */
-function initializeTargetMetadata (metadataKey: string, metadataValue: any, target: IInitializable, ): void {
+function initializeTargetMetadata (metadataKey: string, metadataValue: any, target: IInitializable): void {
     const hasInitializedMetadata: boolean = Reflect.hasMetadata(metadataKey, target);
 
     if (!hasInitializedMetadata) {
@@ -129,10 +129,14 @@ function wrapInitializeMethodInInitializeCheck (
     Object.defineProperty(target, initializeMethodName, {
         ...methodDescriptor,
         value: function (): void {
-            const result: any = originalMethod.apply(this, arguments);
-
+            /**
+             * should define metadata before `initialize` method call,
+             * because of cases when other methods will called inside `initialize` method
+             */
             Reflect.defineMetadata(initializedTargetMetadataKey, true, this);
 
+            const result: any = originalMethod.apply(this, arguments);
+
             if (this[propertyKey]) {}
 
             return result;

+ 1 - 4
src/storages/custom-node-group/CustomNodeGroupStorage.ts

@@ -46,8 +46,7 @@ export class CustomNodeGroupStorage extends MapStorage <ICustomNodeGroup> {
 
     @postConstruct()
     public initialize (): void {
-        this.storage = new Map <string, ICustomNodeGroup>();
-        this.storageId = this.randomGenerator.getRandomString(6);
+        super.initialize();
 
         CustomNodeGroupStorage.customNodeGroupsList.forEach((customNodeGroupName: CustomNodeGroup) => {
             const customNodeGroup: ICustomNodeGroup = this.customNodeGroupFactory(
@@ -60,7 +59,5 @@ export class CustomNodeGroupStorage extends MapStorage <ICustomNodeGroup> {
 
             this.storage.set(customNodeGroupName, customNodeGroup);
         });
-
-        console.log(123, this.constructor.name);
     }
 }

+ 52 - 3
test/unit-tests/decorators/initializable/Initializable.spec.ts

@@ -50,12 +50,61 @@ describe('@initializable', () => {
                     foo.bar();
                 };
 
-                it('should throws an error if `initialize` method wasn\'t called first', () => {
+                it('shouldn\'t throw an error if `initialize` method was called first', () => {
                     assert.doesNotThrow(testFunc, /Class should be initialized/);
                 });
             });
 
-            describe('variant #2: `initialize` method wasn\'t called first', () => {
+            describe('variant #2: other method was called inside `initialize` method with initialization of the property', () => {
+                const testFunc: () => void = () => {
+                    class Foo {
+                        @initializable()
+                        public property!: string;
+
+                        public initialize (property: string): void {
+                            this.innerInitialize(property);
+                        }
+
+                        public innerInitialize (property: string): void {
+                            this.property = property;
+                        }
+                    }
+
+                    const foo: Foo = new Foo();
+
+                    foo.initialize('baz');
+                };
+
+                it('shouldn\'t throw an error if other method was called inside `initialize` method', () => {
+                    assert.doesNotThrow(testFunc, /Class should be initialized/);
+                });
+            });
+
+            describe('variant #3: other method was called inside `initialize` method without initialization of the property', () => {
+                const testFunc: () => void = () => {
+                    class Foo {
+                        @initializable()
+                        public property!: string;
+
+                        public initialize (property: string): void {
+                            this.innerInitialize(property);
+                        }
+
+                        public innerInitialize (property: string): void {
+                        }
+                    }
+
+                    const foo: Foo = new Foo();
+
+                    foo.initialize('baz');
+                };
+
+                it('should throws an error if other method was called inside `initialize` method without initialization of the property', () => {
+                    assert.throws(testFunc, /Property `property` is not initialized/);
+                });
+            });
+
+            describe('variant #4: `initialize` method wasn\'t called first', () => {
                 const testFunc: () => void = () => {
                     class Foo {
                         @initializable()
@@ -79,7 +128,7 @@ describe('@initializable', () => {
                 });
             });
 
-            describe('variant #3: `initialize` method wasn\'t called', () => {
+            describe('variant #5: `initialize` method wasn\'t called', () => {
                 const testFunc: () => void = () => {
                     class Foo {
                         @initializable()

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff