]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
05f61c4d99f43f3370cb2104a98769dd4207184c
[ceph.git] /
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import * as moment from 'moment';
8 import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
9 import { ToastrModule } from 'ngx-toastr';
10
11 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { NotificationService } from '../../../shared/services/notification.service';
13 import { SharedModule } from '../../../shared/shared.module';
14 import { RbdTrashMoveModalComponent } from './rbd-trash-move-modal.component';
15
16 describe('RbdTrashMoveModalComponent', () => {
17   let component: RbdTrashMoveModalComponent;
18   let fixture: ComponentFixture<RbdTrashMoveModalComponent>;
19   let httpTesting: HttpTestingController;
20
21   configureTestBed({
22     imports: [
23       ReactiveFormsModule,
24       HttpClientTestingModule,
25       RouterTestingModule,
26       SharedModule,
27       ToastrModule.forRoot(),
28       BsDatepickerModule.forRoot()
29     ],
30     declarations: [RbdTrashMoveModalComponent],
31     providers: [NgbActiveModal, i18nProviders]
32   });
33
34   beforeEach(() => {
35     fixture = TestBed.createComponent(RbdTrashMoveModalComponent);
36     component = fixture.componentInstance;
37     httpTesting = TestBed.inject(HttpTestingController);
38
39     component.poolName = 'foo';
40     component.imageName = 'bar';
41
42     fixture.detectChanges();
43   });
44
45   it('should create', () => {
46     expect(component).toBeTruthy();
47     expect(component.moveForm).toBeDefined();
48   });
49
50   it('should finish running ngOnInit', () => {
51     expect(component.pattern).toEqual('foo/bar');
52   });
53
54   describe('should call moveImage', () => {
55     let notificationService: NotificationService;
56
57     beforeEach(() => {
58       notificationService = TestBed.inject(NotificationService);
59       spyOn(notificationService, 'show').and.stub();
60       spyOn(component.activeModal, 'close').and.callThrough();
61     });
62
63     afterEach(() => {
64       expect(notificationService.show).toHaveBeenCalledTimes(1);
65       expect(component.activeModal.close).toHaveBeenCalledTimes(1);
66     });
67
68     it('with normal delay', () => {
69       component.moveImage();
70       const req = httpTesting.expectOne('api/block/image/foo%2Fbar/move_trash');
71       req.flush(null);
72       expect(req.request.body).toEqual({ delay: 0 });
73     });
74
75     it('with delay < 0', () => {
76       const oldDate = moment().subtract(24, 'hour').toDate();
77       component.moveForm.patchValue({ expiresAt: oldDate });
78
79       component.moveImage();
80       const req = httpTesting.expectOne('api/block/image/foo%2Fbar/move_trash');
81       req.flush(null);
82       expect(req.request.body).toEqual({ delay: 0 });
83     });
84
85     it('with delay < 0', () => {
86       const oldDate = moment().add(24, 'hour').toISOString();
87       component.moveForm.patchValue({ expiresAt: oldDate });
88
89       component.moveImage();
90       const req = httpTesting.expectOne('api/block/image/foo%2Fbar/move_trash');
91       req.flush(null);
92       expect(req.request.body.delay).toBeGreaterThan(86390);
93     });
94   });
95 });