diff --git a/libs/ng-mocks/src/lib/mock/clone-providers.ts b/libs/ng-mocks/src/lib/mock/clone-providers.ts index 1fa60f568e..0534e7cecb 100644 --- a/libs/ng-mocks/src/lib/mock/clone-providers.ts +++ b/libs/ng-mocks/src/lib/mock/clone-providers.ts @@ -49,7 +49,7 @@ const processTokens = (mockType: AnyType, provider: any) => { const processOwnUseExisting = (sourceType: AnyType, mockType: AnyType, provider: any) => { const provide = funcGetProvider(provider); if (provider !== provide && provider.useExisting === sourceType) { - return toExistingProvider(provider, mockType); + return toExistingProvider(provide, mockType); } if ( provider !== provide && diff --git a/tests/ng-mocks-render/use-existing.spec.ts b/tests/ng-mocks-render/use-existing.spec.ts new file mode 100644 index 0000000000..369f275efe --- /dev/null +++ b/tests/ng-mocks-render/use-existing.spec.ts @@ -0,0 +1,72 @@ +import { CommonModule } from '@angular/common'; +import { + Component, + ContentChild, + Directive, + Input, + NgModule, + TemplateRef, +} from '@angular/core'; +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; + +@Directive({ + selector: '[tpl]', +}) +class TplDirective { + @Input('tpl') public readonly name: string | null = null; + + public constructor(public readonly tpl: TemplateRef) {} +} + +@Directive({ + providers: [ + { + provide: TplDirective, + useExisting: MockDirective, + }, + ], + selector: '[mock]', +}) +class MockDirective { + public constructor(public readonly tpl: TemplateRef) {} +} + +@Component({ + selector: 'target', + template: ` + + rendered-mock + + `, +}) +class TargetComponent {} + +@Component({ + selector: 'component', + template: ``, +}) +class MockComponent { + @ContentChild(TplDirective, {} as any) + public readonly directive?: TplDirective; +} + +@NgModule({ + declarations: [ + TargetComponent, + MockComponent, + MockDirective, + TplDirective, + ], + imports: [CommonModule], +}) +class TargetModule {} + +describe('ng-mocks-render:use-existing', () => { + beforeEach(() => MockBuilder(TargetComponent, TargetModule)); + + it('substitutes in mocks correctly', () => { + MockRender(TargetComponent); + const component = ngMocks.findInstance(MockComponent); + expect(component.directive).toEqual(jasmine.any(MockDirective)); + }); +});