]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
b73a495af8e19e6a7e17adb6b807297b8cdeb913
[ceph.git] /
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import * as _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9
10 import { configureTestBed, i18nProviders } from '../../../../../testing/unit-test-helper';
11 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
12 import { NotificationService } from '../../../../shared/services/notification.service';
13 import { SharedModule } from '../../../../shared/shared.module';
14 import { OsdFlagsModalComponent } from './osd-flags-modal.component';
15
16 function getFlagsArray(component: OsdFlagsModalComponent) {
17   const allFlags = _.cloneDeep(component.allFlags);
18   allFlags['purged_snapdirs'].value = true;
19   allFlags['pause'].value = true;
20   return _.toArray(allFlags);
21 }
22
23 describe('OsdFlagsModalComponent', () => {
24   let component: OsdFlagsModalComponent;
25   let fixture: ComponentFixture<OsdFlagsModalComponent>;
26   let httpTesting: HttpTestingController;
27
28   configureTestBed({
29     imports: [
30       ReactiveFormsModule,
31       SharedModule,
32       HttpClientTestingModule,
33       RouterTestingModule,
34       ToastrModule.forRoot()
35     ],
36     declarations: [OsdFlagsModalComponent],
37     providers: [i18nProviders, NgbActiveModal]
38   });
39
40   beforeEach(() => {
41     httpTesting = TestBed.inject(HttpTestingController);
42     fixture = TestBed.createComponent(OsdFlagsModalComponent);
43     component = fixture.componentInstance;
44   });
45
46   it('should create', () => {
47     expect(component).toBeTruthy();
48   });
49
50   it('should finish running ngOnInit', () => {
51     fixture.detectChanges();
52
53     const flags = getFlagsArray(component);
54
55     const req = httpTesting.expectOne('api/osd/flags');
56     req.flush(['purged_snapdirs', 'pause', 'foo']);
57
58     expect(component.flags).toEqual(flags);
59     expect(component.unknownFlags).toEqual(['foo']);
60   });
61
62   describe('test submitAction', function () {
63     let notificationType: NotificationType;
64     let notificationService: NotificationService;
65     let bsModalRef: NgbActiveModal;
66
67     beforeEach(() => {
68       notificationService = TestBed.inject(NotificationService);
69       spyOn(notificationService, 'show').and.callFake((type) => {
70         notificationType = type;
71       });
72
73       bsModalRef = TestBed.inject(NgbActiveModal);
74       spyOn(bsModalRef, 'close').and.callThrough();
75       component.unknownFlags = ['foo'];
76     });
77
78     it('should run submitAction', () => {
79       component.flags = getFlagsArray(component);
80       component.submitAction();
81       const req = httpTesting.expectOne('api/osd/flags');
82       req.flush(['purged_snapdirs', 'pause', 'foo']);
83       expect(req.request.body).toEqual({ flags: ['pause', 'purged_snapdirs', 'foo'] });
84
85       expect(notificationType).toBe(NotificationType.success);
86       expect(component.activeModal.close).toHaveBeenCalledTimes(1);
87     });
88
89     it('should hide modal if request fails', () => {
90       component.flags = [];
91       component.submitAction();
92       const req = httpTesting.expectOne('api/osd/flags');
93       req.flush([], { status: 500, statusText: 'failure' });
94
95       expect(notificationService.show).toHaveBeenCalledTimes(0);
96       expect(component.activeModal.close).toHaveBeenCalledTimes(1);
97     });
98   });
99 });