11import { HttpClientModule } from '@angular/common/http' ;
2+ import { Component , Input , OnInit , ViewChild } from '@angular/core' ;
23import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
34import { MatIconModule } from '@angular/material/icon' ;
45import { MatListModule } from '@angular/material/list' ;
@@ -8,38 +9,62 @@ import { By } from '@angular/platform-browser';
89import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
910import { RouterTestingModule } from '@angular/router/testing' ;
1011import { KnoraApiConnection } from '@dasch-swiss/dsp-js' ;
11- import {
12- AppInitService ,
13- DspActionModule ,
14- DspApiConfigToken ,
15- DspApiConnectionToken ,
16- DspCoreModule ,
17- DspSearchModule
18- } from '@dasch-swiss/dsp-ui' ;
12+ import { DspActionModule , DspSearchModule } from '@dasch-swiss/dsp-ui' ;
1913import { TranslateModule } from '@ngx-translate/core' ;
14+ import { AppInitService } from 'src/app/app-init.service' ;
2015import { ComponentCommunicationEventService , EmitEvent , Events } from 'src/app/main/services/component-communication-event.service' ;
2116import { UserMenuComponent } from 'src/app/user/user-menu/user-menu.component' ;
2217import { TestConfig } from 'test.config' ;
18+ import { DspApiConfigToken , DspApiConnectionToken } from '../declarations/dsp-api-tokens' ;
2319import { SelectLanguageComponent } from '../select-language/select-language.component' ;
2420import { HeaderComponent } from './header.component' ;
2521
22+ /**
23+ * test host component to simulate parent component.
24+ */
25+ @Component ( {
26+ template : '<app-header #header></app-header>'
27+ } )
28+ class TestHostHeaderComponent {
29+ @ViewChild ( 'header' ) headerComp : HeaderComponent ;
30+ }
31+
32+ /**
33+ * test component to simulate search panel component.
34+ */
35+ @Component ( {
36+ selector : 'app-search-panel' ,
37+ template : ''
38+ } )
39+ class TestSearchPanelComponent {
40+ @Input ( ) projectfilter ?: boolean = false ;
41+ @Input ( ) filterbyproject ?: string ;
42+ @Input ( ) limitToProject ?: string ;
43+ @Input ( ) advanced ?: boolean = false ;
44+ @Input ( ) expert ?: boolean = false ;
45+ }
46+
2647describe ( 'HeaderComponent' , ( ) => {
27- let component : HeaderComponent ;
28- let fixture : ComponentFixture < HeaderComponent > ;
48+ let testHostComponent : TestHostHeaderComponent ;
49+ let testHostFixture : ComponentFixture < TestHostHeaderComponent > ;
50+ let hostCompDe ;
51+ let headerCompDe ;
52+
2953 let componentCommsService : ComponentCommunicationEventService ;
3054
3155 beforeEach ( waitForAsync ( ( ) => {
3256 TestBed . configureTestingModule ( {
3357 declarations : [
3458 HeaderComponent ,
59+ TestHostHeaderComponent ,
60+ TestSearchPanelComponent ,
3561 SelectLanguageComponent ,
3662 UserMenuComponent
3763 ] ,
3864 imports : [
3965 BrowserAnimationsModule ,
4066 HttpClientModule ,
4167 DspActionModule ,
42- DspCoreModule ,
4368 DspSearchModule ,
4469 MatIconModule ,
4570 MatListModule ,
@@ -66,55 +91,59 @@ describe('HeaderComponent', () => {
6691 } ) ) ;
6792
6893 beforeEach ( ( ) => {
69- fixture = TestBed . createComponent ( HeaderComponent ) ;
70- component = fixture . componentInstance ;
71- fixture . detectChanges ( ) ;
94+ testHostFixture = TestBed . createComponent ( TestHostHeaderComponent ) ;
95+ testHostComponent = testHostFixture . componentInstance ;
96+ testHostFixture . detectChanges ( ) ;
97+
98+ hostCompDe = testHostFixture . debugElement ;
99+ headerCompDe = hostCompDe . query ( By . directive ( HeaderComponent ) ) ;
72100 } ) ;
73101
74102 it ( 'should create' , ( ) => {
75- expect ( component ) . toBeTruthy ( ) ;
103+ expect ( testHostComponent ) . toBeTruthy ( ) ;
76104 } ) ;
77105
78106 it ( 'should display the lamp icon button' , ( ) => {
79- const lampIconBtn = fixture . debugElement . query ( By . css ( 'button.home-button' ) ) ;
80- expect ( lampIconBtn ) . toBeDefined ( ) ;
107+ const lampIconBtn = testHostFixture . debugElement . query ( By . css ( 'button.home-button' ) ) ;
108+ expect ( lampIconBtn ) . toBeTruthy ( ) ;
81109 } ) ;
82110
83111 it ( 'should display the link to the help page' , ( ) => {
84- const helpBtn = fixture . debugElement . query ( By . css ( 'button.help' ) ) ;
85- expect ( helpBtn ) . toBeDefined ( ) ;
112+ const helpBtn = testHostFixture . debugElement . query ( By . css ( 'button.help' ) ) ;
113+ expect ( helpBtn ) . toBeTruthy ( ) ;
86114
87115 const helpBtnLabel = helpBtn . nativeElement . innerHTML ;
88116 expect ( helpBtnLabel ) . toEqual ( 'Help' ) ;
89117 } ) ;
90118
91119 it ( 'should display the login button' , ( ) => {
92- const loginBtn = fixture . debugElement . query ( By . css ( 'button.login-button' ) ) ;
93- expect ( loginBtn ) . toBeDefined ( ) ;
120+ const loginBtn = testHostFixture . debugElement . query ( By . css ( 'button.login-button' ) ) ;
121+ expect ( loginBtn ) . toBeTruthy ( ) ;
94122
95123 const loginBtnLabel = loginBtn . nativeElement . innerHTML ;
96124 expect ( loginBtnLabel ) . toEqual ( 'LOGIN' ) ;
97125 } ) ;
98126
99- it ( 'should display fulltext- search' , ( ) => {
100- const searchPanel = fixture . debugElement . query ( By . css ( 'dsp-fulltext -search-panel' ) ) ;
101- expect ( searchPanel ) . toBeDefined ( ) ;
127+ it ( 'should display search panel ' , ( ) => {
128+ const searchPanel = testHostFixture . debugElement . query ( By . css ( 'app -search-panel' ) ) ;
129+ expect ( searchPanel ) . toBeTruthy ( ) ;
102130 } ) ;
103131
104132 it ( 'should subscribe to component communication when the loginSuccess event is emitted' , ( ) => {
105133 componentCommsService . emit ( new EmitEvent ( Events . loginSuccess ) ) ;
106- fixture . detectChanges ( ) ;
107- expect ( component . componentCommsSubscription . closed ) . toBe ( false ) ;
134+ testHostFixture . detectChanges ( ) ;
135+ expect ( testHostComponent . headerComp . componentCommsSubscription . closed ) . toBe ( false ) ;
108136 } ) ;
109137
110138 it ( 'should unsubscribe from changes on destruction' , ( ) => {
111139
112- expect ( component . componentCommsSubscription . closed ) . toBe ( false ) ;
140+ expect ( testHostComponent . headerComp . componentCommsSubscription . closed ) . toBe ( false ) ;
113141
114- fixture . destroy ( ) ;
142+ testHostFixture . destroy ( ) ;
115143
116- expect ( component . componentCommsSubscription . closed ) . toBe ( true ) ;
144+ expect ( testHostComponent . headerComp . componentCommsSubscription . closed ) . toBe ( true ) ;
117145
118146 } ) ;
119147
120148} ) ;
149+
0 commit comments