]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
09420cca7161e1e5f863b6cc02b575255e846268
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { ToastModule } from 'ng2-toastr';
6 import { BsModalRef, ModalModule } from 'ngx-bootstrap';
7 import { throwError as observableThrowError } from 'rxjs';
8
9 import { configureTestBed } from '../../../../testing/unit-test-helper';
10 import { ApiModule } from '../../../shared/api/api.module';
11 import { RbdService } from '../../../shared/api/rbd.service';
12 import { ComponentsModule } from '../../../shared/components/components.module';
13 import { DataTableModule } from '../../../shared/datatable/datatable.module';
14 import { Permissions } from '../../../shared/models/permissions';
15 import { PipesModule } from '../../../shared/pipes/pipes.module';
16 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
17 import { NotificationService } from '../../../shared/services/notification.service';
18 import { ServicesModule } from '../../../shared/services/services.module';
19 import { RbdSnapshotListComponent } from './rbd-snapshot-list.component';
20
21 describe('RbdSnapshotListComponent', () => {
22   let component: RbdSnapshotListComponent;
23   let fixture: ComponentFixture<RbdSnapshotListComponent>;
24
25   const fakeAuthStorageService = {
26     isLoggedIn: () => {
27       return true;
28     },
29     getPermissions: () => {
30       return new Permissions({ 'rbd-image': ['read', 'update', 'create', 'delete'] });
31     }
32   };
33
34   configureTestBed({
35     declarations: [RbdSnapshotListComponent],
36     imports: [
37       DataTableModule,
38       ComponentsModule,
39       ModalModule.forRoot(),
40       ToastModule.forRoot(),
41       ServicesModule,
42       ApiModule,
43       HttpClientTestingModule,
44       RouterTestingModule,
45       PipesModule
46     ],
47     providers: [{ provide: AuthStorageService, useValue: fakeAuthStorageService }]
48   });
49
50   beforeEach(() => {
51     fixture = TestBed.createComponent(RbdSnapshotListComponent);
52     component = fixture.componentInstance;
53     fixture.detectChanges();
54   });
55
56   it('should create', () => {
57     expect(component).toBeTruthy();
58   });
59
60   describe('api delete request', () => {
61     let called;
62     let rbdService: RbdService;
63     let notificationService: NotificationService;
64     let authStorageService: AuthStorageService;
65
66     beforeEach(() => {
67       called = false;
68       rbdService = new RbdService(null);
69       notificationService = new NotificationService(null, null);
70       authStorageService = new AuthStorageService();
71       authStorageService.set('user', { 'rbd-image': ['create', 'read', 'update', 'delete'] });
72       component = new RbdSnapshotListComponent(
73         authStorageService,
74         null,
75         null,
76         null,
77         rbdService,
78         null,
79         notificationService
80       );
81       spyOn(rbdService, 'deleteSnapshot').and.returnValue(observableThrowError({ status: 500 }));
82       spyOn(notificationService, 'notifyTask').and.stub();
83       component.modalRef = new BsModalRef();
84       component.modalRef.content = {
85         stopLoadingSpinner: () => (called = true)
86       };
87     });
88
89     it('should call stopLoadingSpinner if the request fails', <any>fakeAsync(() => {
90       expect(called).toBe(false);
91       component._asyncTask('deleteSnapshot', 'rbd/snap/delete', 'someName');
92       tick(500);
93       expect(called).toBe(true);
94     }));
95   });
96 });