Skip to content

Commit 0434b9d

Browse files
DiogoABastosalep85
authored andcommitted
AAE-27107 Add tests
1 parent 7c98d56 commit 0434b9d

File tree

5 files changed

+187
-95
lines changed

5 files changed

+187
-95
lines changed

lib/core/src/lib/form/components/form-renderer.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ describe('Form Renderer Component', () => {
921921
expect(row).toBeTruthy();
922922

923923
const removeRowButton = testingUtils.getByCSS(
924-
'#field-RepeatableSection0tbw2y-container .adf-grid-list-container .adf-grid-list-remove-row-button'
924+
'#field-RepeatableSection0tbw2y-container .adf-grid-list-container .adf-grid-list-row-remove-button'
925925
);
926926

927927
expect(removeRowButton).toBeTruthy();
@@ -938,7 +938,7 @@ describe('Form Renderer Component', () => {
938938
expect(row).toBeTruthy();
939939

940940
const removeRowButton = testingUtils.getByCSS(
941-
'#field-RepeatableSection0tbw2y-container .adf-grid-list-container .adf-grid-list-remove-row-button'
941+
'#field-RepeatableSection0tbw2y-container .adf-grid-list-container .adf-grid-list-row-remove-button'
942942
);
943943

944944
expect(removeRowButton).toBeFalsy();

lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts

Lines changed: 172 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,61 +1272,121 @@ describe('FormFieldModel', () => {
12721272
let form: FormModel;
12731273
let field: FormFieldModel;
12741274

1275+
const fields = {
1276+
'1': [
1277+
{
1278+
id: 'Text0wwp7n',
1279+
name: 'Text',
1280+
type: 'text',
1281+
readOnly: false,
1282+
required: false,
1283+
colspan: 1,
1284+
rowspan: 1,
1285+
placeholder: null,
1286+
minLength: 0,
1287+
maxLength: 0,
1288+
regexPattern: null,
1289+
visibilityCondition: null,
1290+
params: {
1291+
existingColspan: 1,
1292+
maxColspan: 2
1293+
}
1294+
}
1295+
],
1296+
'2': [
1297+
{
1298+
id: 'Integer0rzkwq',
1299+
name: 'Integer',
1300+
type: 'integer',
1301+
readOnly: false,
1302+
colspan: 1,
1303+
rowspan: 1,
1304+
placeholder: null,
1305+
minValue: null,
1306+
maxValue: null,
1307+
required: false,
1308+
visibilityCondition: null,
1309+
params: {
1310+
existingColspan: 1,
1311+
maxColspan: 2
1312+
}
1313+
}
1314+
]
1315+
};
1316+
1317+
const fieldsDisabled = {
1318+
'1': [
1319+
{
1320+
id: 'Text0wwp7n',
1321+
name: 'Text',
1322+
type: 'text',
1323+
readOnly: true,
1324+
required: false,
1325+
colspan: 1,
1326+
rowspan: 1,
1327+
placeholder: null,
1328+
minLength: 0,
1329+
maxLength: 0,
1330+
regexPattern: null,
1331+
visibilityCondition: null,
1332+
params: {
1333+
existingColspan: 1,
1334+
maxColspan: 2
1335+
}
1336+
}
1337+
],
1338+
'2': [
1339+
{
1340+
id: 'Integer0rzkwq',
1341+
name: 'Integer',
1342+
type: 'integer',
1343+
readOnly: false,
1344+
colspan: 1,
1345+
rowspan: 1,
1346+
placeholder: null,
1347+
minValue: null,
1348+
maxValue: null,
1349+
required: false,
1350+
visibilityCondition: null,
1351+
params: {
1352+
existingColspan: 1,
1353+
maxColspan: 2
1354+
}
1355+
}
1356+
]
1357+
};
1358+
1359+
const json = {
1360+
id: 'RepeatableSection0tbw2y',
1361+
name: 'Repeatable Section',
1362+
type: 'repeatable-section',
1363+
tab: null,
1364+
params: {
1365+
initialNumberOfRows: 2,
1366+
allowInitialRowsDelete: true,
1367+
maxNumberOfRows: 5
1368+
},
1369+
numberOfColumns: 2,
1370+
fields
1371+
};
1372+
1373+
const jsonDisabled = {
1374+
id: 'RepeatableSection0tbw2y',
1375+
name: 'Repeatable Section',
1376+
type: 'repeatable-section',
1377+
tab: null,
1378+
params: {
1379+
initialNumberOfRows: 2,
1380+
allowInitialRowsDelete: true,
1381+
maxNumberOfRows: 5
1382+
},
1383+
numberOfColumns: 2,
1384+
fields: fieldsDisabled
1385+
};
1386+
12751387
beforeEach(() => {
12761388
form = new FormModel();
1277-
field = new FormFieldModel(form, {
1278-
id: 'RepeatableSection0tbw2y',
1279-
name: 'Repeatable Section',
1280-
type: 'repeatable-section',
1281-
tab: null,
1282-
params: {
1283-
initialNumberOfRows: 2,
1284-
allowInitialRowsDelete: true,
1285-
newRowsLimit: 3
1286-
},
1287-
numberOfColumns: 2,
1288-
fields: {
1289-
'1': [
1290-
{
1291-
id: 'Text0wwp7n',
1292-
name: 'Text',
1293-
type: 'text',
1294-
readOnly: false,
1295-
required: false,
1296-
colspan: 1,
1297-
rowspan: 1,
1298-
placeholder: null,
1299-
minLength: 0,
1300-
maxLength: 0,
1301-
regexPattern: null,
1302-
visibilityCondition: null,
1303-
params: {
1304-
existingColspan: 1,
1305-
maxColspan: 2
1306-
}
1307-
}
1308-
],
1309-
'2': [
1310-
{
1311-
id: 'Integer0rzkwq',
1312-
name: 'Integer',
1313-
type: 'integer',
1314-
readOnly: false,
1315-
colspan: 1,
1316-
rowspan: 1,
1317-
placeholder: null,
1318-
minValue: null,
1319-
maxValue: null,
1320-
required: false,
1321-
visibilityCondition: null,
1322-
params: {
1323-
existingColspan: 1,
1324-
maxColspan: 2
1325-
}
1326-
}
1327-
]
1328-
}
1329-
});
1389+
field = new FormFieldModel(form, json);
13301390
});
13311391

13321392
describe('add row', () => {
@@ -1408,5 +1468,64 @@ describe('FormFieldModel', () => {
14081468
expect(form.values[field.id]).toEqual(formValues.removeState);
14091469
});
14101470
});
1471+
1472+
describe('disabled state', () => {
1473+
const textWidgetId = 'Text0wwp7n';
1474+
1475+
/**
1476+
*
1477+
* @param expectation expectation function
1478+
*/
1479+
function checkChildrenWidgets(expectation: any) {
1480+
for (const row of field.rows) {
1481+
for (const column of row.columns) {
1482+
for (const child of column.fields) {
1483+
expectation(child);
1484+
}
1485+
}
1486+
}
1487+
}
1488+
it('should make all children fields disabled if repeatable section is disabled', () => {
1489+
field.readOnly = true;
1490+
1491+
checkChildrenWidgets((child) => {
1492+
expect(child.readOnly).toBeTruthy();
1493+
});
1494+
});
1495+
1496+
it('should allow for initial disabled children widgets if repeatable section is enabled', () => {
1497+
field = new FormFieldModel(form, jsonDisabled);
1498+
1499+
checkChildrenWidgets((child) => {
1500+
if (child.id.startsWith(textWidgetId)) {
1501+
expect(child.readOnly).toBeTruthy();
1502+
return;
1503+
}
1504+
1505+
expect(child.readOnly).toBeFalsy();
1506+
});
1507+
});
1508+
1509+
it('should keep initial disabled children widgets if repeatable section is re-enabled', () => {
1510+
field = new FormFieldModel(form, jsonDisabled);
1511+
field.rows[0].columns[0].fields[0].readOnly = true;
1512+
field.readOnly = true;
1513+
1514+
checkChildrenWidgets((child) => {
1515+
expect(child.readOnly).toBeTruthy();
1516+
});
1517+
1518+
field.readOnly = false;
1519+
1520+
checkChildrenWidgets((child) => {
1521+
if (child.id.startsWith(textWidgetId)) {
1522+
expect(child.readOnly).toBeTruthy();
1523+
return;
1524+
}
1525+
1526+
expect(child.readOnly).toBeFalsy();
1527+
});
1528+
});
1529+
});
14111530
});
14121531
});

lib/core/src/lib/form/components/widgets/core/form-field.model.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,15 @@ export class FormFieldModel extends FormWidgetModel {
351351
this.colspan = 1;
352352
this.rows = [];
353353

354-
for (let i = 0; i < this.getNumberOfRows(params.initialNumberOfRows, params.newRowsLimit, value); i++) {
354+
for (let i = 0; i < this.getNumberOfRows(params.initialNumberOfRows, params.maxNumberOfRows, value); i++) {
355355
this.rows.push(this.createRow(fields, form, i, value?.[i], i < params?.initialNumberOfRows));
356356
}
357357

358358
this.columns = this.rows[0].columns;
359359
}
360360

361-
private getNumberOfRows(initialNrRows: number = 1, rowsLimit?: number, value?: any) {
362-
return value?.length && !!rowsLimit ? Math.min(value?.length, initialNrRows + rowsLimit) : (value?.length ?? initialNrRows);
361+
private getNumberOfRows(initialNrRows: number = 1, maximumNrRows?: number, value?: any) {
362+
return value?.length <= maximumNrRows ? value?.length : initialNrRows;
363363
}
364364

365365
private createRow(fields: any, form: any, index: number, value?: any, isInitial: boolean = false) {
@@ -476,7 +476,7 @@ export class FormFieldModel extends FormWidgetModel {
476476
}
477477

478478
private shouldAddRow(): boolean {
479-
return !this.params.newRowsLimit || this.rows.length < this.params.initialNumberOfRows + this.params.newRowsLimit;
479+
return !this.params.maxNumberOfRows || this.rows.length < this.params.maxNumberOfRows;
480480
}
481481

482482
removeRow(index: number) {
@@ -740,7 +740,7 @@ export class FormFieldModel extends FormWidgetModel {
740740
return this.value;
741741
}
742742

743-
return null;
743+
return undefined;
744744
}
745745
}
746746

lib/core/src/lib/form/components/widgets/repeat/repeat.widget.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ng-content />
1212

1313
@if (!isEditor) {
14-
@let shouldDisplayAddRowButton = !element.field.params.newRowsLimit || (element.field.params.newRowsLimit > getAddedRowsCount());
14+
@let shouldDisplayAddRowButton = !element.field.params.maxNumberOfRows || (element.field.params.maxNumberOfRows > element.field.rows.length);
1515
@if (shouldDisplayAddRowButton) {
1616
<button
1717
mat-button
@@ -23,8 +23,7 @@
2323
<mat-icon>add</mat-icon> {{ 'FORM.FIELD.REPEATABLE_SECTION.ADD_ROW' | translate }}
2424
</button>
2525
} @else {
26-
@let rowLimit = element.field.params.initialNumberOfRows + (element.field.params.newRowsLimit ?? 0);
27-
<span class="adf-container-widget-row-action adf-container-widget-row-limit">{{ 'FORM.FIELD.REPEATABLE_SECTION.ROW_LIMIT_REACHED' | translate: { limit: rowLimit } }}</span>
26+
<span class="adf-container-widget-row-action adf-container-widget-row-limit">{{ 'FORM.FIELD.REPEATABLE_SECTION.ROW_LIMIT_REACHED' | translate: { limit: element.field.params.maxNumberOfRows } }}</span>
2827
}
2928
}
3029
</div>

lib/core/src/lib/form/components/widgets/repeat/repeat.widget.spec.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ describe('RepeatWidgetComponent', () => {
3030
/**
3131
*
3232
* @param initialNumberOfRows initial number of rows
33-
* @param newRowsLimit limit for additional rows
33+
* @param maxNumberOfRows maximum number of rows
3434
* @param allowInitialRowsDelete should allow deleting rows
3535
* @returns repeatable section json based on params
3636
*/
37-
function getFormFieldJson(initialNumberOfRows: number = 2, newRowsLimit?: number, allowInitialRowsDelete: boolean = true) {
37+
function getFormFieldJson(initialNumberOfRows: number = 2, maxNumberOfRows?: number, allowInitialRowsDelete: boolean = true) {
3838
return {
3939
id: 'RepeatableSection0tbw2y',
4040
name: 'Repeatable Section',
@@ -43,7 +43,7 @@ describe('RepeatWidgetComponent', () => {
4343
params: {
4444
initialNumberOfRows,
4545
allowInitialRowsDelete,
46-
newRowsLimit
46+
maxNumberOfRows
4747
},
4848
numberOfColumns: 2,
4949
fields: {
@@ -126,7 +126,7 @@ describe('RepeatWidgetComponent', () => {
126126
});
127127

128128
it('should display add row button if limit is defined but not reached', () => {
129-
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 1)));
129+
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 3)));
130130

131131
fixture.detectChanges();
132132

@@ -135,7 +135,7 @@ describe('RepeatWidgetComponent', () => {
135135
});
136136

137137
it('should NOT display add row button if limit is defined and reached', () => {
138-
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 1)));
138+
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 2)));
139139
spyOn(component, 'getAddedRowsCount').and.returnValue(1);
140140

141141
fixture.detectChanges();
@@ -145,7 +145,7 @@ describe('RepeatWidgetComponent', () => {
145145
});
146146

147147
it('should display row limit if limit has been reached', () => {
148-
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 1)));
148+
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 2)));
149149
spyOn(component, 'getAddedRowsCount').and.returnValue(1);
150150

151151
fixture.detectChanges();
@@ -165,31 +165,5 @@ describe('RepeatWidgetComponent', () => {
165165
testingUtils.clickByCSS('button.adf-container-widget-row-action');
166166
expect(component.addRow).toHaveBeenCalled();
167167
});
168-
169-
describe('getAddedRowsCount', () => {
170-
it('should get correct rows count if initial rows are allowed to be deleted', () => {
171-
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 1)));
172-
173-
fixture.detectChanges();
174-
175-
expect(component.getAddedRowsCount()).toBe(0);
176-
177-
component.addRow();
178-
179-
fixture.detectChanges();
180-
181-
expect(component.getAddedRowsCount()).toBe(1);
182-
});
183-
184-
it('should get correct rows count if initial rows are NOT allowed to be deleted', () => {
185-
component.element = new ContainerModel(new FormFieldModel(new FormModel(), getFormFieldJson(2, 1, false)));
186-
187-
expect(component.getAddedRowsCount()).toBe(0);
188-
189-
component.addRow();
190-
191-
expect(component.getAddedRowsCount()).toBe(1);
192-
});
193-
});
194168
});
195169
});

0 commit comments

Comments
 (0)