mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
Unit tests for base widget component
This commit is contained in:
parent
16231791c4
commit
3d3b156194
@ -32,8 +32,7 @@ export abstract class TextFieldWidgetComponent extends WidgetComponent {
|
||||
// workaround for MDL issues with dynamic components
|
||||
if (handler) {
|
||||
if (this.elementRef && this.hasValue()) {
|
||||
this.setupMaterialTextField(this.elementRef, handler, this.field.value.toString());
|
||||
return true;
|
||||
return this.setupMaterialTextField(this.elementRef, handler, this.field.value.toString());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -15,6 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ElementRef } from '@angular/core';
|
||||
import { WidgetComponent } from './widget.component';
|
||||
import { FormFieldModel } from './core/form-field.model';
|
||||
import { FormModel } from './core/form.model';
|
||||
@ -80,4 +81,53 @@ describe('WidgetComponent', () => {
|
||||
|
||||
component.checkVisibility(fakeField);
|
||||
});
|
||||
|
||||
it('should eval isRequired state of the field', () => {
|
||||
let widget = new WidgetComponent();
|
||||
expect(widget.isRequired()).toBeFalsy();
|
||||
|
||||
widget.field = new FormFieldModel(null);
|
||||
expect(widget.isRequired()).toBeFalsy();
|
||||
|
||||
widget.field = new FormFieldModel(null, { required: false });
|
||||
expect(widget.isRequired()).toBeFalsy();
|
||||
|
||||
widget.field = new FormFieldModel(null, { required: true });
|
||||
expect(widget.isRequired()).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should require element reference to setup textfield', () => {
|
||||
let widget = new WidgetComponent();
|
||||
expect(widget.setupMaterialTextField(null, {}, 'value')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require component handler to setup textfield', () => {
|
||||
let elementRef = new ElementRef(null);
|
||||
let widget = new WidgetComponent();
|
||||
expect(widget.setupMaterialTextField(elementRef, null, 'value')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require field value to setup textfield', () => {
|
||||
let elementRef = new ElementRef(null);
|
||||
let widget = new WidgetComponent();
|
||||
expect(widget.setupMaterialTextField(elementRef, {}, null)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should setup textfield', () => {
|
||||
let changeCalled = false;
|
||||
let elementRef = new ElementRef({
|
||||
querySelector: function () {
|
||||
return {
|
||||
MaterialTextfield: {
|
||||
change: function() {
|
||||
changeCalled = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
let widget = new WidgetComponent();
|
||||
expect(widget.setupMaterialTextField(elementRef, {}, 'value')).toBeTruthy();
|
||||
expect(changeCalled).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
@ -36,6 +36,8 @@ export class WidgetComponent implements AfterViewInit {
|
||||
return this.field ? true : false;
|
||||
}
|
||||
|
||||
// Note for developers:
|
||||
// returns <any> object to be able binding it to the <element reguired="required"> attribute
|
||||
isRequired(): any {
|
||||
if (this.field && this.field.required) {
|
||||
return true;
|
||||
@ -63,16 +65,18 @@ export class WidgetComponent implements AfterViewInit {
|
||||
return false;
|
||||
}
|
||||
|
||||
setupMaterialTextField(elementRef: ElementRef, handler: any, value: string) {
|
||||
setupMaterialTextField(elementRef: ElementRef, handler: any, value: string): boolean {
|
||||
if (elementRef && handler) {
|
||||
let el = elementRef.nativeElement;
|
||||
if (el) {
|
||||
let container = el.querySelector('.mdl-textfield');
|
||||
if (container) {
|
||||
container.MaterialTextfield.change(value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
checkVisibility(field: FormFieldModel) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user