2 HttpClientTestingModule,
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';
10 import { BsModalRef } from 'ngx-bootstrap/modal';
11 import { ToastrModule } from 'ngx-toastr';
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';
19 describe('RbdTrashPurgeModalComponent', () => {
20 let component: RbdTrashPurgeModalComponent;
21 let fixture: ComponentFixture<RbdTrashPurgeModalComponent>;
22 let httpTesting: HttpTestingController;
26 HttpClientTestingModule,
29 ToastrModule.forRoot(),
32 declarations: [RbdTrashPurgeModalComponent],
33 providers: [BsModalRef, i18nProviders]
37 fixture = TestBed.createComponent(RbdTrashPurgeModalComponent);
38 httpTesting = TestBed.get(HttpTestingController);
39 component = fixture.componentInstance;
42 it('should create', () => {
43 fixture.detectChanges();
44 expect(component).toBeTruthy();
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');
53 application_metadata: ['foo'],
57 application_metadata: ['rbd'],
62 expect(component.pools).toEqual(['baz']);
63 expect(component.purgeForm).toBeTruthy();
66 it('should call ngOnInit without pool permissions', () => {
67 component.poolPermission = new Permission([]);
69 httpTesting.expectOne('api/summary');
73 describe('should call purge', () => {
74 let notificationService: NotificationService;
75 let modalRef: BsModalRef;
79 fixture.detectChanges();
80 notificationService = TestBed.get(NotificationService);
81 modalRef = TestBed.get(BsModalRef);
83 component.purgeForm.patchValue({ poolName: 'foo' });
85 spyOn(modalRef, 'hide').and.stub();
86 spyOn(component.purgeForm, 'setErrors').and.stub();
87 spyOn(notificationService, 'show').and.stub();
91 req = httpTesting.expectOne('api/block/image/trash/purge/?pool_name=foo');
94 it('with success', () => {
96 expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(0);
97 expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
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);