Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgr/dashboard: Remove _filterValue from CdFormGroup #24719

Merged
merged 2 commits into from Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -53,9 +53,6 @@ export class ConfigurationFormComponent implements OnInit {
});

this.configForm = new CdFormGroup(formControls);
this.configForm._filterValue = (value) => {
return value;
};
}

ngOnInit() {
Expand Down
Expand Up @@ -15,6 +15,8 @@ import { RgwUserFormComponent } from './rgw-user-form.component';
describe('RgwUserFormComponent', () => {
let component: RgwUserFormComponent;
let fixture: ComponentFixture<RgwUserFormComponent>;
let rgwUserService: RgwUserService;
let formHelper: FormHelper;

configureTestBed({
declarations: [RgwUserFormComponent],
Expand All @@ -26,17 +28,16 @@ describe('RgwUserFormComponent', () => {
fixture = TestBed.createComponent(RgwUserFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
rgwUserService = TestBed.get(RgwUserService);
formHelper = new FormHelper(component.userForm);
});

it('should create', () => {
expect(component).toBeTruthy();
});

describe('s3 key management', () => {
let rgwUserService: RgwUserService;

beforeEach(() => {
rgwUserService = TestBed.get(RgwUserService);
spyOn(rgwUserService, 'addS3Key').and.stub();
});

Expand Down Expand Up @@ -113,13 +114,8 @@ describe('RgwUserFormComponent', () => {
});

describe('username validation', () => {
let rgwUserService: RgwUserService;
let formHelper: FormHelper;

beforeEach(() => {
rgwUserService = TestBed.get(RgwUserService);
spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz']));
formHelper = new FormHelper(component.userForm);
});

it('should validate that username is required', () => {
Expand All @@ -144,4 +140,19 @@ describe('RgwUserFormComponent', () => {
})
);
});

describe('onSubmit', () => {
it('should be able to clear the mail field on update', () => {
spyOn(rgwUserService, 'update');
component.editing = true;
formHelper.setValue('email', '', true);
component.onSubmit();
expect(rgwUserService.update).toHaveBeenCalledWith(null, {
display_name: null,
email: '',
max_buckets: 1000,
suspended: false
});
});
});
});
Expand Up @@ -118,7 +118,8 @@ describe('CdFormGroup', () => {
expect(form.getValue('floating')).toBe(0.1);
});

it('returns strings that are not empty', () => {
it('returns strings', () => {
expect(form.getValue('emptyString')).toBe('');
expect(form.getValue('someString1')).toBe('s');
expect(form.getValue('someString2')).toBe('sth');
});
Expand All @@ -132,19 +133,11 @@ describe('CdFormGroup', () => {
expect(form.getValue('undefined')).toBe(null);
});

it('returns a falsy value for empty string, null, undefined, false and 0', () => {
expect(form.getValue('emptyString')).toBe(false);
it('returns a falsy value for null, undefined, false and 0', () => {
expect(form.getValue('false')).toBeFalsy();
expect(form.getValue('null')).toBeFalsy();
expect(form.getValue('number0')).toBeFalsy();
});

it('test _filterValue', () => {
expect(form._filterValue(0)).toBe(true);
expect(form._filterValue(null)).toBe(true);
expect(form._filterValue(false)).toBe(true);
expect(form._filterValue('')).toBe(false);
});
});

describe('should test showError', () => {
Expand Down
Expand Up @@ -21,9 +21,6 @@ export class CdFormGroup extends FormGroup {

/**
* Get a control out of any control even if its nested in other CdFormGroups or a FormGroup
*
* @param {string} controlName
* @returns {AbstractControl}
*/
get(controlName: string): AbstractControl {
const control = this._get(controlName);
Expand All @@ -49,43 +46,26 @@ export class CdFormGroup extends FormGroup {
}

/**
* Get the value of a control if it has none it will return false
*
* @param {string} controlName
* @returns {any} false or the value of the control
* Get the value of a control
*/
getValue(controlName: string): any {
const value = this.get(controlName).value;
return this._filterValue(value) && value;
}

// Overwrite this if needed.
_filterValue(value) {
return value !== '';
return this.get(controlName).value;
}

/**
* Sets a control without triggering a value changes event
*
* Very useful if a function is called through a value changes event but the value
* should be changed within the call.
*
* @param {string} controlName
* @param value
*/
silentSet(controlName: string, value: any) {
this.get(controlName).setValue(value, { emitEvent: false });
}

/**
* Indicates errors of the control in templates
*
* @param {string} controlName
* @param {NgForm} form
* @param {string} errorName
* @returns {boolean}
*/
showError(controlName: string, form: NgForm, errorName?: string) {
showError(controlName: string, form: NgForm, errorName?: string): boolean {
const control = this.get(controlName);
return (
(form.submitted || control.dirty) &&
Expand Down