/
header.component.spec.ts
162 lines (139 loc) · 5.44 KB
/
header.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { HttpClientModule } from '@angular/common/http';
import { Component, Input, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatDialogModule } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { MatListModule } from '@angular/material/list';
import { MatMenuModule } from '@angular/material/menu';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatToolbarModule } from '@angular/material/toolbar';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { KnoraApiConnection } from '@dasch-swiss/dsp-js';
import { TranslateModule } from '@ngx-translate/core';
import { AppInitService } from 'src/app/app-init.service';
import { ComponentCommunicationEventService, EmitEvent, Events } from 'src/app/main/services/component-communication-event.service';
import { TestConfig } from 'test.config';
import { DspApiConfigToken, DspApiConnectionToken } from '../declarations/dsp-api-tokens';
import { SelectLanguageComponent } from '../select-language/select-language.component';
import { HeaderComponent } from './header.component';
/**
* test host component to simulate parent component.
*/
@Component({
template: '<app-header #header></app-header>'
})
class TestHostHeaderComponent {
@ViewChild('header') headerComp: HeaderComponent;
}
/**
* test component to simulate search panel component.
*/
@Component({
selector: 'app-search-panel',
template: ''
})
class TestSearchPanelComponent {
@Input() projectfilter?: boolean = false;
@Input() filterbyproject?: string;
@Input() limitToProject?: string;
@Input() advanced?: boolean = false;
@Input() expert?: boolean = false;
}
/**
* test component to simulate user menu component.
*/
@Component({
selector: 'app-user-menu',
template: ''
})
class TestUserMenuComponent {
@Input() session?: boolean = true;
}
describe('HeaderComponent', () => {
let testHostComponent: TestHostHeaderComponent;
let testHostFixture: ComponentFixture<TestHostHeaderComponent>;
let hostCompDe;
let headerCompDe;
let componentCommsService: ComponentCommunicationEventService;
const appInitSpy = {
dspConfig: {
release: '2022.01.01'
}
};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
HeaderComponent,
TestHostHeaderComponent,
TestSearchPanelComponent,
TestUserMenuComponent,
SelectLanguageComponent
],
imports: [
BrowserAnimationsModule,
HttpClientModule,
MatDialogModule,
MatIconModule,
MatListModule,
MatMenuModule,
MatSnackBarModule,
MatToolbarModule,
RouterTestingModule,
TranslateModule.forRoot()
],
providers: [
AppInitService,
{
provide: DspApiConfigToken,
useValue: TestConfig.ApiConfig
},
{
provide: DspApiConnectionToken,
useValue: new KnoraApiConnection(TestConfig.ApiConfig)
},
{
provide: AppInitService,
useValue: appInitSpy
},
ComponentCommunicationEventService
]
}).compileComponents();
componentCommsService = TestBed.inject(ComponentCommunicationEventService);
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostHeaderComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
hostCompDe = testHostFixture.debugElement;
headerCompDe = hostCompDe.query(By.directive(HeaderComponent));
});
it('should create', () => {
expect(testHostComponent).toBeTruthy();
});
it('should display the lamp icon button', () => {
const lampIconBtn = testHostFixture.debugElement.query(By.css('button.home-button'));
expect(lampIconBtn).toBeTruthy();
});
it('should display the link to the help page', () => {
const helpBtn = testHostFixture.debugElement.query(By.css('button.help'));
expect(helpBtn).toBeTruthy();
const helpBtnLabel = helpBtn.nativeElement.innerHTML;
expect(helpBtnLabel).toEqual('Help');
});
it('should display search panel', () => {
const searchPanel = testHostFixture.debugElement.query(By.css('app-search-panel'));
expect(searchPanel).toBeTruthy();
});
it('should subscribe to component communication when the loginSuccess event is emitted', () => {
componentCommsService.emit(new EmitEvent(Events.loginSuccess));
testHostFixture.detectChanges();
expect(testHostComponent.headerComp.componentCommsSubscription.closed).toBe(false);
});
it('should unsubscribe from changes on destruction', () => {
expect(testHostComponent.headerComp.componentCommsSubscription.closed).toBe(false);
testHostFixture.destroy();
expect(testHostComponent.headerComp.componentCommsSubscription.closed).toBe(true);
});
});