Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button - Fixes submitting a form when AsyncRule is passed (T887207) #12934

Merged
merged 3 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/ui/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ class Button extends Widget {
this.option('disabled', true);

complete.then(({ status }) => {
needValidate = true;
this.option('disabled', false);

validationStatus = status;
validationStatus === 'valid' && this._submitInput().click();
needValidate = true;
});
}
}
Expand Down
51 changes: 51 additions & 0 deletions testing/tests/DevExpress.ui.widgets/button.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,57 @@ QUnit.module('submit behavior', {
ValidationEngine.registerValidatorInGroup('testGroup', validator);
this.$element.trigger('dxclick');
});

QUnit.test('Form should be submitted only when an async validation rule is passed positively (T887207)', function(assert) {
this.clock.restore();
let value = 'a';
const validValue = 'b';
const validator = new Validator($('<div>').appendTo(this.$form), {
adapter: sinon.createStubInstance(DefaultAdapter),
validationRules: [{
type: 'async',
validationCallback: function() {
const d = new Deferred();
setTimeout(() => {
d.resolve({
isValid: value === validValue
});
}, 10);
return d.promise();
}
}]
});
const done = assert.async();
const onSubmit = () => {
assert.strictEqual(value, validValue, 'submitted with valid value');

ValidationEngine.initGroups();
this.$form.off('submit', onSubmit);
done();
};
const triggerButtonClick = () => {
this.$element.trigger('dxclick');
};

this.$form.on('submit', onSubmit);

this.$element.dxButton({
validationGroup: 'testGroup',
onOptionChanged: function(args) {
if(args.name === 'disabled') {
if(args.value === false && validator._validationInfo.result.status === 'invalid') {
setTimeout(function() {
value = validValue;
triggerButtonClick();
});
}
}
}
});

ValidationEngine.registerValidatorInGroup('testGroup', validator);
triggerButtonClick();
});
Comment on lines +546 to +595
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify this test.

Suggested change
QUnit.test('Form should be submitted only when an async validation rule is passed positively (T887207)', function(assert) {
this.clock.restore();
let value = 'a';
const validValue = 'b';
const validator = new Validator($('<div>').appendTo(this.$form), {
adapter: sinon.createStubInstance(DefaultAdapter),
validationRules: [{
type: 'async',
validationCallback: function() {
const d = new Deferred();
setTimeout(() => {
d.resolve({
isValid: value === validValue
});
}, 10);
return d.promise();
}
}]
});
const done = assert.async();
const onSubmit = () => {
assert.strictEqual(value, validValue, 'submitted with valid value');
ValidationEngine.initGroups();
this.$form.off('submit', onSubmit);
done();
};
const triggerButtonClick = () => {
this.$element.trigger('dxclick');
};
this.$form.on('submit', onSubmit);
this.$element.dxButton({
validationGroup: 'testGroup',
onOptionChanged: function(args) {
if(args.name === 'disabled') {
if(args.value === false && validator._validationInfo.result.status === 'invalid') {
setTimeout(function() {
value = validValue;
triggerButtonClick();
});
}
}
}
});
ValidationEngine.registerValidatorInGroup('testGroup', validator);
triggerButtonClick();
});
QUnit.test('Form should be submitted when an async validation rule is passed positively (T887207)', function(assert) {
this.clock.restore();
const done = assert.async();
const validator = new Validator($('<div>').appendTo(this.$form), {
adapter: sinon.createStubInstance(DefaultAdapter),
validationRules: [{
type: 'async',
validationCallback: function() {
const d = new $.Deferred();
setTimeout(() => {
d.resolve({ isValid: true });
}, 10);
return d.promise();
}
}]
});
ValidationEngine.registerValidatorInGroup('testGroup', validator);
this.$element.dxButton({ validationGroup: 'testGroup' });
this.$form.on('submit', () => {
assert.strictEqual(this.$element.dxButton('option', 'disabled'), false);
done();
});
this.$element.trigger('dxclick');
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main idea is to check that the form is not submitted when a value is invalid and submitted only when a value is valid. I would not simplify this test to prevent regressions during renovations and so forth.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

});

QUnit.module('templates', () => {
Expand Down