fix(ivy): account for `useValue: undefined` providers in module injector (#27035)

PR Close #27035
This commit is contained in:
JoostK 2018-11-21 19:45:49 +01:00 committed by Alex Rickabaugh
parent 75723d5c89
commit 6552471c49
2 changed files with 17 additions and 2 deletions

View File

@ -248,7 +248,7 @@ export class R3Injector {
// Track the InjectorType and add a provider for it. // Track the InjectorType and add a provider for it.
this.injectorDefTypes.add(defType); this.injectorDefTypes.add(defType);
this.records.set(defType, makeRecord(def.factory)); this.records.set(defType, makeRecord(def.factory, NOT_YET));
// Add providers in the same way that @NgModule resolution did: // Add providers in the same way that @NgModule resolution did:
@ -391,7 +391,7 @@ export function providerToFactory(provider: SingleProvider): () => any {
} }
function makeRecord<T>( function makeRecord<T>(
factory: (() => T) | undefined, value: T | {} = NOT_YET, multi: boolean = false): Record<T> { factory: (() => T) | undefined, value: T | {}, multi: boolean = false): Record<T> {
return { return {
factory: factory, factory: factory,
value: value, value: value,

View File

@ -44,6 +44,9 @@ describe('InjectorDef-based createInjector()', () => {
const LOCALE = new InjectionToken<string[]>('LOCALE'); const LOCALE = new InjectionToken<string[]>('LOCALE');
const PRIMITIVE_VALUE = new InjectionToken<string>('PRIMITIVE_VALUE');
const UNDEFINED_VALUE = new InjectionToken<undefined>('UNDEFINED_VALUE');
class ServiceWithDep { class ServiceWithDep {
constructor(readonly service: Service) {} constructor(readonly service: Service) {}
@ -127,6 +130,8 @@ describe('InjectorDef-based createInjector()', () => {
ServiceWithMultiDep, ServiceWithMultiDep,
{provide: LOCALE, multi: true, useValue: 'en'}, {provide: LOCALE, multi: true, useValue: 'en'},
{provide: LOCALE, multi: true, useValue: 'es'}, {provide: LOCALE, multi: true, useValue: 'es'},
{provide: PRIMITIVE_VALUE, useValue: 'foo'},
{provide: UNDEFINED_VALUE, useValue: undefined},
Service, Service,
{provide: SERVICE_TOKEN, useExisting: Service}, {provide: SERVICE_TOKEN, useExisting: Service},
CircularA, CircularA,
@ -204,6 +209,16 @@ describe('InjectorDef-based createInjector()', () => {
expect(instance).toBe(injector.get(Service)); expect(instance).toBe(injector.get(Service));
}); });
it('injects a useValue token with a primitive value', () => {
const value = injector.get(PRIMITIVE_VALUE);
expect(value).toEqual('foo');
});
it('injects a useValue token with value undefined', () => {
const value = injector.get(UNDEFINED_VALUE);
expect(value).toBeUndefined();
});
it('instantiates a class with useClass and deps', () => { it('instantiates a class with useClass and deps', () => {
const instance = injector.get(STATIC_TOKEN); const instance = injector.get(STATIC_TOKEN);
expect(instance instanceof StaticService).toBeTruthy(); expect(instance instanceof StaticService).toBeTruthy();