genetic-resources.component.spec.ts 3.67 KB
Newer Older
Exbrayat Cédric's avatar
Exbrayat Cédric committed
1
2
3
import { TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
4
import { ComponentTester, speculoosMatchers } from 'ngx-speculoos';
Exbrayat Cédric's avatar
Exbrayat Cédric committed
5
6
7
8

import { GeneticResourcesComponent } from './genetic-resources.component';
import { GeneticResourceComponent } from '../genetic-resource/genetic-resource.component';
import { toGeneticResource, toSinglePage } from '../models/test-model-generators';
9
10
11
12
import { LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import { GeneticResourceModel } from '../models/genetic-resource.model';
Exbrayat Cédric's avatar
Exbrayat Cédric committed
13
14
15
16
17
18
19
20
21
22
23
24
25

describe('GeneticResourcesComponent', () => {

  class GeneticResourcesComponentTester extends ComponentTester<GeneticResourcesComponent> {
    constructor() {
      super(GeneticResourcesComponent);
    }

    get results() {
      return this.debugElement.queryAll(By.directive(GeneticResourceComponent));
    }

    get noResults() {
26
      return this.element('#no-results');
Exbrayat Cédric's avatar
Exbrayat Cédric committed
27
28
    }

29
30
31
32
    get resume() {
      return this.element('#resume');
    }
  }
Exbrayat Cédric's avatar
Exbrayat Cédric committed
33

34
35
36
37
38
39
40
41
42
  beforeEach(() => {
    registerLocaleData(localeFr);
    TestBed.configureTestingModule({
      imports: [ReactiveFormsModule],
      declarations: [GeneticResourcesComponent, GeneticResourceComponent],
      providers: [
        { provide: LOCALE_ID, useValue: 'fr-FR' }
      ]
    });
Exbrayat Cédric's avatar
Exbrayat Cédric committed
43

44
    jasmine.addMatchers(speculoosMatchers);
Exbrayat Cédric's avatar
Exbrayat Cédric committed
45
46
47
48
49
50
51
52
53
54
55
56
  });

  it('should display no results if empty', () => {
    const tester = new GeneticResourcesComponentTester();
    const component = tester.componentInstance;

    // given no results
    component.geneticResources = toSinglePage([]);
    tester.detectChanges();

    // then it should display a message
    expect(tester.results.length).toBe(0);
57
    expect(tester.resume).toBeNull();
Exbrayat Cédric's avatar
Exbrayat Cédric committed
58
59
60
61
62
63
64
    expect(tester.noResults).not.toBeNull();
  });

  it('should display results if there are some', () => {
    const tester = new GeneticResourcesComponentTester();
    const component = tester.componentInstance;

65
    // given two results
Exbrayat Cédric's avatar
Exbrayat Cédric committed
66
67
68
69
70
71
72
73
    const bacteria1 = toGeneticResource('Bacteria1');
    const bacteria2 = toGeneticResource('Bacteria2');
    component.geneticResources = toSinglePage([bacteria1, bacteria2]);
    tester.detectChanges();

    // then it should display each result
    expect(tester.noResults).toBeNull();
    expect(tester.results.length).toBe(2);
74

Exbrayat Cédric's avatar
Exbrayat Cédric committed
75
76
77
78
    const result1 = tester.results[0].componentInstance as GeneticResourceComponent;
    expect(result1.geneticResource).toBe(bacteria1);
    const result2 = tester.results[1].componentInstance as GeneticResourceComponent;
    expect(result2.geneticResource).toBe(bacteria2);
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

    expect(tester.resume).toContainText('Résultats 1 à 2 sur 2');
    expect(tester.resume).not.toContainText('limités');
  });

  it('should display limited results in resume, and format numbers in French', () => {
    const tester = new GeneticResourcesComponentTester();
    const component = tester.componentInstance;

    // given results
    const content: Array<GeneticResourceModel> = [];
    for (let i = 0; i < 20; i++) {
      content.push(toGeneticResource(`Bacteria ${i}`));
    }

    // in page 200 on a limited number of pages
    component.geneticResources = toSinglePage(content);
    component.geneticResources.totalElements = 12000;
    component.geneticResources.totalPages = 500;
    component.geneticResources.number = 200;

    tester.detectChanges();

    // then it should display each result
    expect(tester.noResults).toBeNull();
    expect(tester.results.length).toBe(20);
    expect(tester.resume).toContainText('Résultats 4\u00a0001 à 4\u00a0020 sur 12\u00a0000 (limités à 10\u00a0000)');
Exbrayat Cédric's avatar
Exbrayat Cédric committed
106
107
  });
});