]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
749236c0da0632d9ea9719526235cae986278973
[ceph.git] /
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { ToastModule } from 'ng2-toastr';
7 import { BsModalRef } from 'ngx-bootstrap/modal';
8
9 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
10 import { Permission } from '../../../shared/models/permissions';
11 import { NotificationService } from '../../../shared/services/notification.service';
12 import { SharedModule } from '../../../shared/shared.module';
13 import { RbdTrashPurgeModalComponent } from './rbd-trash-purge-modal.component';
14
15 describe('RbdTrashPurgeModalComponent', () => {
16   let component: RbdTrashPurgeModalComponent;
17   let fixture: ComponentFixture<RbdTrashPurgeModalComponent>;
18   let httpTesting: HttpTestingController;
19
20   configureTestBed({
21     imports: [
22       HttpClientTestingModule,
23       ReactiveFormsModule,
24       SharedModule,
25       ToastModule.forRoot(),
26       RouterTestingModule
27     ],
28     declarations: [RbdTrashPurgeModalComponent],
29     providers: [BsModalRef, i18nProviders]
30   });
31
32   beforeEach(() => {
33     fixture = TestBed.createComponent(RbdTrashPurgeModalComponent);
34     httpTesting = TestBed.get(HttpTestingController);
35     component = fixture.componentInstance;
36   });
37
38   it('should create', () => {
39     fixture.detectChanges();
40     expect(component).toBeTruthy();
41   });
42
43   it('should finish ngOnInit', fakeAsync(() => {
44     component.poolPermission = new Permission(['read', 'create', 'update', 'delete']);
45     fixture.detectChanges();
46     const req = httpTesting.expectOne('api/pool?attrs=pool_name,application_metadata');
47     req.flush([
48       {
49         application_metadata: ['foo'],
50         pool_name: 'bar'
51       },
52       {
53         application_metadata: ['rbd'],
54         pool_name: 'baz'
55       }
56     ]);
57     tick();
58     expect(component.pools).toEqual(['baz']);
59     expect(component.purgeForm).toBeTruthy();
60   }));
61
62   it('should call ngOnInit without pool permissions', () => {
63     component.poolPermission = new Permission([]);
64     component.ngOnInit();
65     httpTesting.expectOne('api/summary');
66     httpTesting.verify();
67   });
68
69   describe('should call purge', () => {
70     let notificationService: NotificationService;
71     let modalRef: BsModalRef;
72     let req;
73
74     beforeEach(() => {
75       fixture.detectChanges();
76       notificationService = TestBed.get(NotificationService);
77       modalRef = TestBed.get(BsModalRef);
78
79       component.purgeForm.patchValue({ poolName: 'foo' });
80
81       spyOn(modalRef, 'hide').and.stub();
82       spyOn(component.purgeForm, 'setErrors').and.stub();
83       spyOn(notificationService, 'show').and.stub();
84
85       component.purge();
86
87       req = httpTesting.expectOne('api/block/image/trash/purge/?pool_name=foo');
88     });
89
90     it('with success', () => {
91       req.flush(null);
92       expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(0);
93       expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
94     });
95
96     it('with failure', () => {
97       req.flush(null, { status: 500, statusText: 'failure' });
98       expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(1);
99       expect(component.modalRef.hide).toHaveBeenCalledTimes(0);
100     });
101   });
102 });