search.service.spec.ts 2.73 KB
Newer Older
1
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
Exbrayat Cédric's avatar
Exbrayat Cédric committed
2
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
3
import { Subject } from 'rxjs';
Exbrayat Cédric's avatar
Exbrayat Cédric committed
4
5
6
7
8
9
10

import { toGeneticResource, toSinglePage } from './models/test-model-generators';
import { SearchService } from './search.service';
import { Page } from './models/page';
import { GeneticResourceModel } from './models/genetic-resource.model';

describe('SearchService', () => {
11
12
13
14
15
16
17
18
19
20
21
  let service: SearchService;
  let http: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule]
    });

    service = TestBed.get(SearchService) as SearchService;
    http = TestBed.get(HttpTestingController) as HttpTestingController;
  });
Exbrayat Cédric's avatar
Exbrayat Cédric committed
22
23
24

  it('should search for the query', () => {
    let actualResults: Page<GeneticResourceModel>;
Exbrayat Cédric's avatar
Exbrayat Cédric committed
25
    service.search('Bacteria', 2)
Exbrayat Cédric's avatar
Exbrayat Cédric committed
26
27
28
29
30
      .subscribe(results => actualResults = results);

    const resource = toGeneticResource('Bacteria');
    const expectedResults = toSinglePage([resource]);

Exbrayat Cédric's avatar
Exbrayat Cédric committed
31
    http.expectOne('/api/genetic-resources?query=Bacteria&page=1').flush(expectedResults);
Exbrayat Cédric's avatar
Exbrayat Cédric committed
32
33
34
35
    expect(actualResults).toEqual(expectedResults);

    http.verify();
  });
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

  it('should have a typeahead that suggests if text longer than 1, after 300 ms and only if changed', fakeAsync(() => {
    const typeahead = service.getSuggesterTypeahead();

    const entered = new Subject<string>();
    const results: Array<Array<string>> = [];
    typeahead(entered).subscribe(result => results.push(result));

    // simulate what is entered, character by character, after a delay between each one
    entered.next(' ');
    tick(100);
    entered.next(' v');
    tick(300); // should not trigger a search, but emit an empty array because input is too short
    entered.next(' vi');
    tick(100);
    entered.next(' vit');
    tick(100);
    entered.next(' vit');
    tick(100);
    entered.next(' vit ');
    tick(300); // should finally trigger a search for 'vit'

    const vitResult = ['vitis', 'vitis vinifera'];
    http.expectOne('/api/genetic-resources-suggestions?query=vit').flush(vitResult);

    entered.next(' viti ');
    tick(100);
    entered.next(' viti');
    tick(100);
    entered.next(' vit');
    tick(300); // should not trigger a second search since same value

    entered.next(' viti');
    tick(100);
    entered.next(' vitis');
    tick(100);
    entered.next(' vitis ');
    tick(100);
    entered.next(' vitis v');
    tick(300); // should trigger a second search

    const vitisVResult = ['vitis vinifera'];
    http.expectOne('/api/genetic-resources-suggestions?query=vitis%20v').flush(vitisVResult);

    expect(results).toEqual([[], vitResult, vitisVResult]);

    http.verify();
  }));
Exbrayat Cédric's avatar
Exbrayat Cédric committed
84
});