Skip to content

Commit

Permalink
Add ModuleLoader to get controllers
Browse files Browse the repository at this point in the history
Removes linting errors due to injectors being added at runtime
  • Loading branch information
kpfromer committed May 9, 2018
1 parent 1a4c0ae commit 8d2ffa5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/module/module.loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as module from './module';
import { ModuleLoader } from './module.loader';

describe('ModuleLoader', () => {
describe('getController', () => {
it('should throw error if module given is invalid', () => {
jest.spyOn(module, 'isModule').mockReturnValue(false);

class InvalidModule {}
class WantedController {}

const handler = () => {
ModuleLoader.getController(InvalidModule, WantedController);
};

expect(handler).toThrow(TypeError);
});

it('should get controller from module', () => {
jest.spyOn(module, 'isModule').mockReturnValue(true);

const mockController = jest.fn();

class WantedController {}
class Module {
static getController() {
return mockController;
}
}

const controller = ModuleLoader.getController(Module, WantedController);

expect(controller).toBe(mockController);
});
});
});
11 changes: 11 additions & 0 deletions src/module/module.loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isModule } from './module';

export class ModuleLoader {
static getController<T>(module: any, controller: new (...args) => T): T {
if (!isModule(module)) {
throw new TypeError('Invalid Module');
}

return module.getController(controller);
}
}
2 changes: 1 addition & 1 deletion src/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isController } from '../controller/controller';
import { MODULE_METADATA } from '../constants';

interface IModule {
getController<T>(): new (...args) => T;
getController<T>(contoller: new (...args) => T): T;
getExports(): (Type<any> | Provider<any>)[];
}

Expand Down

0 comments on commit 8d2ffa5

Please sign in to comment.