From 9cee2471dcee0459e0cdd3ea1e4331a6073b45cd Mon Sep 17 00:00:00 2001 From: Michael Gusev Date: Wed, 31 Oct 2018 16:48:51 +0100 Subject: [PATCH] Respect of @ContentChild in mocked template --- lib/mock-component/mock-component.spec.ts | 31 +++++++++++++++++++ lib/mock-component/mock-component.ts | 16 ++++++++-- .../template-outlet.component.ts | 14 +++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 lib/mock-component/test-components/template-outlet.component.ts diff --git a/lib/mock-component/mock-component.spec.ts b/lib/mock-component/mock-component.spec.ts index d56eba0310..c7228b73af 100644 --- a/lib/mock-component/mock-component.spec.ts +++ b/lib/mock-component/mock-component.spec.ts @@ -7,6 +7,7 @@ import { ChildComponent } from './test-components/child-component.component'; import { CustomFormControlComponent } from './test-components/custom-form-control.component'; import { EmptyComponent } from './test-components/empty-component.component'; import { SimpleComponent } from './test-components/simple-component.component'; +import { TemplateOutletComponent } from './test-components/template-outlet.component'; @Component({ selector: 'example-component-container', @@ -23,6 +24,12 @@ import { SimpleComponent } from './test-components/simple-component.component'; doh + + ng-content body header +
block 1 body
+ block 2 body + ng-content body footer +
` }) export class ExampleComponentContainer { @@ -45,6 +52,7 @@ describe('MockComponent', () => { ExampleComponentContainer, MockComponents(EmptyComponent, SimpleComponent, + TemplateOutletComponent, ChildComponent, CustomFormControlComponent), ], @@ -159,4 +167,27 @@ describe('MockComponent', () => { expect(component.formControl.value).toBe('foo'); }); }); + + describe('NgTemplateOutlet', () => { + it('renders all @ContentChild properties and ngContent too', () => { + fixture.detectChanges(); + const templateOutlet = fixture.debugElement.query(By.directive(MockComponent(TemplateOutletComponent))); + expect(templateOutlet).toBeDefined(); + + // looking for ng-content. + const ngContent = templateOutlet.query(By.css('[data-key="ng-content"]')); + expect(ngContent).toBeTruthy(); + expect(ngContent.nativeElement.innerText).toEqual('ng-content body header ng-content body footer'); + + // looking for 1st templateRef. + const block1 = templateOutlet.query(By.css('[data-key="block1"]')); + expect(block1).toBeTruthy(); + expect(block1.nativeElement.innerText).toEqual('block 1 body'); + + // looking for 2nd templateRef. + const block2 = templateOutlet.query(By.css('[data-key="block2"]')); + expect(block2).toBeTruthy(); + expect(block2.nativeElement.innerText).toEqual('block 2 body'); + }); + }); }); diff --git a/lib/mock-component/mock-component.ts b/lib/mock-component/mock-component.ts index ba99467668..473448200a 100644 --- a/lib/mock-component/mock-component.ts +++ b/lib/mock-component/mock-component.ts @@ -1,4 +1,4 @@ -import { Component, EventEmitter, forwardRef, Type } from '@angular/core'; +import { Component, EventEmitter, forwardRef, Query, Type } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { MockOf } from '../common'; import { directiveResolver } from '../common/reflect'; @@ -21,7 +21,16 @@ export function MockComponent(component: Type): Type; } - const { exportAs, inputs, outputs, selector } = directiveResolver.resolve(component); + const { exportAs, inputs, outputs, queries, selector } = directiveResolver.resolve(component); + + let template = '
'; + if (queries) { + template += Object.keys(queries) + .map((key: string) => { + const query: Query = queries[key]; + return `
`; + }).join(''); + } const options: Component = { exportAs, @@ -36,8 +45,9 @@ export function MockComponent(component: Type): Type ComponentMock) }], + queries, selector, - template: '' + template, }; @MockOf(component) diff --git a/lib/mock-component/test-components/template-outlet.component.ts b/lib/mock-component/test-components/template-outlet.component.ts new file mode 100644 index 0000000000..e40758b9dc --- /dev/null +++ b/lib/mock-component/test-components/template-outlet.component.ts @@ -0,0 +1,14 @@ +import { Component, ContentChild, TemplateRef } from '@angular/core'; + +@Component({ + selector: 'template-outlet-component', + template: ` +

+

+

+ `, +}) +export class TemplateOutletComponent { + @ContentChild('block1') injectedBlock1: TemplateRef; + @ContentChild('block2') injectedBlock2: TemplateRef; +}