]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
3264e62dd06cf1b79eb6309f6bcc460e353a26b4
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { FormControl, ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import { ToastrModule } from 'ngx-toastr';
8 import { of as observableOf } from 'rxjs';
9
10 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
11 import { RgwBucketService } from '../../../shared/api/rgw-bucket.service';
12 import { RgwSiteService } from '../../../shared/api/rgw-site.service';
13 import { NotificationType } from '../../../shared/enum/notification-type.enum';
14 import { NotificationService } from '../../../shared/services/notification.service';
15 import { SharedModule } from '../../../shared/shared.module';
16 import { RgwBucketFormComponent } from './rgw-bucket-form.component';
17
18 describe('RgwBucketFormComponent', () => {
19   let component: RgwBucketFormComponent;
20   let fixture: ComponentFixture<RgwBucketFormComponent>;
21   let rgwBucketService: RgwBucketService;
22   let getPlacementTargetsSpy;
23
24   configureTestBed({
25     declarations: [RgwBucketFormComponent],
26     imports: [
27       HttpClientTestingModule,
28       ReactiveFormsModule,
29       RouterTestingModule,
30       SharedModule,
31       ToastrModule.forRoot()
32     ],
33     providers: [i18nProviders]
34   });
35
36   beforeEach(() => {
37     fixture = TestBed.createComponent(RgwBucketFormComponent);
38     component = fixture.componentInstance;
39     rgwBucketService = TestBed.get(RgwBucketService);
40     getPlacementTargetsSpy = spyOn(TestBed.get(RgwSiteService), 'getPlacementTargets');
41   });
42
43   it('should create', () => {
44     expect(component).toBeTruthy();
45   });
46
47   describe('bucketNameValidator', () => {
48     it('should validate name (1/4)', () => {
49       const validatorFn = component.bucketNameValidator();
50       const ctrl = new FormControl('');
51       const validatorPromise = validatorFn(ctrl);
52       expect(validatorPromise instanceof Promise).toBeTruthy();
53       if (validatorPromise instanceof Promise) {
54         validatorPromise.then((resp) => {
55           expect(resp).toBe(null);
56         });
57       }
58     });
59
60     it('should validate name (2/4)', () => {
61       const validatorFn = component.bucketNameValidator();
62       const ctrl = new FormControl('ab');
63       ctrl.markAsDirty();
64       const validatorPromise = validatorFn(ctrl);
65       expect(validatorPromise instanceof Promise).toBeTruthy();
66       if (validatorPromise instanceof Promise) {
67         validatorPromise.then((resp) => {
68           expect(resp.bucketNameInvalid).toBeTruthy();
69         });
70       }
71     });
72
73     it('should validate name (3/4)', () => {
74       const validatorFn = component.bucketNameValidator();
75       const ctrl = new FormControl('abc');
76       ctrl.markAsDirty();
77       const validatorPromise = validatorFn(ctrl);
78       expect(validatorPromise instanceof Promise).toBeTruthy();
79       if (validatorPromise instanceof Promise) {
80         validatorPromise.then((resp) => {
81           expect(resp).toBe(null);
82         });
83       }
84     });
85
86     it('should validate name (4/4)', () => {
87       spyOn(rgwBucketService, 'enumerate').and.returnValue(observableOf(['abcd']));
88       const validatorFn = component.bucketNameValidator();
89       const ctrl = new FormControl('abcd');
90       ctrl.markAsDirty();
91       const validatorPromise = validatorFn(ctrl);
92       expect(validatorPromise instanceof Promise).toBeTruthy();
93       if (validatorPromise instanceof Promise) {
94         validatorPromise.then((resp) => {
95           expect(resp instanceof Object).toBeTruthy();
96           expect(resp.bucketNameExists).toBeTruthy();
97         });
98       }
99     });
100
101     it('should get zonegroup and placement targets', () => {
102       const payload = {
103         zonegroup: 'default',
104         placement_targets: [
105           {
106             name: 'default-placement',
107             data_pool: 'default.rgw.buckets.data'
108           },
109           {
110             name: 'placement-target2',
111             data_pool: 'placement-target2.rgw.buckets.data'
112           }
113         ]
114       };
115       getPlacementTargetsSpy.and.returnValue(observableOf(payload));
116       fixture.detectChanges();
117
118       expect(component.zonegroup).toBe(payload.zonegroup);
119       const placementTargets = [];
120       for (const placementTarget of payload['placement_targets']) {
121         placementTarget['description'] = `${placementTarget['name']} (pool: ${
122           placementTarget['data_pool']
123         })`;
124         placementTargets.push(placementTarget);
125       }
126       expect(component.placementTargets).toEqual(placementTargets);
127     });
128   });
129
130   describe('submit form', () => {
131     let notificationService: NotificationService;
132
133     beforeEach(() => {
134       spyOn(TestBed.get(Router), 'navigate').and.stub();
135       notificationService = TestBed.get(NotificationService);
136       spyOn(notificationService, 'show');
137     });
138
139     it('tests create success notification', () => {
140       spyOn(rgwBucketService, 'create').and.returnValue(observableOf([]));
141       component.editing = false;
142       component.bucketForm.markAsDirty();
143       component.submit();
144       expect(notificationService.show).toHaveBeenCalledWith(
145         NotificationType.success,
146         'Created Object Gateway bucket ""'
147       );
148     });
149
150     it('tests update success notification', () => {
151       spyOn(rgwBucketService, 'update').and.returnValue(observableOf([]));
152       component.editing = true;
153       component.bucketForm.markAsDirty();
154       component.submit();
155       expect(notificationService.show).toHaveBeenCalledWith(
156         NotificationType.success,
157         'Updated Object Gateway bucket ""'
158       );
159     });
160   });
161 });