ng-metadata icon indicating copy to clipboard operation
ng-metadata copied to clipboard

feature request: Expose $inject helper

Open aciccarello opened this issue 8 years ago • 5 comments

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
}

aciccarello avatar Jul 01 '16 19:07 aciccarello

This would mainly expose a way to set $inject to the result of the _dependenciesFor() method.

aciccarello avatar Jul 05 '16 18:07 aciccarello

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

Hotell avatar Jul 10 '16 13:07 Hotell

will this work for you @aciccarello ?

Hotell avatar Jul 13 '16 22:07 Hotell

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.

aciccarello avatar Jul 13 '16 22:07 aciccarello

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

aciccarello avatar Jul 14 '16 22:07 aciccarello