summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/modal.service.spec.ts
blob: 4e5ed061d830c04f0db3ca8797733d8b3d2ae224 (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
import { Component } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { NgbActiveModal, NgbModal, NgbModalModule } from '@ng-bootstrap/ng-bootstrap';

import { configureTestBed } from '~/testing/unit-test-helper';
import { ModalService } from './modal.service';

@Component({
  template: ``
})
class MockComponent {
  foo = '';

  constructor(public activeModal: NgbActiveModal) {}
}

describe('ModalService', () => {
  let service: ModalService;
  let ngbModal: NgbModal;

  configureTestBed({ declarations: [MockComponent], imports: [NgbModalModule] }, [MockComponent]);

  beforeEach(() => {
    service = TestBed.inject(ModalService);
    ngbModal = TestBed.inject(NgbModal);
  });

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

  it('should call NgbModal.open when show is called', () => {
    spyOn(ngbModal, 'open').and.callThrough();

    const modaRef = service.show(MockComponent, { foo: 'bar' });

    expect(ngbModal.open).toBeCalled();
    expect(modaRef.componentInstance.foo).toBe('bar');
    expect(modaRef.componentInstance.activeModal).toBeTruthy();
  });

  it('should call dismissAll and hasOpenModals', fakeAsync(() => {
    spyOn(ngbModal, 'dismissAll').and.callThrough();
    spyOn(ngbModal, 'hasOpenModals').and.callThrough();

    expect(ngbModal.hasOpenModals()).toBeFalsy();

    service.show(MockComponent, { foo: 'bar' });
    expect(service.hasOpenModals()).toBeTruthy();

    service.dismissAll();
    tick();
    expect(service.hasOpenModals()).toBeFalsy();

    expect(ngbModal.dismissAll).toBeCalled();
    expect(ngbModal.hasOpenModals).toBeCalled();
  }));
});