Skip to content

Commit

Permalink
Instantiate controllers, added controllers decorator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
notmedia committed Sep 6, 2021
1 parent b491c34 commit 8fd7716
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/common/test/decorators/core/controller.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'reflect-metadata';

import { Controller, Scope } from '../../../lib';

describe('@Controller', () => {
it('should create injectable class with no options', () => {
@Controller()
class Test {}

expect(Reflect.getOwnMetadata('horn:controller', Test)).toStrictEqual({
scope: Scope.SINGLETON,
});
});

it('should create injectable class with options', () => {
@Controller({
scope: Scope.SINGLETON,
})
class Test {}

expect(Reflect.getOwnMetadata('horn:controller', Test)).toStrictEqual({
scope: Scope.SINGLETON,
});
});
});
29 changes: 29 additions & 0 deletions packages/core/lib/di/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export class Module {
}

public instantiate() {
this.instantiateInjectables();
this.instantiateControllers();
}

private instantiateInjectables() {
// eslint-disable-next-line no-restricted-syntax
for (const [, injectable] of this.injectables.entries()) {
const dependencyRefs = injectable.getDependencies();
Expand Down Expand Up @@ -105,4 +110,28 @@ export class Module {

return injectable;
}

private instantiateControllers() {
// eslint-disable-next-line no-restricted-syntax
for (const [, controller] of this.controllers.entries()) {
const dependencyRefs = controller.getDependencies();
const dependencies = [];

for (let index = 0; index < dependencyRefs.length; index++) {
const dependency = this.findDepenendency(dependencyRefs[index]);

if (!dependency) {
throw new ResolveDependencyError(
dependencyRefs[index].name,
controller.getName(),
this.name
);
}

dependencies.push(dependency.getInstance());
}

controller.instantiate(dependencies);
}
}
}

0 comments on commit 8fd7716

Please sign in to comment.