]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
0ce979974c4bbc6511df6367591df982f9fae05c
[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 } 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]
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(
44     'should finish ngOnInit',
45     fakeAsync(() => {
46       component.poolPermission = new Permission(['read', 'create', 'update', 'delete']);
47       fixture.detectChanges();
48       const req = httpTesting.expectOne('api/pool?attrs=pool_name,application_metadata');
49       req.flush([
50         {
51           application_metadata: ['foo'],
52           pool_name: 'bar'
53         },
54         {
55           application_metadata: ['rbd'],
56           pool_name: 'baz'
57         }
58       ]);
59       tick();
60       expect(component.pools).toEqual(['baz']);
61       expect(component.purgeForm).toBeTruthy();
62     })
63   );
64
65   it('should call ngOnInit without pool permissions', () => {
66     component.poolPermission = new Permission([]);
67     component.ngOnInit();
68     httpTesting.expectOne('api/summary');
69     httpTesting.verify();
70   });
71
72   describe('should call purge', () => {
73     let notificationService: NotificationService;
74     let modalRef: BsModalRef;
75     let req;
76
77     beforeEach(() => {
78       fixture.detectChanges();
79       notificationService = TestBed.get(NotificationService);
80       modalRef = TestBed.get(BsModalRef);
81
82       component.purgeForm.patchValue({ poolName: 'foo' });
83
84       spyOn(modalRef, 'hide').and.stub();
85       spyOn(component.purgeForm, 'setErrors').and.stub();
86       spyOn(notificationService, 'show').and.stub();
87
88       component.purge();
89
90       req = httpTesting.expectOne('api/block/image/trash/purge/?pool_name=foo');
91     });
92
93     it('with success', () => {
94       req.flush(null);
95       expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(0);
96       expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
97     });
98
99     it('with failure', () => {
100       req.flush(null, { status: 500, statusText: 'failure' });
101       expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(1);
102       expect(component.modalRef.hide).toHaveBeenCalledTimes(0);
103     });
104   });
105 });