Skip to content

Commit

Permalink
Added check for openWhenEmpty flag and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmeeks512 committed Feb 1, 2024
1 parent f682516 commit 36136be
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/components/editgrid/EditGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,10 @@ export default class EditGridComponent extends NestedArrayComponent {
return false;
}
else if (rowsEditing && this.saveEditMode) {
this.setCustomValidity(this.t(this.errorMessage('unsavedRowsError')), dirty);
return false;
if (!this.component.openWhenEmpty) {
this.setCustomValidity(this.t(this.errorMessage('unsavedRowsError')), dirty);
return false;
}
}

const message = this.invalid || this.invalidMessage(data, dirty);
Expand Down
43 changes: 43 additions & 0 deletions src/components/editgrid/EditGrid.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from './fixtures';

import ModalEditGrid from '../../../test/forms/modalEditGrid';
import EditGridOpenWhenEmpty from '../../../test/forms/editGridOpenWhenEmpty';
import Webform from '../../Webform';
import { displayAsModalEditGrid } from '../../../test/formtest';
import { Formio } from '../../Formio';
Expand Down Expand Up @@ -1382,4 +1383,46 @@ describe('EditGrid Open when Empty', () => {
})
.catch(done);
});

it('Should submit form with empty rows when submit button is pressed and no rows are saved', (done) => {
const formElement = document.createElement('div');
const form = new Webform(formElement);

form.setForm(compOpenWhenEmpty).then(() => {
const editGrid = form.components[0];

setTimeout(() => {
Harness.dispatchEvent('click', form.element, '[name="data[submit]"]');
setTimeout(() => {
const editRow = editGrid.editRows[0];
assert(!editGrid.error, 'Should be no errors on EditGrid');
assert.equal(editRow.errors, null, 'Should not be any errors on open row');
assert.equal(form.submission.state, 'submitted', 'Form should be submitted');
done();
}, 450);
}, 100);
}).catch(done);
});

it('Should not submit form if any row inputs are set as required', (done) => {
const formElement = document.createElement('div');
const form = new Webform(formElement);

form.setForm(EditGridOpenWhenEmpty).then(() => {
const editGrid = form.components[0];

setTimeout(() => {
Harness.dispatchEvent('click', form.element, '[name="data[submit]"]');
setTimeout(() => {
assert(!form.submission.state, 'Form should not be submitted');
const editRow = editGrid.editRows[0];
assert(editGrid.error, 'Should show error on EditGrid');
assert.equal(editRow.errors.length, 1, 'Should show error on row');
const textField = editRow.components[0];
assert(textField.element.className.includes('formio-error-wrapper'), 'Should add error class to component');
done();
}, 450);
}, 100);
}).catch(done);
});
});
67 changes: 67 additions & 0 deletions test/forms/editGridOpenWhenEmpty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export default {
_id: '65b119c4414257179284c2b4',
type: 'form',
tags: [],
owner: '5e05a6b7549cdc2ece30c6b0',
components: [
{
label: 'Edit Grid',
openWhenEmpty: true,
tableView: false,
rowDrafts: false,
key: 'editGrid',
type: 'editgrid',
input: true,
components: [
{
label: 'Text Field',
applyMaskOn: 'change',
tableView: true,
validate: { required: true },
key: 'textField',
type: 'textfield',
input: true
},
{
label: 'Text Area',
applyMaskOn: 'change',
autoExpand: false,
tableView: true,
key: 'textArea',
type: 'textarea',
input: true
}
]
},
{
type: 'button',
label: 'Submit',
key: 'submit',
input: true,
tableView: false,
showValidations: false,
}
],
controller: '',
revisions: '',
_vid: 0,
title: 'Edit Grid',
display: 'form',
access: [
{
roles: [
'5e96e79ee1c3ad3178454100',
'5e96e79ee1c3ad3178454101',
'5e96e79ee1c3ad3178454102'
],
type: 'read_all'
}
],
submissionAccess: [],
settings: {},
properties: {},
name: 'editGrid',
path: 'editgrid',
};


0 comments on commit 36136be

Please sign in to comment.