From 9949e60714ac6a658c5c60b1f7950c704bc63ad0 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 29 Sep 2020 15:26:29 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20form=20should=20support?= =?UTF-8?q?=20re-entering=20edit=20mode=20on=20success?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rose/addon/components/rose/form/index.js | 16 ++++++- .../integration/components/rose/form-test.js | 45 ++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/addons/rose/addon/components/rose/form/index.js b/addons/rose/addon/components/rose/form/index.js index 249cd9fbd..2a9f102e3 100644 --- a/addons/rose/addon/components/rose/form/index.js +++ b/addons/rose/addon/components/rose/form/index.js @@ -40,13 +40,27 @@ export default class RoseFormComponent extends Component { /** * Calls the passed `onSubmit` function while disabling the default form * submit behavior. + * + * Submit handlers _may optionally return a promise_. If they do, the promise + * is assumed to reflect the state of the submission operation, + * i.e. if the promise resolves the submission was successful and if it + * rejects, the submission failed. * @param {Event} e */ @action handleSubmit(e) { e.preventDefault(); e.stopPropagation(); - this.args.onSubmit(); + const submitResult = this.args.onSubmit(); + // If the submit handler returns a promise, we want to handle success + // by re-enabling read-only mode on the form (if applicable). + // Since submit is allowed to fail in this context and we don't want + // unhandled rejection warnings, we have a no-op catch. + if (submitResult?.then) { + submitResult + .then(() => this.isEditable = false) + .catch(() => {}); + } return false; } diff --git a/addons/rose/tests/integration/components/rose/form-test.js b/addons/rose/tests/integration/components/rose/form-test.js index 7c33aa290..a8103ca56 100644 --- a/addons/rose/tests/integration/components/rose/form-test.js +++ b/addons/rose/tests/integration/components/rose/form-test.js @@ -2,6 +2,7 @@ import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import { render, find, findAll, click } from '@ember/test-helpers'; import hbs from 'htmlbars-inline-precompile'; +import { resolve, reject } from 'rsvp'; module('Integration | Component | rose/form', function (hooks) { setupRenderingTest(hooks); @@ -70,7 +71,7 @@ module('Integration | Component | rose/form', function (hooks) { this.cancel = () => { assert.ok(true, 'cancel function may still be passed even for @showEditToggle'); }; - this.onSubmit = () => {}; + this.submit = () => {}; await render(hbs` {}; + await render(hbs` + + + + + `); + // Before enabling edit mode, fields are disabled and the edit mode button is displayed + assert.ok(find('.rose-form')); + assert.equal(findAll('input[disabled]').length, 1); + assert.equal(findAll('button').length, 1); + assert.equal(find('button').textContent.trim(), 'Edit'); + // After entering edit mode, fields are enabled and save/cancel buttons are displayed + await click('button'); + assert.equal(findAll('input[disabled]').length, 0); + assert.equal(findAll('button').length, 2); + assert.equal(find('[type="submit"]').textContent.trim(), 'Save'); + assert.equal(find('button:not([type="submit"])').textContent.trim(), 'Cancel'); + // After saving with failure, fields are enabled and save/cancel buttons are displayed + this.submit = () => reject(); + assert.equal(findAll('input[disabled]').length, 0); + assert.equal(findAll('button').length, 2); + assert.equal(find('[type="submit"]').textContent.trim(), 'Save'); + assert.equal(find('button:not([type="submit"])').textContent.trim(), 'Cancel'); + // After saving with success, fields are disabled again and the edit mode button is displayed + this.submit = () => resolve(); + await click('button[type="submit"]'); + assert.equal(findAll('input[disabled]').length, 1); + assert.equal(findAll('button').length, 1); + assert.equal(find('button').textContent.trim(), 'Edit'); + }); });