ng-metadata
ng-metadata copied to clipboard
feature request: Expose $inject helper
It would be helpful if there was a public function which could set the $inject property on a function. This would allow using type annotations when interacting with classic Angular 1 apis.
// In my.component.ts
@Component({ selector: 'my-component' })
class MyComponent {
constructor(depService: MyDependency) {
// ...
}
}
// In my.component.spec.ts
angular.mock.inject(($controller) => {
let component = $controller(annotate(MyComponent)); // Needs $inject annotated
}
This would mainly expose a way to set $inject
to the result of the _dependenciesFor()
method.
you can do something like:
// In my.component.ts
@Component({ selector: 'my-component' })
class MyComponent {
constructor(depService: MyDependency) {
// ...
}
}
// In my.component.spec.ts
import {provide} from 'ng-metadata/core';
angular.mock.inject(($controller) => {
const [compName,annotatedComponent] = provide(MyComponent);
let component = $controller(annotatedComponent); // Needs $inject annotated
}
although I highly recommend to not use this old Angular 1 apis. if you already have a component via ngMetadata @Component
rather use IRender
interface or test the component as vanilla js:
const depService = new MyDependency();
const component = new MyComponent(depService);
https://hotell.gitbooks.io/ng-metadata/content/docs/api/testing/function.html
will this work for you @aciccarello ?
Sorry for not replying earlier. I tried using the provide function earlier but had some problems. I'll try that again (hopefully tomorrow) and see if I can make it work.
As a workaround for now I've been manually initializing the component class.
The provide
function returns a directiveFactory function, not the annotated controller. I'd like to test the controller without compiling the template (or using $scope
) so I'll have to stick with manually initialize it for now.
I haven't looked at it too much but I think the TestComponentBuilder is how the component would be initialized in ng2. It probably should be a goal for #93. It's marked as stable but who knows :unamused:
Note: I'm currently using ng-metadata 1.12