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';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of as observableOf } from 'rxjs';
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';
18 describe('RgwBucketFormComponent', () => {
19 let component: RgwBucketFormComponent;
20 let fixture: ComponentFixture<RgwBucketFormComponent>;
21 let rgwBucketService: RgwBucketService;
22 let getPlacementTargetsSpy;
25 declarations: [RgwBucketFormComponent],
27 HttpClientTestingModule,
31 ToastrModule.forRoot()
33 providers: [i18nProviders]
37 fixture = TestBed.createComponent(RgwBucketFormComponent);
38 component = fixture.componentInstance;
39 rgwBucketService = TestBed.get(RgwBucketService);
40 getPlacementTargetsSpy = spyOn(TestBed.get(RgwSiteService), 'getPlacementTargets');
43 it('should create', () => {
44 expect(component).toBeTruthy();
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);
60 it('should validate name (2/4)', () => {
61 const validatorFn = component.bucketNameValidator();
62 const ctrl = new FormControl('ab');
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();
73 it('should validate name (3/4)', () => {
74 const validatorFn = component.bucketNameValidator();
75 const ctrl = new FormControl('abc');
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);
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');
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();
101 it('should get zonegroup and placement targets', () => {
103 zonegroup: 'default',
106 name: 'default-placement',
107 data_pool: 'default.rgw.buckets.data'
110 name: 'placement-target2',
111 data_pool: 'placement-target2.rgw.buckets.data'
115 getPlacementTargetsSpy.and.returnValue(observableOf(payload));
116 fixture.detectChanges();
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']
124 placementTargets.push(placementTarget);
126 expect(component.placementTargets).toEqual(placementTargets);
130 describe('submit form', () => {
131 let notificationService: NotificationService;
134 spyOn(TestBed.get(Router), 'navigate').and.stub();
135 notificationService = TestBed.get(NotificationService);
136 spyOn(notificationService, 'show');
139 it('tests create success notification', () => {
140 spyOn(rgwBucketService, 'create').and.returnValue(observableOf([]));
141 component.editing = false;
142 component.bucketForm.markAsDirty();
144 expect(notificationService.show).toHaveBeenCalledWith(
145 NotificationType.success,
146 'Created Object Gateway bucket ""'
150 it('tests update success notification', () => {
151 spyOn(rgwBucketService, 'update').and.returnValue(observableOf([]));
152 component.editing = true;
153 component.bucketForm.markAsDirty();
155 expect(notificationService.show).toHaveBeenCalledWith(
156 NotificationType.success,
157 'Updated Object Gateway bucket ""'