From 6552471c493cc69aa19012ddc03da9bee6fb5deb Mon Sep 17 00:00:00 2001 From: JoostK Date: Wed, 21 Nov 2018 19:45:49 +0100 Subject: [PATCH] fix(ivy): account for `useValue: undefined` providers in module injector (#27035) PR Close #27035 --- packages/core/src/di/r3_injector.ts | 4 ++-- packages/core/test/di/r3_injector_spec.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/core/src/di/r3_injector.ts b/packages/core/src/di/r3_injector.ts index e0da52a14d..0c67906526 100644 --- a/packages/core/src/di/r3_injector.ts +++ b/packages/core/src/di/r3_injector.ts @@ -248,7 +248,7 @@ export class R3Injector { // Track the InjectorType and add a provider for it. 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: @@ -391,7 +391,7 @@ export function providerToFactory(provider: SingleProvider): () => any { } function makeRecord( - factory: (() => T) | undefined, value: T | {} = NOT_YET, multi: boolean = false): Record { + factory: (() => T) | undefined, value: T | {}, multi: boolean = false): Record { return { factory: factory, value: value, diff --git a/packages/core/test/di/r3_injector_spec.ts b/packages/core/test/di/r3_injector_spec.ts index b91a4376e5..1c7e94505f 100644 --- a/packages/core/test/di/r3_injector_spec.ts +++ b/packages/core/test/di/r3_injector_spec.ts @@ -44,6 +44,9 @@ describe('InjectorDef-based createInjector()', () => { const LOCALE = new InjectionToken('LOCALE'); + const PRIMITIVE_VALUE = new InjectionToken('PRIMITIVE_VALUE'); + const UNDEFINED_VALUE = new InjectionToken('UNDEFINED_VALUE'); + class ServiceWithDep { constructor(readonly service: Service) {} @@ -127,6 +130,8 @@ describe('InjectorDef-based createInjector()', () => { ServiceWithMultiDep, {provide: LOCALE, multi: true, useValue: 'en'}, {provide: LOCALE, multi: true, useValue: 'es'}, + {provide: PRIMITIVE_VALUE, useValue: 'foo'}, + {provide: UNDEFINED_VALUE, useValue: undefined}, Service, {provide: SERVICE_TOKEN, useExisting: Service}, CircularA, @@ -204,6 +209,16 @@ describe('InjectorDef-based createInjector()', () => { 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', () => { const instance = injector.get(STATIC_TOKEN); expect(instance instanceof StaticService).toBeTruthy();