Skip to content
Snippets Groups Projects
search-page.component.spec.ts 3.4 KiB
Newer Older
Lotte Hofstede's avatar
Lotte Hofstede committed
import { NO_ERRORS_SCHEMA } from '@angular/core';
Lotte Hofstede's avatar
Lotte Hofstede committed
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
Lotte Hofstede's avatar
Lotte Hofstede committed
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { CommunityDataService } from '../core/data/community-data.service';
Lotte Hofstede's avatar
Lotte Hofstede committed
import { SearchPageComponent } from './search-page.component';
import { SearchService } from './search-service/search.service';
Lotte Hofstede's avatar
Lotte Hofstede committed
import { Community } from '../core/shared/community.model';
Lotte Hofstede's avatar
Lotte Hofstede committed
import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model';
import { SortDirection, SortOptions } from '../core/cache/models/sort-options.model';
Art Lowel's avatar
Art Lowel committed
describe('SearchPageComponent', () => {
Lotte Hofstede's avatar
Lotte Hofstede committed
  let comp: SearchPageComponent;
  let fixture: ComponentFixture<SearchPageComponent>;
Lotte Hofstede's avatar
Lotte Hofstede committed
  let searchServiceObject: SearchService;
Art Lowel's avatar
Art Lowel committed
  const mockResults = Observable.of(['test', 'data']);
Lotte Hofstede's avatar
Lotte Hofstede committed
  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())
Lotte Hofstede's avatar
Lotte Hofstede committed
  };

  class RouterStub {
Lotte Hofstede's avatar
Lotte Hofstede committed
    navigateByUrl(url: string) {
      return url;
    }
Lotte Hofstede's avatar
Lotte Hofstede committed

  beforeEach(async(() => {
    TestBed.configureTestingModule({
Lotte Hofstede's avatar
Lotte Hofstede committed
      // imports: [ SearchPageModule ],
Lotte Hofstede's avatar
Lotte Hofstede committed
      declarations: [SearchPageComponent],
Lotte Hofstede's avatar
Lotte Hofstede committed
      providers: [
        { provide: SearchService, useValue: searchServiceStub },
        { provide: ActivatedRoute, useValue: activatedRouteStub },
        { provide: CommunityDataService, useValue: communityDataServiceStub },
        { provide: Router, useClass: RouterStub }
      ],
Lotte Hofstede's avatar
Lotte Hofstede committed
      schemas: [NO_ERRORS_SCHEMA]
Lotte Hofstede's avatar
Lotte Hofstede committed
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(SearchPageComponent);
    comp = fixture.componentInstance; // SearchPageComponent test instance
    fixture.detectChanges();
Lotte Hofstede's avatar
Lotte Hofstede committed
    searchServiceObject = (comp as any).service;
Lotte Hofstede's avatar
Lotte Hofstede committed
  it('should set the scope and query based on the route parameters', () => {
    expect(comp.query).toBe(queryParam);
    expect((comp as any).scope).toBe(scopeParam);
  });
Lotte Hofstede's avatar
Lotte Hofstede committed
  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);
William Welling's avatar
William Welling committed
    });
Lotte Hofstede's avatar
Lotte Hofstede committed
});