diff --git a/lib/core/src/lib/form/components/widgets/core/container.model.spec.ts b/lib/core/src/lib/form/components/widgets/core/container.model.spec.ts index 47a0d7be6a..0394b05d73 100644 --- a/lib/core/src/lib/form/components/widgets/core/container.model.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/container.model.spec.ts @@ -17,13 +17,154 @@ import { ContainerModel } from './container.model'; import { FormFieldModel } from './form-field.model'; -import { FormModel } from './form.model'; +import { FormFieldTypes } from './form-field-types'; describe('ContainerModel', () => { + let field: FormFieldModel; - it('should store the form reference', () => { - const form = new FormModel(); - const model = new ContainerModel(new FormFieldModel(form)); - expect(model.form).toBe(form); + beforeEach(() => { + field = new FormFieldModel(null, { + id: 'group-id', + name: 'group-name', + type: FormFieldTypes.GROUP, + params: { + allowCollapse: false, + collapseByDefault: false, + hideHeader: false + }, + numberOfColumns: 1, + tab: null + }); + }); + + it('should initialize with default values', () => { + const container = new ContainerModel(field); + + expect(container.field).toBe(field); + expect(container.columns).toEqual([]); + expect(container.isExpanded).toBe(true); + expect(container.rowspan).toBe(1); + expect(container.colspan).toBe(1); + }); + + describe('isVisible getter', () => { + it('should return true when field is visible', () => { + const container = new ContainerModel(field); + + expect(container.isVisible).toBe(true); + }); + + it('should return false when field is NOT visible', () => { + field.isVisible = false; + const container = new ContainerModel(field); + + expect(container.isVisible).toBe(false); + }); + }); + + describe('isTypeFieldGroup getter', () => { + it('should return true when field is a group', () => { + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + }); + + it('should return false when field is NOT a group', () => { + const container = new ContainerModel(new FormFieldModel(null, { type: FormFieldTypes.CONTAINER })); + + expect(container.isTypeFieldGroup).toBe(false); + }); + }); + + describe('isCollapsible getter', () => { + it('should return false when field is NOT a group', () => { + const container = new ContainerModel(new FormFieldModel(null, { type: FormFieldTypes.CONTAINER })); + + expect(container.isTypeFieldGroup).toBe(false); + expect(container.isCollapsible).toBe(false); + }); + + it('should return false when field is group and allowCollapse is false', () => { + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + expect(container.isCollapsible).toBe(false); + }); + + it('should return true when field is a group and allowCollapse is true', () => { + field.params.allowCollapse = true; + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + expect(container.isCollapsible).toBe(true); + }); + + it('should return false when field is a group and allowCollapse is NOT set', () => { + field.params.allowCollapse = undefined; + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + expect(container.isCollapsible).toBe(false); + }); + }); + + describe('isCollapsedByDefault getter', () => { + it('should return false when field is NOT a group', () => { + const container = new ContainerModel(new FormFieldModel(null, { type: FormFieldTypes.CONTAINER })); + + expect(container.isTypeFieldGroup).toBe(false); + expect(container.isCollapsedByDefault).toBe(false); + }); + + it('should return false when field is group and collapseByDefault is false', () => { + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + expect(container.isCollapsedByDefault).toBe(false); + }); + + it('should return true when field is a group and collapseByDefault is true', () => { + field.params.collapseByDefault = true; + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + expect(container.isCollapsedByDefault).toBe(true); + }); + + it('should return false when field is a group and collapseByDefault is NOT set', () => { + field.params.collapseByDefault = undefined; + const container = new ContainerModel(field); + + expect(container.isTypeFieldGroup).toBe(true); + expect(container.isCollapsedByDefault).toBe(false); + }); + }); + + describe('hideHeader getter', () => { + it('should return false when field is NOT a group', () => { + const container = new ContainerModel(new FormFieldModel(null, { type: FormFieldTypes.CONTAINER })); + + expect(container.hideHeader).toBe(false); + }); + + it('should return false when field is a group and hideHeader is false', () => { + const container = new ContainerModel(field); + + expect(container.hideHeader).toBe(false); + }); + + it('should return true when field is a group and hideHeader is true', () => { + field.params.hideHeader = true; + const container = new ContainerModel(field); + + expect(container.hideHeader).toBe(true); + }); + + it('should return false when field is a group and hideHeader is NOT set', () => { + field.params.hideHeader = undefined; + const container = new ContainerModel(field); + + expect(container.hideHeader).toBe(false); + }); }); }); diff --git a/lib/core/src/lib/form/components/widgets/core/container.model.ts b/lib/core/src/lib/form/components/widgets/core/container.model.ts index 093f28ac7a..6a166ce620 100644 --- a/lib/core/src/lib/form/components/widgets/core/container.model.ts +++ b/lib/core/src/lib/form/components/widgets/core/container.model.ts @@ -15,7 +15,7 @@ * limitations under the License. */ - /* eslint-disable @angular-eslint/component-selector */ +/* eslint-disable @angular-eslint/component-selector */ import { FormFieldModel } from './form-field.model'; import { FormWidgetModel } from './form-widget.model'; @@ -23,7 +23,6 @@ import { ContainerColumnModel } from './container-column.model'; import { FormFieldTypes } from './form-field-types'; export class ContainerModel extends FormWidgetModel { - field: FormFieldModel; readonly columns: ContainerColumnModel[] = []; @@ -31,43 +30,35 @@ export class ContainerModel extends FormWidgetModel { readonly rowspan: number = 1; readonly colspan: number = 1; - get isVisible(): boolean { - return this.field.isVisible; - } - constructor(field: FormFieldModel) { super(field.form, field.json); if (field) { this.field = field; this.columns = field.columns || []; - this.isExpanded = !this.isCollapsedByDefault(); + this.isExpanded = !this.isCollapsedByDefault; this.colspan = field.colspan; this.rowspan = field.rowspan; } } - isGroup(): boolean { + get isVisible(): boolean { + return this.field.isVisible; + } + + get isTypeFieldGroup(): boolean { return this.type === FormFieldTypes.GROUP; } - isCollapsible(): boolean { - let allowCollapse = false; - - if (this.isGroup() && this.field.params['allowCollapse']) { - allowCollapse = this.field.params['allowCollapse']; - } - - return allowCollapse; + get isCollapsible(): boolean { + return this.isTypeFieldGroup && (this.field.params?.allowCollapse ?? false); } - isCollapsedByDefault(): boolean { - let collapseByDefault = false; + get isCollapsedByDefault(): boolean { + return this.isTypeFieldGroup && (this.field.params?.collapseByDefault ?? false); + } - if (this.isCollapsible() && this.field.params['collapseByDefault']) { - collapseByDefault = this.field.params['collapseByDefault']; - } - - return collapseByDefault; + get hideHeader(): boolean { + return this.isTypeFieldGroup && (this.field.params?.hideHeader ?? false); } } diff --git a/lib/core/src/lib/form/components/widgets/header/header.widget.html b/lib/core/src/lib/form/components/widgets/header/header.widget.html index 7afef5293f..ea2ae9acc7 100644 --- a/lib/core/src/lib/form/components/widgets/header/header.widget.html +++ b/lib/core/src/lib/form/components/widgets/header/header.widget.html @@ -1,18 +1,37 @@ -