1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
3 import { SharedModule } from '~/app/shared/shared.module';
4 import { HttpClientTestingModule } from '@angular/common/http/testing';
5 import { RouterTestingModule } from '@angular/router/testing';
6 import { ReactiveFormsModule } from '@angular/forms';
7 import { ToastrModule } from 'ngx-toastr';
14 } from 'carbon-components-angular';
15 import { CoreModule } from '~/app/core/core.module';
16 import { RgwStorageClassFormComponent } from './rgw-storage-class-form.component';
17 import { TIER_TYPE_DISPLAY } from '../models/rgw-storage-class.model';
19 describe('RgwStorageClassFormComponent', () => {
20 let component: RgwStorageClassFormComponent;
21 let fixture: ComponentFixture<RgwStorageClassFormComponent>;
23 beforeEach(async () => {
24 await TestBed.configureTestingModule({
26 BrowserAnimationsModule,
28 HttpClientTestingModule,
31 ToastrModule.forRoot(),
39 declarations: [RgwStorageClassFormComponent]
40 }).compileComponents();
42 fixture = TestBed.createComponent(RgwStorageClassFormComponent);
43 component = fixture.componentInstance;
44 fixture.detectChanges();
47 it('should create', () => {
48 component.goToListView();
49 expect(component).toBeTruthy();
52 it('should initialize the form with empty values', () => {
53 const storageClassForm = component.storageClassForm;
54 expect(storageClassForm).toBeTruthy();
55 expect(storageClassForm.get('zonegroup')).toBeTruthy();
56 expect(storageClassForm.get('placement_target')).toBeTruthy();
59 it('on zonegroup changes', () => {
60 component.zoneGroupDetails = {
61 default_zonegroup: 'zonegroup1',
69 name: 'default-placement',
74 tier_type: 'cloud-s3',
75 retain_head_object: true,
76 storage_class: 'CLOUDIBM',
77 allow_read_through: true,
78 read_through_restore_days: 1,
79 restore_storage_class: 'test67',
81 storage_class: 'CLOUDIBM',
82 endpoint: 'https://s3.amazonaws.com',
83 access_key: 'ACCESSKEY',
84 target_path: '/path/to/storage',
85 target_storage_class: 'STANDARD',
88 multipart_min_part_size: 87877,
89 multipart_sync_threshold: 987877,
93 glacier_restore_days: 5,
94 glacier_restore_tier_type: 'Standard'
104 component.storageClassForm.get('zonegroup').setValue('zonegroup1');
105 component.onZonegroupChange();
106 expect(component.placementTargets).toEqual(['default-placement']);
107 expect(component.storageClassForm.get('placement_target').value).toBe('default-placement');
110 it('should set form values on submit', () => {
111 const storageClassName = 'storageClass1';
112 component.storageClassForm.get('storage_class').setValue(storageClassName);
113 component.storageClassForm.get('zonegroup').setValue('zonegroup1');
114 component.storageClassForm.get('placement_target').setValue('placement1');
115 component.storageClassForm.get('endpoint').setValue('http://ams03.com');
116 component.storageClassForm.get('access_key').setValue('accesskey');
117 component.storageClassForm.get('secret_key').setValue('secretkey');
118 component.storageClassForm.get('target_path').setValue('/target');
119 component.storageClassForm.get('retain_head_object').setValue(true);
120 component.storageClassForm.get('allow_read_through').setValue(true);
121 component.storageClassForm.get('region').setValue('useast1');
122 component.storageClassForm.get('multipart_sync_threshold').setValue(1024);
123 component.storageClassForm.get('multipart_min_part_size').setValue(256);
124 component.goToListView();
125 component.submitAction();
126 expect(component).toBeTruthy();
129 it('should set required validators for CLOUD_TIER fields', () => {
130 (component as any).updateValidatorsBasedOnStorageClass(TIER_TYPE_DISPLAY.CLOUD_TIER);
131 const requiredFields = ['region', 'endpoint', 'access_key', 'secret_key', 'target_path'];
132 requiredFields.forEach((field) => {
133 const control = component.storageClassForm.get(field);
134 control.setValue('');
135 control.updateValueAndValidity();
137 ['glacier_restore_tier_type', 'restore_storage_class'].forEach((field) => {
138 const control = component.storageClassForm.get(field);
139 control.setValue('');
140 control.updateValueAndValidity();
141 expect(component).toBeTruthy();
145 it('should set required validators for GLACIER fields', () => {
146 (component as any).updateValidatorsBasedOnStorageClass(TIER_TYPE_DISPLAY.GLACIER);
147 const requiredFields = [
153 'glacier_restore_tier_type',
154 'restore_storage_class'
156 requiredFields.forEach((field) => {
157 const control = component.storageClassForm.get(field);
158 control.setValue('');
159 control.updateValueAndValidity();
160 expect(component).toBeTruthy();
164 it('should clear validators for LOCAL fields', () => {
165 (component as any).updateValidatorsBasedOnStorageClass(TIER_TYPE_DISPLAY.LOCAL);
173 'glacier_restore_tier_type',
174 'restore_storage_class'
176 allFields.forEach((field) => {
177 const control = component.storageClassForm.get(field);
178 control.setValue('');
179 control.updateValueAndValidity();
180 expect(component).toBeTruthy();