solved issues with tests

This commit is contained in:
lotte
2019-07-25 14:41:47 +02:00
parent bb76015aa1
commit fa6fe668c3
4 changed files with 111 additions and 51 deletions

View File

@@ -1,17 +1,15 @@
import {SearchFixedFilterService} from './search-fixed-filter.service'; import {SearchFixedFilterService} from './search-fixed-filter.service';
import { RouteService } from '../../../core/services/route.service';
import {RequestService} from '../../../core/data/request.service'; import {RequestService} from '../../../core/data/request.service';
import {HALEndpointService} from '../../../core/shared/hal-endpoint.service'; import {HALEndpointService} from '../../../core/shared/hal-endpoint.service';
import {of as observableOf} from 'rxjs'; import {of as observableOf} from 'rxjs';
import {RequestEntry} from '../../../core/data/request.reducer'; import {RequestEntry} from '../../../core/data/request.reducer';
import { FilteredDiscoveryQueryResponse, RestResponse } from '../../../core/cache/response.models'; import {FilteredDiscoveryQueryResponse} from '../../../core/cache/response.models';
describe('SearchFixedFilterService', () => { describe('SearchFixedFilterService', () => {
let service: SearchFixedFilterService; let service: SearchFixedFilterService;
const filterQuery = 'filter:query'; const filterQuery = 'filter:query';
const routeServiceStub = {} as RouteService;
const requestServiceStub = Object.assign({ const requestServiceStub = Object.assign({
/* tslint:disable:no-empty */ /* tslint:disable:no-empty */
configure: () => {}, configure: () => {},
@@ -26,7 +24,7 @@ describe('SearchFixedFilterService', () => {
}); });
beforeEach(() => { beforeEach(() => {
service = new SearchFixedFilterService(routeServiceStub, requestServiceStub, halServiceStub); service = new SearchFixedFilterService(requestServiceStub, halServiceStub);
}); });
describe('when getQueryByFilterName is called with a filterName', () => { describe('when getQueryByFilterName is called with a filterName', () => {

View File

@@ -0,0 +1,87 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule } from '@ngx-translate/core';
import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Observable, of as observableOf } from 'rxjs';
import { Params } from '@angular/router';
import {SearchLabelComponent} from './search-label.component';
import {ObjectKeysPipe} from '../../../shared/utils/object-keys-pipe';
import {SearchService} from '../../search-service/search.service';
import {SEARCH_CONFIG_SERVICE} from '../../../+my-dspace-page/my-dspace-page.component';
import {SearchServiceStub} from '../../../shared/testing/search-service-stub';
import {SearchConfigurationServiceStub} from '../../../shared/testing/search-configuration-service-stub';
describe('SearchLabelComponent', () => {
let comp: SearchLabelComponent;
let fixture: ComponentFixture<SearchLabelComponent>;
const searchLink = '/search';
let searchService;
const key1 = 'author';
const key2 = 'subject';
const value1 = 'Test, Author';
const normValue1 = 'Test, Author';
const value2 = 'TestSubject';
const value3 = 'Test, Authority,authority';
const normValue3 = 'Test, Authority';
const filter1 = [key1, value1];
const filter2 = [key2, value2];
const mockFilters = [
filter1,
filter2
];
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), NoopAnimationsModule, FormsModule],
declarations: [SearchLabelComponent, ObjectKeysPipe],
providers: [
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) },
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() }
// { provide: SearchConfigurationService, useValue: {getCurrentFrontendFilters : () => observableOf({})} }
],
schemas: [NO_ERRORS_SCHEMA]
}).overrideComponent(SearchLabelComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default }
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchLabelComponent);
comp = fixture.componentInstance;
searchService = (comp as any).searchService;
comp.key = key1;
comp.value = value1;
(comp as any).appliedFilters = observableOf(mockFilters);
fixture.detectChanges();
});
describe('when getRemoveParams is called', () => {
let obs: Observable<Params>;
beforeEach(() => {
obs = comp.getRemoveParams();
});
it('should return all params but the provided filter', () => {
obs.subscribe((params) => {
// Should contain only filter2 and page: length == 2
expect(Object.keys(params).length).toBe(2);
});
})
});
describe('when normalizeFilterValue is called', () => {
it('should return properly filter value', () => {
let result: string;
result = comp.normalizeFilterValue(value1);
expect(result).toBe(normValue1);
result = comp.normalizeFilterValue(value3);
expect(result).toBe(normValue3);
})
});
});

View File

@@ -6,11 +6,9 @@ import { SearchService } from '../search-service/search.service';
import {ChangeDetectionStrategy, NO_ERRORS_SCHEMA} from '@angular/core'; import {ChangeDetectionStrategy, NO_ERRORS_SCHEMA} from '@angular/core';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {SearchServiceStub} from '../../shared/testing/search-service-stub'; import {SearchServiceStub} from '../../shared/testing/search-service-stub';
import { Observable, of as observableOf } from 'rxjs'; import {of as observableOf} from 'rxjs';
import { Params } from '@angular/router';
import {ObjectKeysPipe} from '../../shared/utils/object-keys-pipe'; import {ObjectKeysPipe} from '../../shared/utils/object-keys-pipe';
import {SEARCH_CONFIG_SERVICE} from '../../+my-dspace-page/my-dspace-page.component'; import {SEARCH_CONFIG_SERVICE} from '../../+my-dspace-page/my-dspace-page.component';
import { SearchConfigurationServiceStub } from '../../shared/testing/search-configuration-service-stub';
describe('SearchLabelsComponent', () => { describe('SearchLabelsComponent', () => {
let comp: SearchLabelsComponent; let comp: SearchLabelsComponent;
@@ -22,10 +20,7 @@ describe('SearchLabelsComponent', () => {
const field1 = 'author'; const field1 = 'author';
const field2 = 'subject'; const field2 = 'subject';
const value1 = 'Test, Author'; const value1 = 'Test, Author';
const normValue1 = 'Test, Author';
const value2 = 'TestSubject'; const value2 = 'TestSubject';
const value3 = 'Test, Authority,authority';
const normValue3 = 'Test, Authority';
const filter1 = [field1, value1]; const filter1 = [field1, value1];
const filter2 = [field2, value2]; const filter2 = [field2, value2];
const mockFilters = [ const mockFilters = [
@@ -39,8 +34,7 @@ describe('SearchLabelsComponent', () => {
declarations: [SearchLabelsComponent, ObjectKeysPipe], declarations: [SearchLabelsComponent, ObjectKeysPipe],
providers: [ providers: [
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) }, { provide: SearchService, useValue: new SearchServiceStub(searchLink) },
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() } { provide: SEARCH_CONFIG_SERVICE, useValue: {getCurrentFrontendFilters : () => observableOf(mockFilters)} }
// { provide: SearchConfigurationService, useValue: {getCurrentFrontendFilters : () => observableOf({})} }
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
}).overrideComponent(SearchLabelsComponent, { }).overrideComponent(SearchLabelsComponent, {
@@ -56,30 +50,11 @@ describe('SearchLabelsComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
}); });
describe('when getRemoveParams is called', () => { describe('when the component has been initialized', () => {
let obs: Observable<Params>;
beforeEach(() => {
obs = comp.getRemoveParams(filter1[0], filter1[1]);
});
it('should return all params but the provided filter', () => { it('should return all params but the provided filter', () => {
obs.subscribe((params) => { comp.appliedFilters.subscribe((filters) => {
// Should contain only filter2 and page: length == 2 expect(filters).toBe(mockFilters);
expect(Object.keys(params).length).toBe(2);
}); });
}) })
}); });
describe('when normalizeFilterValue is called', () => {
it('should return properly filter value', () => {
let result: string;
result = comp.normalizeFilterValue(value1);
expect(result).toBe(normValue1);
result = comp.normalizeFilterValue(value3);
expect(result).toBe(normValue3);
})
});
}); });

View File

@@ -200,7 +200,7 @@ describe('SearchPageComponent', () => {
beforeEach(() => { beforeEach(() => {
menu = fixture.debugElement.query(By.css('#search-sidebar-sm')).nativeElement; menu = fixture.debugElement.query(By.css('#search-sidebar-sm')).nativeElement;
comp.isSidebarCollapsed = () => observableOf(true); (comp as any).isSidebarCollapsed$ = observableOf(true);
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -215,7 +215,7 @@ describe('SearchPageComponent', () => {
beforeEach(() => { beforeEach(() => {
menu = fixture.debugElement.query(By.css('#search-sidebar-sm')).nativeElement; menu = fixture.debugElement.query(By.css('#search-sidebar-sm')).nativeElement;
comp.isSidebarCollapsed = () => observableOf(false); (comp as any).isSidebarCollapsed$ = observableOf(false);
fixture.detectChanges(); fixture.detectChanges();
}); });