From b86bee69504e326b01be10ca48369439747e73aa Mon Sep 17 00:00:00 2001 From: Lhc_fl Date: Tue, 20 Aug 2024 09:59:01 +0800 Subject: [PATCH 1/2] FIX: Empty param-form should reject submit The `onSubmit` hook will only be triggered when the form is valid, so we need to clear the contents of `serializedData` in advance in `submit`. Otherwise, it may not throw a validation error. --- .../discourse/components/param-input-form.gjs | 2 +- .../controllers/group-reports-show.js | 2 +- .../components/param-input-test.js | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/components/param-input-form.gjs b/assets/javascripts/discourse/components/param-input-form.gjs index 21da0cb4..1a58998e 100644 --- a/assets/javascripts/discourse/components/param-input-form.gjs +++ b/assets/javascripts/discourse/components/param-input-form.gjs @@ -278,6 +278,7 @@ export default class ParamInputForm extends Component { if (this.form == null) { throw "No form"; } + this.serializedData = null; await this.form.submit(); if (this.serializedData == null) { throw new ParamValidationError("validation_failed"); @@ -293,7 +294,6 @@ export default class ParamInputForm extends Component { @action onSubmit(data) { - this.serializedData = null; const serializedData = {}; for (const [id, val] of Object.entries(data)) { serializedData[id] = diff --git a/assets/javascripts/discourse/controllers/group-reports-show.js b/assets/javascripts/discourse/controllers/group-reports-show.js index 5f222c76..d429dd3c 100644 --- a/assets/javascripts/discourse/controllers/group-reports-show.js +++ b/assets/javascripts/discourse/controllers/group-reports-show.js @@ -91,7 +91,7 @@ export default class GroupReportsShowController extends Controller { } catch (error) { if (error.jqXHR?.status === 422 && error.jqXHR.responseJSON) { this.results = error.jqXHR.responseJSON; - } else if (error instanceof ParamValidationError) { + } else if (!(error instanceof ParamValidationError)) { popupAjaxError(error); } } finally { diff --git a/test/javascripts/components/param-input-test.js b/test/javascripts/components/param-input-test.js index 29377088..c9ddfdb8 100644 --- a/test/javascripts/components/param-input-test.js +++ b/test/javascripts/components/param-input-test.js @@ -169,4 +169,30 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) { }); } } + + test("empty form will reject submit", async function (assert) { + this.setProperties({ + param_info: [ + { + identifier: "string", + type: "string", + default: null, + nullable: false, + }, + ], + initialValues: {}, + onRegisterApi: ({ submit }) => { + this.submit = submit; + }, + }); + + await render(hbs` + `); + + assert.rejects(this.submit()); + }); }); From 30fae1e5103f0c08d0764467bc1ea71b1a9acd53 Mon Sep 17 00:00:00 2001 From: Lhc_fl Date: Tue, 20 Aug 2024 10:03:05 +0800 Subject: [PATCH 2/2] fix tests --- test/javascripts/components/param-input-test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/javascripts/components/param-input-test.js b/test/javascripts/components/param-input-test.js index c9ddfdb8..a116e282 100644 --- a/test/javascripts/components/param-input-test.js +++ b/test/javascripts/components/param-input-test.js @@ -194,5 +194,11 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) { />`); assert.rejects(this.submit()); + + // After successfully submitting the test once, edit and submit again. + await fillIn(`[name="string"]`, "foo"); + await this.submit(); + await fillIn(`[name="string"]`, ""); + assert.rejects(this.submit()); }); });