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 · 2 comments

Comments

Projects
None yet
2 participants
@yjaaidi
Contributor

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

Contributor

yjaaidi commented May 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment