]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-flags-modal/osd-flags-modal.component.spec.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / osd / osd-flags-modal / osd-flags-modal.component.spec.ts
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 _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9
10 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
11 import { NotificationService } from '~/app/shared/services/notification.service';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { configureTestBed } from '~/testing/unit-test-helper';
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: [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 });