Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: Expose $inject helper #115

Open
aciccarello opened this issue Jul 1, 2016 · 5 comments
Open

feature request: Expose $inject helper #115

aciccarello opened this issue Jul 1, 2016 · 5 comments
Labels

Comments

@aciccarello
Copy link
Collaborator

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
Copy link
Collaborator Author

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

@Hotell
Copy link
Member

Hotell commented Jul 10, 2016

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
Copy link
Member

Hotell commented Jul 13, 2016

will this work for you @aciccarello ?

@aciccarello
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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 😒

Note: I'm currently using ng-metadata 1.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants