]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
7f1708fff44abbf21f8794b8079aa958932004e6
[ceph.git] /
1 import {
2   HttpClientTestingModule,
3   HttpTestingController,
4   TestRequest
5 } from '@angular/common/http/testing';
6 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
7 import { ReactiveFormsModule } from '@angular/forms';
8 import { RouterTestingModule } from '@angular/router/testing';
9
10 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
11 import { ToastrModule } from 'ngx-toastr';
12
13 import { Permission } from '~/app/shared/models/permissions';
14 import { NotificationService } from '~/app/shared/services/notification.service';
15 import { SharedModule } from '~/app/shared/shared.module';
16 import { configureTestBed } from '~/testing/unit-test-helper';
17 import { RbdTrashPurgeModalComponent } from './rbd-trash-purge-modal.component';
18
19 describe('RbdTrashPurgeModalComponent', () => {
20   let component: RbdTrashPurgeModalComponent;
21   let fixture: ComponentFixture<RbdTrashPurgeModalComponent>;
22   let httpTesting: HttpTestingController;
23
24   configureTestBed({
25     imports: [
26       HttpClientTestingModule,
27       ReactiveFormsModule,
28       SharedModule,
29       ToastrModule.forRoot(),
30       RouterTestingModule
31     ],
32     declarations: [RbdTrashPurgeModalComponent],
33     providers: [NgbActiveModal]
34   });
35
36   beforeEach(() => {
37     fixture = TestBed.createComponent(RbdTrashPurgeModalComponent);
38     httpTesting = TestBed.inject(HttpTestingController);
39     component = fixture.componentInstance;
40   });
41
42   it('should create', () => {
43     fixture.detectChanges();
44     expect(component).toBeTruthy();
45   });
46
47   it('should finish ngOnInit', fakeAsync(() => {
48     component.poolPermission = new Permission(['read', 'create', 'update', 'delete']);
49     fixture.detectChanges();
50     const req = httpTesting.expectOne('api/pool?attrs=pool_name,application_metadata');
51     req.flush([
52       {
53         application_metadata: ['foo'],
54         pool_name: 'bar'
55       },
56       {
57         application_metadata: ['rbd'],
58         pool_name: 'baz'
59       }
60     ]);
61     tick();
62     expect(component.pools).toEqual(['baz']);
63     expect(component.purgeForm).toBeTruthy();
64   }));
65
66   it('should call ngOnInit without pool permissions', () => {
67     component.poolPermission = new Permission([]);
68     component.ngOnInit();
69     httpTesting.verify();
70   });
71
72   describe('should call purge', () => {
73     let notificationService: NotificationService;
74     let activeModal: NgbActiveModal;
75     let req: TestRequest;
76
77     beforeEach(() => {
78       fixture.detectChanges();
79       notificationService = TestBed.inject(NotificationService);
80       activeModal = TestBed.inject(NgbActiveModal);
81
82       component.purgeForm.patchValue({ poolName: 'foo' });
83
84       spyOn(activeModal, 'close').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.activeModal.close).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.activeModal.close).toHaveBeenCalledTimes(0);
103     });
104   });
105 });