diff --git a/lib/core/form/components/form-field/form-field.component.spec.ts b/lib/core/form/components/form-field/form-field.component.spec.ts
index 07fe29ddf2..866a04db0c 100644
--- a/lib/core/form/components/form-field/form-field.component.spec.ts
+++ b/lib/core/form/components/form-field/form-field.component.spec.ts
@@ -20,11 +20,13 @@ import { MaterialModule } from '../../../material.module';
import { ErrorWidgetComponent } from '../widgets/error/error.component';
import { FormRenderingService } from './../../services/form-rendering.service';
import { WidgetVisibilityService } from './../../services/widget-visibility.service';
-import { CheckboxWidgetComponent } from './../widgets/checkbox/checkbox.widget';
import { FormFieldModel, FormFieldTypes, FormModel } from './../widgets/core/index';
import { InputMaskDirective } from './../widgets/text/text-mask.component';
-import { TextWidgetComponent } from './../widgets/text/text.widget';
+import { TextWidgetComponent, CheckboxWidgetComponent, WidgetComponent } from '../widgets/index';
import { FormFieldComponent } from './form-field.component';
+import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
+import { FormService } from '../../services/form.service';
+import { EcmModelService } from '../../services/ecm-model.service';
describe('FormFieldComponent', () => {
@@ -35,22 +37,33 @@ describe('FormFieldComponent', () => {
let formRenderingService: FormRenderingService;
beforeEach(async(() => {
+
TestBed.configureTestingModule({
- imports: [
- MaterialModule
- ],
- declarations: [
- FormFieldComponent,
- TextWidgetComponent,
- CheckboxWidgetComponent,
- InputMaskDirective,
- ErrorWidgetComponent],
- providers: [
- FormRenderingService,
- WidgetVisibilityService
- ]
- })
- .compileComponents();
+ imports: [
+ MaterialModule
+ ],
+ declarations: [
+ FormFieldComponent,
+ WidgetComponent,
+ TextWidgetComponent,
+ CheckboxWidgetComponent,
+ InputMaskDirective,
+ ErrorWidgetComponent],
+ providers: [
+ FormRenderingService,
+ WidgetVisibilityService,
+ FormService,
+ EcmModelService
+ ]
+ });
+
+ TestBed.overrideModule(BrowserDynamicTestingModule, {
+ set: {
+ entryComponents: [WidgetComponent, TextWidgetComponent, CheckboxWidgetComponent]
+ }
+ });
+
+ TestBed.compileComponents();
}));
beforeEach(() => {
@@ -60,9 +73,14 @@ describe('FormFieldComponent', () => {
form = new FormModel();
});
- xit('should create default component instance', () => {
+ afterEach(() => {
+ fixture.destroy();
+ });
+
+ it('should create default component instance', () => {
let field = new FormFieldModel(form, {
- type: FormFieldTypes.TEXT
+ type: FormFieldTypes.TEXT,
+ id: 'FAKE-TXT-WIDGET'
});
component.field = field;
@@ -72,9 +90,10 @@ describe('FormFieldComponent', () => {
expect(component.componentRef.componentType).toBe(TextWidgetComponent);
});
- xit('should create custom component instance', () => {
+ it('should create custom component instance', () => {
let field = new FormFieldModel(form, {
- type: FormFieldTypes.TEXT
+ type: FormFieldTypes.TEXT,
+ id: 'FAKE-TXT-WIDGET'
});
formRenderingService.setComponentTypeResolver(FormFieldTypes.TEXT, () => CheckboxWidgetComponent, true);
@@ -87,7 +106,8 @@ describe('FormFieldComponent', () => {
it('should require component type to be resolved', () => {
let field = new FormFieldModel(form, {
- type: FormFieldTypes.TEXT
+ type: FormFieldTypes.TEXT,
+ id: 'FAKE-TXT-WIDGET'
});
spyOn(formRenderingService, 'resolveComponentType').and.returnValue(null);
@@ -98,4 +118,41 @@ describe('FormFieldComponent', () => {
expect(component.componentRef).toBeUndefined();
});
+ it('should hide the field when it is not visible', () => {
+ let field = new FormFieldModel(form, {
+ type: FormFieldTypes.TEXT,
+ id: 'FAKE-TXT-WIDGET'
+ });
+
+ component.field = field;
+ component.field.isVisible = false;
+ fixture.detectChanges();
+ expect(fixture.nativeElement.querySelector('#field-FAKE-TXT-WIDGET-container').hidden).toBeTruthy();
+ });
+
+ it('should show the field when it is visible', () => {
+ let field = new FormFieldModel(form, {
+ type: FormFieldTypes.TEXT,
+ id: 'FAKE-TXT-WIDGET'
+ });
+
+ component.field = field;
+ fixture.detectChanges();
+ expect(fixture.nativeElement.querySelector('#field-FAKE-TXT-WIDGET-container').hidden).toBeFalsy();
+ });
+
+ it('should hide a visible element', () => {
+ let field = new FormFieldModel(form, {
+ type: FormFieldTypes.TEXT,
+ id: 'FAKE-TXT-WIDGET'
+ });
+
+ component.field = field;
+ fixture.detectChanges();
+ expect(fixture.nativeElement.querySelector('#field-FAKE-TXT-WIDGET-container').hidden).toBeFalsy();
+ component.field.isVisible = false;
+ fixture.detectChanges();
+ expect(fixture.nativeElement.querySelector('#field-FAKE-TXT-WIDGET-container').hidden).toBeTruthy();
+ });
+
});
diff --git a/lib/core/form/components/form-field/form-field.component.ts b/lib/core/form/components/form-field/form-field.component.ts
index c47ce49d06..a87fbf3ae7 100644
--- a/lib/core/form/components/form-field/form-field.component.ts
+++ b/lib/core/form/components/form-field/form-field.component.ts
@@ -40,7 +40,8 @@ declare var adf: any;
@Component({
selector: 'adf-form-field, form-field',
template: `
-
@@ -96,7 +97,8 @@ export class FormFieldComponent implements OnInit, OnDestroy {
instance.field = this.field;
instance.fieldChanged.subscribe(field => {
if (field && this.field.form) {
- this.visibilityService.refreshVisibility(this.field.form);
+ this.visibilityService.refreshVisibility(field.form);
+ field.form.onFormFieldChanged(field);
}
});
}
diff --git a/lib/core/form/components/form.component.html b/lib/core/form/components/form.component.html
index 47972a0448..87e59c9be2 100644
--- a/lib/core/form/components/form.component.html
+++ b/lib/core/form/components/form.component.html
@@ -19,7 +19,7 @@
-
+
diff --git a/lib/core/form/components/form.component.visibility.spec.ts b/lib/core/form/components/form.component.visibility.spec.ts
index 8a5835c991..304a4dca3b 100644
--- a/lib/core/form/components/form.component.visibility.spec.ts
+++ b/lib/core/form/components/form.component.visibility.spec.ts
@@ -62,6 +62,10 @@ describe('FormComponent UI and visibiltiy', () => {
service = fixture.debugElement.injector.get(FormService);
});
+ afterEach(() => {
+ fixture.destroy();
+ });
+
it('should create instance of FormComponent', () => {
expect(fixture.componentInstance instanceof FormComponent).toBe(true, 'should create FormComponent');
});
@@ -124,12 +128,13 @@ describe('FormComponent UI and visibiltiy', () => {
component.ngOnChanges({ 'taskId': change });
fixture.detectChanges();
- let firstEl = fixture.debugElement.query(By.css('#country'));
- expect(firstEl).toBeNull();
+ let firstEl = fixture.debugElement.query(By.css('#field-country-container'));
+ expect(firstEl.nativeElement.hidden).toBeTruthy();
let secondEl = fixture.debugElement.query(By.css('#name'));
expect(secondEl).not.toBeNull();
expect(secondEl).toBeDefined();
+ expect(fixture.nativeElement.querySelector('#field-name-container').hidden).toBeFalsy();
});
it('should hide the field based on the previous one', () => {
@@ -143,9 +148,10 @@ describe('FormComponent UI and visibiltiy', () => {
let firstEl = fixture.debugElement.query(By.css('#name'));
expect(firstEl).not.toBeNull();
expect(firstEl).toBeDefined();
+ expect(fixture.nativeElement.querySelector('#field-name-container').hidden).toBeFalsy();
- let secondEl = fixture.debugElement.query(By.css('#country'));
- expect(secondEl).toBeNull();
+ let secondEl = fixture.debugElement.query(By.css('#field-country-container'));
+ expect(secondEl.nativeElement.hidden).toBeTruthy();
});
it('should show the hidden field when the visibility condition change to true', () => {
@@ -156,20 +162,19 @@ describe('FormComponent UI and visibiltiy', () => {
component.ngOnChanges({ 'taskId': change });
fixture.detectChanges();
- let firstEl = fixture.debugElement.query(By.css('#country'));
- expect(firstEl).toBeNull();
+ let firstEl = fixture.debugElement.query(By.css('#field-country-container'));
+ expect(firstEl.nativeElement.hidden).toBeTruthy();
- const secondEl = fixture.debugElement.query(By.css('#name'));
- expect(secondEl).not.toBeNull();
+ const secondEl = fixture.debugElement.query(By.css('#field-name-container'));
+ expect(secondEl.nativeElement.hidden).toBeFalsy();
- let el = secondEl.nativeElement;
-
- el.value = 'italy';
- el.dispatchEvent(new Event('input'));
+ let inputElement = fixture.nativeElement.querySelector('#name');
+ inputElement.value = 'italy';
+ inputElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
- firstEl = fixture.debugElement.query(By.css('#country'));
- expect(firstEl).not.toBeNull();
+ firstEl = fixture.debugElement.query(By.css('#field-country-container'));
+ expect(firstEl.nativeElement.hidden).toBeFalsy();
});
});
diff --git a/lib/core/form/components/widgets/amount/amount.widget.html b/lib/core/form/components/widgets/amount/amount.widget.html
index a03c1470a6..e70e41eb77 100644
--- a/lib/core/form/components/widgets/amount/amount.widget.html
+++ b/lib/core/form/components/widgets/amount/amount.widget.html
@@ -9,7 +9,7 @@
[required]="isRequired()"
[value]="field.value"
[(ngModel)]="field.value"
- (ngModelChange)="checkVisibility(field)"
+ (ngModelChange)="onFieldChanged(field)"
[disabled]="field.readOnly"
placeholder="{{field.placeholder}}">
diff --git a/lib/core/form/components/widgets/checkbox/checkbox.widget.html b/lib/core/form/components/widgets/checkbox/checkbox.widget.html
index f0b0293801..f5c7c23640 100644
--- a/lib/core/form/components/widgets/checkbox/checkbox.widget.html
+++ b/lib/core/form/components/widgets/checkbox/checkbox.widget.html
@@ -5,7 +5,7 @@
[required]="field.required"
[disabled]="field.readOnly || readOnly"
[(ngModel)]="field.value"
- (change)="onChange()">
+ (ngModelChange)="onFieldChanged(field)">
{{field.name}}
*
diff --git a/lib/core/form/components/widgets/checkbox/checkbox.widget.ts b/lib/core/form/components/widgets/checkbox/checkbox.widget.ts
index a4ccf77b95..6b8471463c 100644
--- a/lib/core/form/components/widgets/checkbox/checkbox.widget.ts
+++ b/lib/core/form/components/widgets/checkbox/checkbox.widget.ts
@@ -18,7 +18,6 @@
/* tslint:disable:component-selector no-input-rename */
import { Component, ViewEncapsulation } from '@angular/core';
-import { WidgetVisibilityService } from '../../../services/widget-visibility.service';
import { FormService } from './../../../services/form.service';
import { baseHost , WidgetComponent } from './../widget.component';
@@ -30,12 +29,8 @@ import { baseHost , WidgetComponent } from './../widget.component';
})
export class CheckboxWidgetComponent extends WidgetComponent {
- constructor(private visibilityService: WidgetVisibilityService, public formService: FormService) {
+ constructor(public formService: FormService) {
super(formService);
}
- onChange() {
- this.visibilityService.refreshVisibility(this.field.form);
- }
-
}
diff --git a/lib/core/form/components/widgets/container/container.widget.html b/lib/core/form/components/widgets/container/container.widget.html
index fc9a3e5abb..4aee462184 100644
--- a/lib/core/form/components/widgets/container/container.widget.html
+++ b/lib/core/form/components/widgets/container/container.widget.html
@@ -1,17 +1,17 @@
-