blob: 67778b9ee86736546e2c33929015cc4b2881e553 [file] [log] [blame]
/* tslint:disable:no-unused-variable */
import { FormsModule } from '@angular/forms';
import { async, fakeAsync, ComponentFixture, TestBed, tick, inject } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { By } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { Injectable, DebugElement, EventEmitter, SimpleChange } from '@angular/core';
import { click, newEvent } from '../../testing/index';
import { MdDialogRef } from '@angular/material';
import { MdDialogModule, MaterialModule, MdDialog, MdDialogConfig, Overlay, OverlayContainer, OVERLAY_PROVIDERS } from '@angular/material';
import { AbstractMockObservableService } from '../../common/abstract-mock-observable.service';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { MockComponent } from '../../testing/mock.component';
import { AppModule } from '../../app.module';
import { FormattedTimestampPipe } from '../../common-components/pipes/formatted-timestamp.pipe';
import { StringToDatePipe } from '../../common-components/pipes/string-to-date.pipe';
import { DaterangepickerConfig } from 'ng2-daterangepicker';
import { AbstractListComponent } from '../../lists/abstract-list/abstract-list.component';
import { SessionContext } from '../../common/session-context';
import { Router } from '@angular/router';
import { User } from '../../model/user';
import { EntryComponent } from '../../dialogs/entry/entry.component';
import { MainNavigationComponent } from '../../common-components/main-navigation/main-navigation.component';
import { SearchComponent } from './search.component';
import { SearchResultListComponent } from '../../lists/search-result-list/search-result-list.component';
import { GlobalSearchFilter } from '../../model/global-search-filter';
import { Notification } from '../../model/notification';
import { NotificationService } from '../../services/notification.service';
import { SearchResultService } from '../../services/search-result.service';
import { SEARCH_RESULT_NOTIFICATIONS } from '../../test-data/search-result-notifications';
import { FilterSelection } from '../../model/filter-selection';
import { CurrentRemindersComponent } from '../../lists/current-reminders/current-reminders.component';
import { ReminderComponent } from '../reminder/reminder.component';
import { ReminderSearchFilter } from '../../model/reminder-search-filter';
import { ReminderService } from '../../services/reminder.service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MessageService } from '../../services/message.service';
import { ResponsibilityService } from '../../services/responsibility.service';
import { ShiftChangeProtocolComponent } from '../../dialogs/shift-change-protocol/shift-change-protocol.component';
import { USERS } from '../../test-data/users';
class FakeRouter {
navigate(commands: any[]) {
return commands[0];
}
}
@NgModule({
declarations: [EntryComponent, ShiftChangeProtocolComponent],
entryComponents: [
EntryComponent, ShiftChangeProtocolComponent
]
})
class TestModule { }
describe('SearchComponent', () => {
const sessionContext: SessionContext = new SessionContext();
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
let router: Router;
let mockNotificationService: MockNotificationService;
let mockNotificationSearchSortService: MockNotificationSearchSortService;
let mockSearchResultService: MockSearchResultService;
let messageService;
let routerStub: FakeRouter;
routerStub = {
navigate: jasmine.createSpy('navigate').and.callThrough()
};
const correctUser: User = {
id: '44', username: 'carlo', password: 'serverPwd'
, name: 'Carlo Cottura', specialUser: true, itemName: this.userName
};
class MockResponsibilityService extends AbstractMockObservableService {
plannedResponsibilities = null;
responsibilities = null;
getPlannedResponsibilities() {
const resptService = new MockResponsibilityService();
resptService.content = this.plannedResponsibilities;
return resptService;
};
getResponsibilities() {
const resptService = new MockResponsibilityService();
resptService.content = this.responsibilities;
return resptService;
}
}
class MockNotificationService extends AbstractMockObservableService {
itemChanged$ = new EventEmitter();
itemAdded$ = new EventEmitter();
loadCalled = false;
getNotificationVersions(incidentId: number) {
return this;
}
}
class MockNotificationSearchSortService extends AbstractMockObservableService {
itemChanged$ = new EventEmitter();
itemAdded$ = new EventEmitter();
loadCalled = false;
searchResultNotifications = SEARCH_RESULT_NOTIFICATIONS;
}
class MockSearchResultService extends AbstractMockObservableService {
public getSearchResults(searchResultFilter?: GlobalSearchFilter) {
const searchResultNotifications = SEARCH_RESULT_NOTIFICATIONS;
this.content = searchResultNotifications;
return this;
}
}
class MockDialogRef extends AbstractMockObservableService {
close() { }
}
beforeEach(async(() => {
messageService = new MessageService();
mockNotificationService = new MockNotificationService();
mockNotificationSearchSortService = new MockNotificationSearchSortService();
mockSearchResultService = new MockSearchResultService();
router = new FakeRouter() as any as Router;
TestBed.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [EntryComponent, ShiftChangeProtocolComponent]
}
});
TestBed.configureTestingModule({
imports: [FormsModule,
BrowserAnimationsModule,
MdDialogModule,
MaterialModule.forRoot()],
declarations: [
SearchComponent,
EntryComponent,
ShiftChangeProtocolComponent,
MainNavigationComponent,
SearchResultListComponent,
FormattedTimestampPipe,
StringToDatePipe,
MockComponent({ selector: 'app-entry' }),
MockComponent({
selector: 'app-responsibility', inputs: [
'responsiblitySelection']
}),
MockComponent({ selector: 'app-sorting', inputs: ['columnName', 'initColumnName', 'isDesc', 'defaultState'] }),
MockComponent({ selector: 'input', inputs: ['options'] }),
MockComponent({ selector: 'app-search', inputs: ['withCheckboxes', 'withEditButtons', 'isCollapsible'] }),
MockComponent({ selector: 'app-autocomplete', inputs: ['responsibilityForwarding'], outputs: ['responsibilityForwarding'] }),
MockComponent({ selector: 'app-loading-spinner' }),
MockComponent({
selector: 'app-abstract-list', inputs: [
'withCheckboxes', 'withEditButtons', 'isCollapsible', 'stayHidden', 'gridId', 'enforceShowReadOnly']
}),
MockComponent({
selector: 'app-finished-notifications', inputs: ['responsiblitySelection',
'withCheckboxes', 'withEditButtons', 'isCollapsible', 'stayHidden', 'gridId', 'enforceShowReadOnly']
}),
MockComponent({
selector: 'app-open-notifications', inputs: ['responsiblitySelection',
'withCheckboxes', 'withEditButtons', 'isCollapsible', 'stayHidden', 'gridId', 'enforceShowReadOnly']
}),
MockComponent({
selector: 'app-future-notifications', inputs: ['responsiblitySelection',
'withCheckboxes', 'withEditButtons', 'isCollapsible', 'stayHidden', 'gridId', 'enforceShowReadOnly']
}),
MockComponent({
selector: 'app-message-banner'
})
],
providers: [
{ provide: Router, useValue: routerStub },
{ provide: MdDialogRef, useClass: MockDialogRef },
{ provide: Overlay, useClass: Overlay },
{ provide: OVERLAY_PROVIDERS, useClass: OVERLAY_PROVIDERS },
{ provide: DaterangepickerConfig, useClass: DaterangepickerConfig },
{ provide: NotificationService, useValue: mockNotificationService },
{ provide: SearchResultService, useValue: mockSearchResultService },
{ provide: SessionContext, useClass: SessionContext },
{ provide: ReminderService, useClass: ReminderService },
{ provide: ResponsibilityService, useClass: MockResponsibilityService },
{ provide: MessageService, useValue: messageService }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
sessionContext.clearStorage();
sessionContext.setCurrUser(USERS[0]);
component.sessionContext = sessionContext;
});
it('should init correctly with correct user', async(() => {
sessionContext.setCurrUser(correctUser);
mockNotificationSearchSortService.searchResultNotifications = [];
fixture.detectChanges();
expect(component.user.id).toBe(correctUser.id);
}));
it('should should navigate to Overview after click at homebbutton', (() => {
spyOn(component, 'goToOverview').and.callThrough();
fixture.detectChanges();
fixture.whenStable().then(() => {
const button = fixture.debugElement.nativeElement.querySelector('button#homebutton');
button.click();
fixture.detectChanges();
expect(component.goToOverview).toHaveBeenCalled();
expect(routerStub.navigate).toHaveBeenCalledWith(['/overview']);
});
}));
it('should navigate to overview on home-button click', () => {
component.goToOverview();
expect(routerStub.navigate).toHaveBeenCalledWith(['/overview']);
});
it('should open a dialog to view a notification on button click', async(() => {
let entryOpend = false;
let dialog;
const testNotification = SEARCH_RESULT_NOTIFICATIONS[0];
sessionContext.setCurrUser(correctUser);
fixture.detectChanges();
component.dialog.afterOpen.subscribe((value) => {
dialog = value.componentInstance;
spyOn(dialog, 'setNotification');
entryOpend = true;
});
component.openDialogLookUpEntry(testNotification);
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(entryOpend).toBe(true);
expect(dialog.isEditDialog).toBe(false);
});
}));
it('should delete search parameters after Suche löschen button pressed', (() => {
let dialog;
const globalSearchFilter: GlobalSearchFilter = new GlobalSearchFilter();
globalSearchFilter.searchString = 'Ab1224ag';
globalSearchFilter.responsibilityForwarding = 'Karl Heinz';
globalSearchFilter.statusOpenSelection = false;
globalSearchFilter.statusInWorkSelection = false;
globalSearchFilter.statusDoneSelection = false;
globalSearchFilter.statusClosedSelection = false;
globalSearchFilter.fkRefBranch = 2;
globalSearchFilter.fkRefGridTerritory = 1;
globalSearchFilter.fastSearchSelected = false;
component.dialog.afterOpen.subscribe((value) => {
dialog = value.componentInstance;
spyOn(dialog, 'deleteSearch');
});
fixture.whenStable().then(() => {
const button = fixture.debugElement.nativeElement.querySelector('button#deletesearch');
button.click();
fixture.detectChanges();
expect(component.globalSearchFilter.searchString).toBe('');
expect(component.globalSearchFilter.responsibilityForwarding).toBe('');
expect(component.globalSearchFilter.statusOpenSelection).toBe(true);
expect(component.globalSearchFilter.statusInWorkSelection).toBe(true);
expect(component.globalSearchFilter.statusDoneSelection).toBe(true);
expect(component.globalSearchFilter.statusClosedSelection).toBe(true);
expect(component.globalSearchFilter.fkRefBranch).toBe(-1);
expect(component.globalSearchFilter.fkRefGridTerritory).toBe(-1);
expect(component.globalSearchFilter.fastSearchSelected).toBe(true);
});
}));
it('should execute fast search and store it in the session context', (() => {
let dialog;
const globalSearchFilter: GlobalSearchFilter = new GlobalSearchFilter();
globalSearchFilter.searchString = 'a';
globalSearchFilter.statusOpenSelection = true;
globalSearchFilter.fastSearchSelected = true;
component.dialog.afterOpen.subscribe((value) => {
dialog = value.componentInstance;
spyOn(dialog, 'search');
});
fixture.whenStable().then(() => {
const button = fixture.debugElement.nativeElement.querySelector('button#search');
button.click();
fixture.detectChanges();
expect(component.currentSearchFilter).toBe(true);
expect(sessionContext.getGlobalSearchFilter().fastSearchSelected).toBe(true);
});
}));
it('should call search function after search button pressed', async(() => {
spyOn(component, 'search').and.callThrough();
fixture.detectChanges();
fixture.whenStable().then(() => {
const currentSearchFilter: GlobalSearchFilter = new GlobalSearchFilter();
currentSearchFilter.searchString = 'Test';
currentSearchFilter.responsibilityForwarding = 'Max';
currentSearchFilter.statusOpenSelection = false;
currentSearchFilter.statusInWorkSelection = false;
currentSearchFilter.statusDoneSelection = false;
currentSearchFilter.statusClosedSelection = false;
currentSearchFilter.fkRefBranch = 1;
currentSearchFilter.fkRefGridTerritory = 2;
currentSearchFilter.fastSearchSelected = false;
component.currentSearchFilter = currentSearchFilter;
let de: DebugElement;
de = fixture.debugElement.queryAll(By.css('form button:not(#deletesearch)'))[1];
click(de);
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(component.search).toHaveBeenCalled();
expect(component.sessionContext.getGlobalSearchFilter()).not.toBeNull();
expect(component.sessionContext.getGlobalSearchFilter().searchString).toBe(currentSearchFilter.searchString);
});
});
}));
});