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