Skip to content
Snippets Groups Projects
Commit 307443f5 authored by Kristof De Langhe's avatar Kristof De Langhe
Browse files

54053: Active filter label tests

parent 05053684
No related branches found
No related tags found
No related merge requests found
import { SearchLabelsComponent } from './search-labels.component';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule } from '@ngx-translate/core';
import { SearchService } from '../search-service/search.service';
import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SearchServiceStub } from '../../shared/testing/search-service-stub';
import { Observable } from 'rxjs/Observable';
import { FilterLabel } from '../search-service/filter-label.model';
import { Params } from '@angular/router';
describe('SearchLabelsComponent', () => {
let comp: SearchLabelsComponent;
let fixture: ComponentFixture<SearchLabelsComponent>;
const searchLink = '/search';
let searchService;
const field1 = 'author';
const field2 = 'subject';
const value1 = 'TestAuthor';
const value2 = 'TestSubject';
const filter1 = new FilterLabel(value1, field1);
const filter2 = new FilterLabel(value2, field2);
const mockFilters = [
filter1,
filter2
];
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), NoopAnimationsModule, FormsModule],
declarations: [SearchLabelsComponent],
providers: [
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) }
],
schemas: [NO_ERRORS_SCHEMA]
}).overrideComponent(SearchLabelsComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default }
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchLabelsComponent);
comp = fixture.componentInstance;
searchService = (comp as any).searchService;
(comp as any).appliedFilters = Observable.of(mockFilters);
fixture.detectChanges();
});
describe('when getRemoveParams is called', () => {
let obs: Observable<Params>;
beforeEach(() => {
obs = comp.getRemoveParams(filter1);
});
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);
});
})
});
});
......@@ -11,7 +11,7 @@ import { map } from 'rxjs/operators';
})
export class SearchLabelsComponent {
protected appliedFilters: Observable<FilterLabel[]>;
appliedFilters: Observable<FilterLabel[]>;
constructor(private searchService: SearchService) {
this.appliedFilters = this.searchService.getFilterLabels();
......
......@@ -32,6 +32,8 @@ import {
} from '../../core/cache/response-cache.models';
import { SearchQueryResponse } from './search-query-response.model';
import { SearchFilterConfig } from './search-filter-config.model';
import { FilterLabel } from './filter-label.model';
import createSpyObj = jasmine.createSpyObj;
@Component({ template: '' })
class DummyComponent {
......@@ -245,5 +247,36 @@ describe('SearchService', () => {
expect((searchService as any).responseCache.get).toHaveBeenCalledWith(requestUrl);
});
});
describe('when getFilterLabels is called', () => {
let obs: Observable<FilterLabel[]>;
const value = 'Test';
const orgField = 'author';
const field = 'f.' + orgField;
const mockConfig = new RemoteData(false, false, true, null, [
{
name: orgField,
type: null,
hasFacets: false,
pageSize: 5,
isOpenByDefault: false,
paramName: field
} as SearchFilterConfig
]);
const mockParams = [];
beforeEach(() => {
spyOn((searchService as any), 'getConfig').and.returnValue(Observable.of(mockConfig));
mockParams[field] = value;
(searchService as any).route.queryParams = Observable.of(mockParams);
obs = searchService.getFilterLabels();
});
it('should return the correct labels', () => {
obs.subscribe((filters) => {
expect(filters[0].value).toEqual(value);
});
});
});
});
});
......@@ -37,4 +37,8 @@ export class SearchServiceStub {
getSearchLink() {
return this.searchLink;
}
getFilterLabels() {
return Observable.of([]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment