diff --git a/packages/core/src/render3/di.ts b/packages/core/src/render3/di.ts index 87885c8436..2ea1df8f46 100644 --- a/packages/core/src/render3/di.ts +++ b/packages/core/src/render3/di.ts @@ -60,7 +60,7 @@ import {findComponentView, getParentInjectorIndex, getParentInjectorView, hasPar * * ``` */ -let includeViewProviders = false; +let includeViewProviders = true; function setIncludeViewProviders(v: boolean): boolean { const oldValue = includeViewProviders; diff --git a/packages/core/test/linker/view_injector_integration_spec.ts b/packages/core/test/linker/view_injector_integration_spec.ts index d65a7b508e..1202b0c522 100644 --- a/packages/core/test/linker/view_injector_integration_spec.ts +++ b/packages/core/test/linker/view_injector_integration_spec.ts @@ -292,21 +292,19 @@ class TestComp { expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2'); }); - fixmeIvy( - 'FW-889: Element injector cannot access the viewProviders of the component to which it belongs') - .it('should instantiate viewProviders that have dependencies', () => { - TestBed.configureTestingModule({declarations: [SimpleComponent]}); - const viewProviders = [ - {provide: 'injectable1', useValue: 'injectable1'}, { - provide: 'injectable2', - useFactory: (val: any) => `${val}-injectable2`, - deps: ['injectable1'] - } - ]; - TestBed.overrideComponent(SimpleComponent, {set: {viewProviders}}); - const el = createComponent('
'); - expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2'); - }); + it('should instantiate viewProviders that have dependencies', () => { + TestBed.configureTestingModule({declarations: [SimpleComponent]}); + const viewProviders = [ + {provide: 'injectable1', useValue: 'injectable1'}, { + provide: 'injectable2', + useFactory: (val: any) => `${val}-injectable2`, + deps: ['injectable1'] + } + ]; + TestBed.overrideComponent(SimpleComponent, {set: {viewProviders}}); + const el = createComponent('
'); + expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2'); + }); it('should instantiate components that depend on viewProviders providers', () => { TestBed.configureTestingModule({declarations: [NeedsServiceComponent]}); @@ -500,22 +498,20 @@ class TestComp { expect(destroyed).toBe(true); }); - fixmeIvy( - 'FW-889: Element injector cannot access the viewProviders of the component to which it belongs') - .it('should instantiate view providers lazily', () => { - let created = false; - TestBed.configureTestingModule({declarations: [SimpleComponent]}); - TestBed.overrideComponent( - SimpleComponent, - {set: {viewProviders: [{provide: 'service', useFactory: () => created = true}]}}); - const el = createComponent('
'); + it('should instantiate view providers lazily', () => { + let created = false; + TestBed.configureTestingModule({declarations: [SimpleComponent]}); + TestBed.overrideComponent( + SimpleComponent, + {set: {viewProviders: [{provide: 'service', useFactory: () => created = true}]}}); + const el = createComponent('
'); - expect(created).toBe(false); + expect(created).toBe(false); - el.children[0].injector.get('service'); + el.children[0].injector.get('service'); - expect(created).toBe(true); - }); + expect(created).toBe(true); + }); it('should not instantiate other directives that depend on viewProviders providers (same element)', () => { diff --git a/packages/core/test/render3/providers_spec.ts b/packages/core/test/render3/providers_spec.ts index b5cedd99c4..a585ae1e67 100644 --- a/packages/core/test/render3/providers_spec.ts +++ b/packages/core/test/render3/providers_spec.ts @@ -1188,7 +1188,11 @@ describe('providers', () => { static ngInjectableDef = defineInjectable({factory: () => new SomeInj(inject(String))}); } - @Component({template: `

`, providers: [{provide: String, useValue: 'From my component'}]}) + @Component({ + template: `

`, + providers: [{provide: String, useValue: 'From my component'}], + viewProviders: [{provide: Number, useValue: 123}] + }) class MyComponent { constructor() {} @@ -1204,7 +1208,9 @@ describe('providers', () => { } }, features: [ - ProvidersFeature([{provide: String, useValue: 'From my component'}]), + ProvidersFeature( + [{provide: String, useValue: 'From my component'}], + [{provide: Number, useValue: 123}]), ], }); } @@ -1237,12 +1243,24 @@ describe('providers', () => { }); } - it('should work', () => { + it('should work from within the template', () => { const fixture = new ComponentFixture(AppComponent); expect(fixture.html).toEqual('

'); const p = fixture.hostElement.querySelector('p'); const injector = getInjector(p as any); + expect(injector.get(Number)).toEqual(123); + expect(injector.get(String)).toEqual('From my component'); + expect(injector.get(Some).location).toEqual('From app component'); + }); + + it('should work from the host of the component', () => { + const fixture = new ComponentFixture(AppComponent); + expect(fixture.html).toEqual('

'); + + const myCmp = fixture.hostElement.querySelector('my-cmp'); + const injector = getInjector(myCmp as any); + expect(injector.get(Number)).toEqual(123); expect(injector.get(String)).toEqual('From my component'); expect(injector.get(Some).location).toEqual('From app component'); });