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';
6 import { of as observableOf } from 'rxjs';
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';
14 describe('RgwBucketFormComponent', () => {
15 let component: RgwBucketFormComponent;
16 let fixture: ComponentFixture<RgwBucketFormComponent>;
17 let queryResult: Array<string> = [];
19 class MockRgwBucketService extends RgwBucketService {
21 return observableOf(queryResult);
26 declarations: [RgwBucketFormComponent],
27 imports: [HttpClientTestingModule, ReactiveFormsModule, RouterTestingModule, SharedModule],
28 providers: [RgwUserService, { provide: RgwBucketService, useClass: MockRgwBucketService }]
32 fixture = TestBed.createComponent(RgwBucketFormComponent);
33 component = fixture.componentInstance;
34 fixture.detectChanges();
37 it('should create', () => {
38 expect(component).toBeTruthy();
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);
54 it('should validate name (2/4)', () => {
55 const validatorFn = component.bucketNameValidator();
56 const ctrl = new FormControl('ab');
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();
67 it('should validate name (3/4)', () => {
68 const validatorFn = component.bucketNameValidator();
69 const ctrl = new FormControl('abc');
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);
80 it('should validate name (4/4)', () => {
81 queryResult = ['abcd'];
82 const validatorFn = component.bucketNameValidator();
83 const ctrl = new FormControl('abcd');
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();