summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/refresh-interval.service.spec.ts
blob: c26d6389be653075a1e19069b5acb380c71b6cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NgZone } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { configureTestBed } from '~/testing/unit-test-helper';
import { RefreshIntervalService } from './refresh-interval.service';

describe('RefreshIntervalService', () => {
  let service: RefreshIntervalService;

  configureTestBed({
    providers: [RefreshIntervalService]
  });

  beforeEach(() => {
    service = TestBed.inject(RefreshIntervalService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should initial private interval time right', () => {
    sessionStorage.setItem('dashboard_interval', '10000');
    const ngZone = TestBed.inject(NgZone);
    service = new RefreshIntervalService(ngZone);
    expect(service.getRefreshInterval()).toBe(10000);
  });

  describe('setRefreshInterval', () => {
    let notifyCount: number;

    it('should send notification to component at correct interval time when interval changed', fakeAsync(() => {
      service.intervalData$.subscribe(() => {
        notifyCount++;
      });

      notifyCount = 0;
      service.setRefreshInterval(10000);
      tick(10000);
      expect(service.getRefreshInterval()).toBe(10000);
      expect(notifyCount).toBe(1);

      notifyCount = 0;
      service.setRefreshInterval(30000);
      tick(30000);
      expect(service.getRefreshInterval()).toBe(30000);
      expect(notifyCount).toBe(1);

      service.ngOnDestroy();
    }));
  });
});