From ac26d81b83feda6538b172f1927c2f9299c3ad46 Mon Sep 17 00:00:00 2001 From: MG Date: Sat, 2 Jan 2021 10:16:15 +0100 Subject: [PATCH] fix: declarations w/o selectors are reachable with ngMocks helpers closes #266 --- lib/mock-render/mock-render.ts | 11 +++++++++++ tests/issue-266/test.spec.ts | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/mock-render/mock-render.ts b/lib/mock-render/mock-render.ts index b6cfb440e1..73227e2f53 100644 --- a/lib/mock-render/mock-render.ts +++ b/lib/mock-render/mock-render.ts @@ -33,6 +33,17 @@ const applyParamsToFixtureInstance = ( const registerTemplateMiddleware = (template: AnyType, meta: Directive): void => { const child = extendClass(template); + + let providers = meta.providers || []; + providers = [ + ...providers, + { + provide: template, + useExisting: child, + }, + ]; + meta.providers = providers; + if (isNgDef(template, 'c')) { Component(meta)(child); } else { diff --git a/tests/issue-266/test.spec.ts b/tests/issue-266/test.spec.ts index 1c50b3dfb2..d7d2e7c698 100644 --- a/tests/issue-266/test.spec.ts +++ b/tests/issue-266/test.spec.ts @@ -7,7 +7,7 @@ import { Pipe, PipeTransform, } from '@angular/core'; -import { MockBuilder, MockRender } from 'ng-mocks'; +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; @Component({ template: 'target', @@ -60,6 +60,11 @@ describe('issue-248', () => { ); expect(fixture.point.componentInstance.name).toEqual('component'); expect(fixture.point.nativeElement.innerHTML).toEqual('target'); + + expect(ngMocks.find(TargetComponent)).toBe(fixture.point); + expect(ngMocks.findInstance(TargetComponent)).toBe( + fixture.point.componentInstance, + ); }); it('renders directives', () => { @@ -82,6 +87,11 @@ describe('issue-248', () => { jasmine.any(TargetDirective), ); expect(fixture.point.componentInstance.value).toEqual('321'); + + expect(ngMocks.find(TargetDirective)).toBe(fixture.point); + expect(ngMocks.findInstance(TargetDirective)).toBe( + fixture.point.componentInstance, + ); }); it('fails on not provided pipes', () => {