Newer
Older
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { CommunityDataService } from '../core/data/community-data.service';
import { SearchPageComponent } from './search-page.component';
import { SearchService } from './search-service/search.service';
import { Community } from '../core/shared/community.model';
import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model';
import { SortDirection, SortOptions } from '../core/cache/models/sort-options.model';
let comp: SearchPageComponent;
let fixture: ComponentFixture<SearchPageComponent>;
const mockResults = Observable.of(['test', 'data']);
const searchServiceStub = {
search: () => mockResults
};
const queryParam = 'test query';
const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
const activatedRouteStub = {
queryParams: Observable.of({
query: queryParam,
scope: scopeParam
})
};
const mockCommunityList = [];
const communityDataServiceStub = {
findAll: () => Observable.of(mockCommunityList),
findById: () => Observable.of(new Community())
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: SearchService, useValue: searchServiceStub },
{ provide: ActivatedRoute, useValue: activatedRouteStub },
{ provide: CommunityDataService, useValue: communityDataServiceStub },
{ provide: Router, useClass: RouterStub }
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchPageComponent);
comp = fixture.componentInstance; // SearchPageComponent test instance
fixture.detectChanges();
it('should set the scope and query based on the route parameters', () => {
expect(comp.query).toBe(queryParam);
expect((comp as any).scope).toBe(scopeParam);
});
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
describe('when update search results is called', () => {
let pagination;
let sort;
beforeEach(() => {
pagination = Object.assign(
{},
new PaginationComponentOptions(),
{
currentPage: 5,
pageSize: 15
}
);
sort = Object.assign({},
new SortOptions(),
{
direction: SortDirection.Ascending,
field: 'test-field'
}
);
});
it('should call the search function of the search service with the right parameters', () => {
spyOn(searchServiceObject, 'search').and.callThrough();
(comp as any).updateSearchResults({
pagination: pagination,
sort: sort
});
expect(searchServiceObject.search).toHaveBeenCalledWith(queryParam, scopeParam, {
pagination: pagination,
sort: sort
});
});
it('should update the results', () => {
spyOn(searchServiceObject, 'search').and.callThrough();
(comp as any).updateSearchResults({});
expect(comp.results as any).toBe(mockResults);