]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
f80db6ee4e2d04529a8f7d66964c9703fe109583
[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 { RouterTestingModule } from '@angular/router/testing';
5
6 import { of as observableOf } from 'rxjs';
7
8 import { RgwBucketService } from '../../../shared/api/rgw-bucket.service';
9 import { RgwUserService } from '../../../shared/api/rgw-user.service';
10 import { SharedModule } from '../../../shared/shared.module';
11 import { configureTestBed } from '../../../shared/unit-test-helper';
12 import { RgwBucketFormComponent } from './rgw-bucket-form.component';
13
14 describe('RgwBucketFormComponent', () => {
15   let component: RgwBucketFormComponent;
16   let fixture: ComponentFixture<RgwBucketFormComponent>;
17   let queryResult: Array<string> = [];
18
19   class MockRgwBucketService extends RgwBucketService {
20     enumerate() {
21       return observableOf(queryResult);
22     }
23   }
24
25   configureTestBed({
26     declarations: [RgwBucketFormComponent],
27     imports: [HttpClientTestingModule, ReactiveFormsModule, RouterTestingModule, SharedModule],
28     providers: [RgwUserService, { provide: RgwBucketService, useClass: MockRgwBucketService }]
29   });
30
31   beforeEach(() => {
32     fixture = TestBed.createComponent(RgwBucketFormComponent);
33     component = fixture.componentInstance;
34     fixture.detectChanges();
35   });
36
37   it('should create', () => {
38     expect(component).toBeTruthy();
39   });
40
41   describe('bucketNameValidator', () => {
42     it('should validate name (1/4)', () => {
43       const validatorFn = component.bucketNameValidator();
44       const ctrl = new FormControl('');
45       const validatorPromise = validatorFn(ctrl);
46       expect(validatorPromise instanceof Promise).toBeTruthy();
47       if (validatorPromise instanceof Promise) {
48         validatorPromise.then((resp) => {
49           expect(resp).toBe(null);
50         });
51       }
52     });
53
54     it('should validate name (2/4)', () => {
55       const validatorFn = component.bucketNameValidator();
56       const ctrl = new FormControl('ab');
57       ctrl.markAsDirty();
58       const validatorPromise = validatorFn(ctrl);
59       expect(validatorPromise instanceof Promise).toBeTruthy();
60       if (validatorPromise instanceof Promise) {
61         validatorPromise.then((resp) => {
62           expect(resp.bucketNameInvalid).toBeTruthy();
63         });
64       }
65     });
66
67     it('should validate name (3/4)', () => {
68       const validatorFn = component.bucketNameValidator();
69       const ctrl = new FormControl('abc');
70       ctrl.markAsDirty();
71       const validatorPromise = validatorFn(ctrl);
72       expect(validatorPromise instanceof Promise).toBeTruthy();
73       if (validatorPromise instanceof Promise) {
74         validatorPromise.then((resp) => {
75           expect(resp).toBe(null);
76         });
77       }
78     });
79
80     it('should validate name (4/4)', () => {
81       queryResult = ['abcd'];
82       const validatorFn = component.bucketNameValidator();
83       const ctrl = new FormControl('abcd');
84       ctrl.markAsDirty();
85       const validatorPromise = validatorFn(ctrl);
86       expect(validatorPromise instanceof Promise).toBeTruthy();
87       if (validatorPromise instanceof Promise) {
88         validatorPromise.then((resp) => {
89           expect(resp instanceof Object).toBeTruthy();
90           expect(resp.bucketNameExists).toBeTruthy();
91         });
92       }
93     });
94   });
95 });