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';
6 import 'rxjs/add/observable/of';
7 import { Observable } from 'rxjs/Observable';
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';
14 describe('RgwBucketFormComponent', () => {
15 let component: RgwBucketFormComponent;
16 let fixture: ComponentFixture<RgwBucketFormComponent>;
17 let queryResult: Array<string> = [];
19 class MockRgwBucketService extends RgwBucketService {
21 return Observable.of(queryResult);
25 beforeEach(async(() => {
26 TestBed.configureTestingModule({
27 declarations: [RgwBucketFormComponent],
28 imports: [HttpClientTestingModule, ReactiveFormsModule, RouterTestingModule, SharedModule],
29 providers: [RgwUserService, { provide: RgwBucketService, useClass: MockRgwBucketService }]
30 }).compileComponents();
34 fixture = TestBed.createComponent(RgwBucketFormComponent);
35 component = fixture.componentInstance;
36 fixture.detectChanges();
39 it('should create', () => {
40 expect(component).toBeTruthy();
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);
56 it('should validate name (2/4)', () => {
57 const validatorFn = component.bucketNameValidator();
58 const ctrl = new FormControl('ab');
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();
69 it('should validate name (3/4)', () => {
70 const validatorFn = component.bucketNameValidator();
71 const ctrl = new FormControl('abc');
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);
82 it('should validate name (4/4)', () => {
83 queryResult = ['abcd'];
84 const validatorFn = component.bucketNameValidator();
85 const ctrl = new FormControl('abcd');
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();