import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFactoryResolver, ElementRef, Injector, NgModule, OnInit, ViewChild, Component, DebugElement } from '@angular/core'; import { By } from '@angular/platform-browser'; import { DocViewerComponent } from './doc-viewer.component'; import { DocumentContents } from 'app/documents/document.service'; import { EmbeddedModule, embeddedComponents, EmbeddedComponents } from 'app/embedded/embedded.module'; /// Embedded Test Components /// ///// FooComponent ///// @Component({ selector: 'aio-foo', template: `Foo Component` }) class FooComponent { } ///// BarComponent ///// @Component({ selector: 'aio-bar', template: `
Howdy, doc viewer
'; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; fixture.detectChanges(); expect(docViewerEl.innerHTML).toEqual(contents); }); it(('should display nothing after reset static content doc'), () => { const contents = 'Howdy, doc viewer
'; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; fixture.detectChanges(); component.currentDoc = { title: 'fake title', contents: '', url: 'a/c' }; fixture.detectChanges(); expect(docViewerEl.innerHTML).toEqual(''); }); it(('should apply FooComponent'), () => { const contents = `Above Foo
Below Foo
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; fixture.detectChanges(); const fooHtml = docViewerEl.querySelector('aio-foo').innerHTML; expect(fooHtml).toContain('Foo Component'); }); it(('should apply multiple FooComponents'), () => { const contents = `Above Foo
Below Foo
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; fixture.detectChanges(); const foos = docViewerEl.querySelectorAll('aio-foo'); expect(foos.length).toBe(2); }); it(('should apply BarComponent'), () => { const contents = `Above Bar
Below Bar
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; fixture.detectChanges(); const barHtml = docViewerEl.querySelector('aio-bar').innerHTML; expect(barHtml).toContain('Bar Component'); }); it(('should project bar content into BarComponent'), () => { const contents = `Above Bar
Below Bar
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; // necessary to trigger projection within ngOnInit fixture.detectChanges(); const barHtml = docViewerEl.querySelector('aio-bar').innerHTML; expect(barHtml).toContain('###bar content###'); }); it(('should include Foo and Bar'), () => { const contents = `Top
Bottom
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; // necessary to trigger Bar's projection within ngOnInit fixture.detectChanges(); const foos = docViewerEl.querySelectorAll('aio-foo'); expect(foos.length).toBe(2, 'should have 2 foos'); const barHtml = docViewerEl.querySelector('aio-bar').innerHTML; expect(barHtml).toContain('###bar content###', 'should have bar with projected content'); }); it(('should not include Bar within Foo'), () => { const contents = `Top
Bottom
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; // necessary to trigger Bar's projection within ngOnInit fixture.detectChanges(); const foos = docViewerEl.querySelectorAll('aio-foo'); expect(foos.length).toBe(2, 'should have 2 foos'); const bars = docViewerEl.querySelectorAll('aio-bar'); expect(bars.length).toBe(0, 'did not expect Bar inside Foo'); }); // because FooComponents are processed before BazComponents it(('should include Foo within Bar'), () => { const contents = `Top
Bottom
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; // necessary to trigger Bar's projection within ngOnInit fixture.detectChanges(); const foos = docViewerEl.querySelectorAll('aio-foo'); expect(foos.length).toBe(2, 'should have 2 foos'); const bars = docViewerEl.querySelectorAll('aio-bar'); expect(bars.length).toBe(1, 'should have a bar'); expect(bars[0].innerHTML).toContain('Bar Component', 'should have bar template content'); }); // TheTop
Bottom
`; component.currentDoc = { title: 'fake title', contents, url: 'a/b' }; // necessary to trigger Bar's projection within ngOnInit fixture.detectChanges(); const bazs = docViewerEl.querySelectorAll('aio-baz'); // Both baz tags are there ... expect(bazs.length).toBe(2, 'should have 2 bazs'); expect(bazs[0].innerHTML).not.toContain('Baz Component', 'did not expect 1st Baz template content'); expect(bazs[1].innerHTML).toContain('Baz Component', 'expected 2nd Baz template content'); }); });