Skip to content

Commit

Permalink
fixup! fix(ivy): ensure overrides for 'multi: true' only appear once …
Browse files Browse the repository at this point in the history
…in final providers
  • Loading branch information
atscott committed Oct 18, 2019
1 parent f3d3f7f commit 8255771
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
25 changes: 23 additions & 2 deletions packages/core/test/test_bed_spec.ts
Expand Up @@ -225,10 +225,31 @@ describe('TestBed', () => {
});

it('allow to override a provider', () => {
TestBed.overrideProvider(NAME, {useValue: 'injected World !'});
TestBed.overrideProvider(NAME, {useValue: 'injected World!'});
const hello = TestBed.createComponent(HelloWorld);
hello.detectChanges();
expect(hello.nativeElement).toHaveText('Hello injected World !');
expect(hello.nativeElement).toHaveText('Hello injected World!');
});

it('uses the most recent provider override', () => {
TestBed.overrideProvider(NAME, {useValue: 'injected World!'});
TestBed.overrideProvider(NAME, {useValue: 'injected World a second time!'});
const hello = TestBed.createComponent(HelloWorld);
hello.detectChanges();
expect(hello.nativeElement).toHaveText('Hello injected World a second time!');
});

it('overrides a providers in an array', () => {
TestBed.configureTestingModule({
imports: [HelloWorldModule],
providers: [
[{provide: NAME, useValue: 'injected World!'}],
]
});
TestBed.overrideProvider(NAME, {useValue: 'injected World a second time!'});
const hello = TestBed.createComponent(HelloWorld);
hello.detectChanges();
expect(hello.nativeElement).toHaveText('Hello injected World a second time!');
});

describe('allow override of multi provider', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/testing/src/r3_test_bed_compiler.ts
Expand Up @@ -620,8 +620,9 @@ export class R3TestBedCompiler {
private getOverriddenProviders(providers?: Provider[]): Provider[] {
if (!providers || !providers.length || this.providerOverridesByToken.size === 0) return [];

const overrides = this.getProviderOverrides(providers);
const overriddenProviders = [...providers, ...overrides];
const flattenedProviders = flatten<Provider[]>(providers);
const overrides = this.getProviderOverrides(flattenedProviders);
const overriddenProviders = [...flattenedProviders, ...overrides];
const tokenToProviderMap: Map<any, Provider> = new Map();

// Iterate through providers from the end so only the most recent provider is used for a given
Expand Down

0 comments on commit 8255771

Please sign in to comment.