fix(ivy): support forward refs in provider deps (#30201)
Currently, we are not properly resolving forward refs when they appear in deps for providers created with the useFactory strategy. This commit wraps provider deps in the resolveForwardRef call so the tokens are passed into the inject function as expected. PR Close #30201
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Component, Directive, Inject, Injectable, InjectionToken} from '@angular/core';
|
||||
import {Component, Directive, Inject, Injectable, InjectionToken, NgModule, forwardRef} from '@angular/core';
|
||||
import {TestBed, async, inject} from '@angular/core/testing';
|
||||
import {By} from '@angular/platform-browser';
|
||||
import {onlyInIvy} from '@angular/private/testing';
|
||||
@ -283,4 +283,42 @@ describe('providers', () => {
|
||||
})));
|
||||
});
|
||||
});
|
||||
|
||||
describe('forward refs', () => {
|
||||
|
||||
it('should support forward refs in provider deps', () => {
|
||||
class MyService {
|
||||
constructor(public dep: {value: string}) {}
|
||||
}
|
||||
|
||||
class OtherService {
|
||||
value = 'one';
|
||||
}
|
||||
|
||||
@Component({selector: 'app-comp', template: ``})
|
||||
class AppComp {
|
||||
constructor(public myService: MyService) {}
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
providers: [
|
||||
OtherService, {
|
||||
provide: MyService,
|
||||
useFactory: (dep: {value: string}) => new MyService(dep),
|
||||
deps: [forwardRef(() => OtherService)]
|
||||
}
|
||||
],
|
||||
declarations: [AppComp]
|
||||
})
|
||||
class MyModule {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({imports: [MyModule]});
|
||||
|
||||
const fixture = TestBed.createComponent(AppComp);
|
||||
expect(fixture.componentInstance.myService.dep.value).toBe('one');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user