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