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

Can't override providers while unit-testing downgraded angular 2 components. #8675

Closed
yjaaidi opened this issue May 16, 2016 · 3 comments
Closed

Comments

@yjaaidi
Copy link
Contributor

@yjaaidi yjaaidi commented May 16, 2016

Hi!

I'm having an issue while trying to mock Http requests for Ng2 components downgraded to Ng1.

With pure Ng2, I am able to to override providers easily using TestComponentBuilder.overrideProviders or MockDirectiveResolver.setProvidersOverride.

But I can't find a way to override providers on an Ng1/Ng2 hybrid app using UpgradeAdapter.
With UpgradeAdapter.addProvider, it only works if the Http provider is never defined but if it is already defined in @Component({providers: [HTTP_PROVIDERS]})... there seems to be no way to override it.

Here is an example where the mocking works if the provider is not defined in the component:
http://embed.plnkr.co/7ursstPKCiXubHWnEZdj/

You can then produce the problem by uncommenting the line at src/main.ts:36.

Thank you in advance!

@yjaaidi
Copy link
Contributor Author

@yjaaidi yjaaidi commented May 17, 2016

Yeahhaa !

Found the trick! You have to inject the mock DirectiveProvider into the updateAdapter.

it('should override component providers', inject([DirectiveResolver], (
  mockDirectiveResolver: MockDirectiveResolver
) => {
  ...

  mockDirectiveResolver.setProvidersOverride(MyComponent, [
    provide(XHRBackend, {useFactory: () => mockBackend})
  ])

  upgradeAdapter.addProvider(
    provide(DirectiveResolver, {
      useFactory: () => mockDirectiveResolver
    })
  )

  ...
}
@mhevery mhevery closed this May 17, 2016
@yjaaidi
Copy link
Contributor Author

@yjaaidi yjaaidi commented May 18, 2016

@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Sep 8, 2019

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants