summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/motd-notification.service.spec.ts
blob: 267e6aa57c15b44d410d3014b944b04b58c92d80 (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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { Motd } from '~/app/shared/api/motd.service';
import { configureTestBed } from '~/testing/unit-test-helper';
import { MotdNotificationService } from './motd-notification.service';

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

  configureTestBed({
    providers: [MotdNotificationService],
    imports: [HttpClientTestingModule]
  });

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

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

  it('should hide [1]', () => {
    spyOn(service.motdSource, 'next');
    spyOn(service.motdSource, 'getValue').and.returnValue({
      severity: 'info',
      expires: '',
      message: 'foo',
      md5: 'acbd18db4cc2f85cedef654fccc4a4d8'
    });
    service.hide();
    expect(localStorage.getItem('dashboard_motd_hidden')).toBe(
      'info:acbd18db4cc2f85cedef654fccc4a4d8'
    );
    expect(sessionStorage.getItem('dashboard_motd_hidden')).toBeNull();
    expect(service.motdSource.next).toBeCalledWith(null);
  });

  it('should hide [2]', () => {
    spyOn(service.motdSource, 'getValue').and.returnValue({
      severity: 'warning',
      expires: '',
      message: 'bar',
      md5: '37b51d194a7513e45b56f6524f2d51f2'
    });
    service.hide();
    expect(sessionStorage.getItem('dashboard_motd_hidden')).toBe(
      'warning:37b51d194a7513e45b56f6524f2d51f2'
    );
    expect(localStorage.getItem('dashboard_motd_hidden')).toBeNull();
  });

  it('should process response [1]', () => {
    const motd: Motd = {
      severity: 'danger',
      expires: '',
      message: 'foo',
      md5: 'acbd18db4cc2f85cedef654fccc4a4d8'
    };
    spyOn(service.motdSource, 'next');
    service.processResponse(motd);
    expect(service.motdSource.next).toBeCalledWith(motd);
  });

  it('should process response [2]', () => {
    const motd: Motd = {
      severity: 'warning',
      expires: '',
      message: 'foo',
      md5: 'acbd18db4cc2f85cedef654fccc4a4d8'
    };
    localStorage.setItem('dashboard_motd_hidden', 'info');
    service.processResponse(motd);
    expect(sessionStorage.getItem('dashboard_motd_hidden')).toBeNull();
    expect(localStorage.getItem('dashboard_motd_hidden')).toBeNull();
  });

  it('should process response [3]', () => {
    const motd: Motd = {
      severity: 'info',
      expires: '',
      message: 'foo',
      md5: 'acbd18db4cc2f85cedef654fccc4a4d8'
    };
    spyOn(service.motdSource, 'next');
    localStorage.setItem('dashboard_motd_hidden', 'info:acbd18db4cc2f85cedef654fccc4a4d8');
    service.processResponse(motd);
    expect(service.motdSource.next).not.toBeCalled();
  });

  it('should process response [4]', () => {
    const motd: Motd = {
      severity: 'info',
      expires: '',
      message: 'foo',
      md5: 'acbd18db4cc2f85cedef654fccc4a4d8'
    };
    spyOn(service.motdSource, 'next');
    localStorage.setItem('dashboard_motd_hidden', 'info:37b51d194a7513e45b56f6524f2d51f2');
    service.processResponse(motd);
    expect(service.motdSource.next).toBeCalled();
  });

  it('should process response [5]', () => {
    const motd: Motd = {
      severity: 'info',
      expires: '',
      message: 'foo',
      md5: 'acbd18db4cc2f85cedef654fccc4a4d8'
    };
    spyOn(service.motdSource, 'next');
    localStorage.setItem('dashboard_motd_hidden', 'danger:acbd18db4cc2f85cedef654fccc4a4d8');
    service.processResponse(motd);
    expect(service.motdSource.next).toBeCalled();
  });
});