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 11, 2019
1 parent 828356e commit 69b674d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
28 changes: 21 additions & 7 deletions packages/core/test/test_bed_spec.ts
Expand Up @@ -231,7 +231,7 @@ describe('TestBed', () => {
expect(hello.nativeElement).toHaveText('Hello injected World !');
});

it('allow to override multi provider', () => {
describe('allow override of multi provider', () => {
const token = new InjectionToken<string[]>('token');
@NgModule({providers: [{provide: token, useValue: 'valueFromModule', multi: true}]})
class MyModule {
Expand All @@ -241,13 +241,27 @@ describe('TestBed', () => {
class MyModule2 {
}

TestBed.configureTestingModule({imports: [MyModule, MyModule2]});
const overrideValue = ['override'];
TestBed.overrideProvider(token, { useValue: overrideValue, multi: true } as any);
beforeEach(() => { TestBed.configureTestingModule({imports: [MyModule, MyModule2]}); });

it('with an array', () => {
const overrideValue = ['override'];
TestBed.overrideProvider(token, { useValue: overrideValue, multi: true } as any);

const value = TestBed.inject(token);
expect(value.length).toEqual(overrideValue.length);
expect(value).toEqual(overrideValue);
});

it('with a non-array', () => {
// This is actually invalid because multi providers return arrays. We have this here so we can
// ensure Ivy behaves the same as VE does currently.
const overrideValue = 'override';
TestBed.overrideProvider(token, { useValue: overrideValue, multi: true } as any);

const value = TestBed.inject(token);
expect(value.length).toEqual(overrideValue.length);
expect(value).toEqual(overrideValue);
const value = TestBed.inject(token);
expect(value.length).toEqual(overrideValue.length);
expect(value).toEqual(overrideValue as {} as string[]);
});
});

it('should allow overriding a provider defined via ModuleWithProviders (using TestBed.overrideProvider)',
Expand Down
15 changes: 9 additions & 6 deletions packages/core/testing/src/r3_test_bed_compiler.ts
Expand Up @@ -154,12 +154,15 @@ export class R3TestBedCompiler {
overrideProvider(
token: any,
provider: {useFactory?: Function, useValue?: any, deps?: any[], multi?: boolean}): void {
const providerDef = provider.useFactory ? {
provide: token,
useFactory: provider.useFactory,
deps: provider.deps || [],
} :
{provide: token, useValue: provider.useValue};
// Note, we explicitly avoid setting `multi: true` here. If multi is true and we set it, TestBed
// will override
// all instances of the token and when the test module is finalized, the injector will add each
// occurrence of the
// override to the records. We only ever want one when it's overridden, so we treat it as
// non-multi.
const providerDef = provider.useFactory ?
{provide: token, useFactory: provider.useFactory, deps: provider.deps || []} :
{provide: token, useValue: provider.useValue};

let injectableDef: InjectableDef<any>|null;
const isRoot =
Expand Down

0 comments on commit 69b674d

Please sign in to comment.