diff --git a/spec/exampleclass.ts b/spec/exampleclass.ts new file mode 100644 index 0000000..0428bc1 --- /dev/null +++ b/spec/exampleclass.ts @@ -0,0 +1,11 @@ +import TinyDiInjectableBase from '../dist/injectable'; + +export default class MyClass extends TinyDiInjectableBase { + constructor(private dep: any) { + super(); + } +} +MyClass.$inject = { + deps: ['dependency'], + callAs: 'class' +}; diff --git a/spec/typescript.spec.ts b/spec/typescript.spec.ts new file mode 100644 index 0000000..0b6058e --- /dev/null +++ b/spec/typescript.spec.ts @@ -0,0 +1,28 @@ +/// +import TinyDiInjectableBase from '../dist/injectable'; + +describe('typescript class', () => { + it('can define $inject property', () => { + class MyClass extends TinyDiInjectableBase {} + MyClass.$inject = ['dependency']; + expect(MyClass.$inject).toBeDefined(); + }); + + it('can be loaded by tiny-di', () => { + let tiny = require('../dist'); + let injector: TinyDiInjector = new tiny(); + injector.setResolver(resolver); + injector.bind('dependency').to('exampleDep'); + injector.bind('myClassInstance').load(__dirname + '/exampleclass.js'); + + let instance = injector.get('myClassInstance'); + expect(instance.dep).toEqual('exampleDep'); + }); +}); + +/** + * define resolver to load default exported classes + */ +function resolver(module: string) { + return require(module).default; +} diff --git a/src/injectable.ts b/src/injectable.ts index fc20443..867056a 100644 --- a/src/injectable.ts +++ b/src/injectable.ts @@ -1,5 +1,5 @@ export default class TinyDiInjectableBase { - public static $inject: string[] | { + public static $inject: string[] | { deps: string[], callAs: 'class' | 'function' }; diff --git a/tsconfig.spec.json b/tsconfig.spec.json new file mode 100644 index 0000000..d20422a --- /dev/null +++ b/tsconfig.spec.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "noImplicitAny": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "module": "commonjs", + "sourceMap": true, + "declaration": true, + "target": "es5", + "outDir": "buildspec" + }, + + "exclude": [ + "node_modules", + "src", + "buildspec" + ] +}