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