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';
6 import { ToastModule } from 'ng2-toastr';
7 import { BsModalRef } from 'ngx-bootstrap/modal';
9 import { configureTestBed, i18nProviders } 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';
15 describe('RbdTrashPurgeModalComponent', () => {
16 let component: RbdTrashPurgeModalComponent;
17 let fixture: ComponentFixture<RbdTrashPurgeModalComponent>;
18 let httpTesting: HttpTestingController;
22 HttpClientTestingModule,
25 ToastModule.forRoot(),
28 declarations: [RbdTrashPurgeModalComponent],
29 providers: [BsModalRef, i18nProviders]
33 fixture = TestBed.createComponent(RbdTrashPurgeModalComponent);
34 httpTesting = TestBed.get(HttpTestingController);
35 component = fixture.componentInstance;
38 it('should create', () => {
39 fixture.detectChanges();
40 expect(component).toBeTruthy();
43 it('should finish ngOnInit', fakeAsync(() => {
44 component.poolPermission = new Permission(['read', 'create', 'update', 'delete']);
45 fixture.detectChanges();
46 const req = httpTesting.expectOne('api/pool?attrs=pool_name,application_metadata');
49 application_metadata: ['foo'],
53 application_metadata: ['rbd'],
58 expect(component.pools).toEqual(['baz']);
59 expect(component.purgeForm).toBeTruthy();
62 it('should call ngOnInit without pool permissions', () => {
63 component.poolPermission = new Permission([]);
65 httpTesting.expectOne('api/summary');
69 describe('should call purge', () => {
70 let notificationService: NotificationService;
71 let modalRef: BsModalRef;
75 fixture.detectChanges();
76 notificationService = TestBed.get(NotificationService);
77 modalRef = TestBed.get(BsModalRef);
79 component.purgeForm.patchValue({ poolName: 'foo' });
81 spyOn(modalRef, 'hide').and.stub();
82 spyOn(component.purgeForm, 'setErrors').and.stub();
83 spyOn(notificationService, 'show').and.stub();
87 req = httpTesting.expectOne('api/block/image/trash/purge/?pool_name=foo');
90 it('with success', () => {
92 expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(0);
93 expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
96 it('with failure', () => {
97 req.flush(null, { status: 500, statusText: 'failure' });
98 expect(component.purgeForm.setErrors).toHaveBeenCalledTimes(1);
99 expect(component.modalRef.hide).toHaveBeenCalledTimes(0);