]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
efee2aacfba1db26de7b788b2158c5d5aa549b6a
[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 { NgbPopoverModule } from '@ng-bootstrap/ng-bootstrap';
7 import moment from 'moment';
8 import { ToastrModule } from 'ngx-toastr';
9
10 import { NotificationService } from '~/app/shared/services/notification.service';
11 import { SharedModule } from '~/app/shared/shared.module';
12 import { configureTestBed } from '~/testing/unit-test-helper';
13 import { RbdTrashMoveModalComponent } from './rbd-trash-move-modal.component';
14 import {
15   CheckboxModule,
16   DatePickerModule,
17   ModalModule,
18   TimePickerModule
19 } from 'carbon-components-angular';
20 import { DateTimePickerComponent } from '~/app/shared/components/date-time-picker/date-time-picker.component';
21
22 describe('RbdTrashMoveModalComponent', () => {
23   let component: RbdTrashMoveModalComponent;
24   let fixture: ComponentFixture<RbdTrashMoveModalComponent>;
25   let httpTesting: HttpTestingController;
26
27   configureTestBed({
28     imports: [
29       ReactiveFormsModule,
30       HttpClientTestingModule,
31       RouterTestingModule,
32       SharedModule,
33       ToastrModule.forRoot(),
34       NgbPopoverModule,
35       ModalModule,
36       CheckboxModule,
37       DatePickerModule,
38       TimePickerModule
39     ],
40     declarations: [RbdTrashMoveModalComponent, DateTimePickerComponent],
41     providers: [
42       { provide: 'poolName', useValue: 'foo' },
43       { provide: 'imageName', useValue: 'bar' },
44       { provide: 'namespace', useValue: '' },
45       { provide: 'hasSnapshots', useValue: false }
46     ]
47   });
48
49   beforeEach(() => {
50     fixture = TestBed.createComponent(RbdTrashMoveModalComponent);
51     component = fixture.componentInstance;
52     httpTesting = TestBed.inject(HttpTestingController);
53
54     component.poolName = 'foo';
55     component.imageName = 'bar';
56
57     fixture.detectChanges();
58   });
59
60   it('should create', () => {
61     expect(component).toBeTruthy();
62     expect(component.moveForm).toBeDefined();
63   });
64
65   it('should finish running ngOnInit', () => {
66     expect(component.pattern).toEqual('foo/bar');
67   });
68
69   describe('should call moveImage', () => {
70     let notificationService: NotificationService;
71
72     beforeEach(() => {
73       notificationService = TestBed.inject(NotificationService);
74       spyOn(notificationService, 'show').and.stub();
75       spyOn(component, 'closeModal').and.callThrough();
76     });
77
78     afterEach(() => {
79       expect(notificationService.show).toHaveBeenCalledTimes(1);
80       expect(component.closeModal).toHaveBeenCalledTimes(1);
81     });
82
83     it('with normal delay', () => {
84       component.moveImage();
85       const req = httpTesting.expectOne('api/block/image/foo%2Fbar/move_trash');
86       req.flush(null);
87       expect(req.request.body).toEqual({ delay: 0 });
88     });
89
90     it('with delay < 0', () => {
91       const oldDate = moment().subtract(24, 'hour').toDate();
92       component.moveForm.patchValue({ expiresAt: oldDate });
93
94       component.moveImage();
95       const req = httpTesting.expectOne('api/block/image/foo%2Fbar/move_trash');
96       req.flush(null);
97       expect(req.request.body).toEqual({ delay: 0 });
98     });
99
100     it('with delay < 0', () => {
101       const oldDate = moment().add(24, 'hour').toISOString();
102       component.moveForm.patchValue({ expiresAt: oldDate });
103
104       component.moveImage();
105       const req = httpTesting.expectOne('api/block/image/foo%2Fbar/move_trash');
106       req.flush(null);
107       expect(req.request.body.delay).toBeGreaterThan(76390);
108     });
109   });
110 });