]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-trash-purge-modal/rbd-trash-purge-modal.component.spec.ts
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / rbd-trash-purge-modal / rbd-trash-purge-modal.component.spec.ts
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 { BsModalRef } from 'ngx-bootstrap/modal';
11 import { ToastrModule } from 'ngx-toastr';
12
13 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
14 import { Permission } from '../../../shared/models/permissions';
15 import { NotificationService } from '../../../shared/services/notification.service';
16 import { SharedModule } from '../../../shared/shared.module';
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: [BsModalRef, i18nProviders]
34 });
35
36 beforeEach(() => {
37 fixture = TestBed.createComponent(RbdTrashPurgeModalComponent);
38 httpTesting = TestBed.get(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.expectOne('api/summary');
70 httpTesting.verify();
71 });
72
73 describe('should call purge', () => {
74 let notificationService: NotificationService;
75 let modalRef: BsModalRef;
76 let req: TestRequest;
77
78 beforeEach(() => {
79 fixture.detectChanges();
80 notificationService = TestBed.get(NotificationService);
81 modalRef = TestBed.get(BsModalRef);
82
83 component.purgeForm.patchValue({ poolName: 'foo' });
84
85 spyOn(modalRef, 'hide').and.stub();
86 spyOn(component.purgeForm, 'setErrors').and.stub();
87 spyOn(notificationService, 'show').and.stub();
88
89 component.purge();
90
91 req = httpTesting.expectOne('api/block/image/trash/purge/?pool_name=foo');
92 });
93
94 it('with success', () => {
95 req.flush(null);
96 expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(0);
97 expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
98 });
99
100 it('with failure', () => {
101 req.flush(null, { status: 500, statusText: 'failure' });
102 expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(1);
103 expect(component.modalRef.hide).toHaveBeenCalledTimes(0);
104 });
105 });
106 });