From a4aebc83acc94e96fd2293ddf11279048d2ca4c4 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 10 Jul 2024 12:22:31 +0100 Subject: [PATCH] DEV: Add simple test for json-schema-editor and remove extra `await` (#27827) Followup to ce3d91f422f2618edad62ac63c58062067f7011b --- .../components/modal/json-schema-editor.hbs | 1 + .../components/modal/json-schema-editor.js | 2 +- .../components/json-schema-editor-test.gjs | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/discourse/tests/unit/components/json-schema-editor-test.gjs diff --git a/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.hbs b/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.hbs index f1ce01d1f14..ac1edd72088 100644 --- a/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.hbs +++ b/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.hbs @@ -6,6 +6,7 @@ "admin.site_settings.json_schema.modal_title" name=@model.settingName }} + @inline={{@inline}} class="json-schema-editor-modal" > <:body> diff --git a/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.js b/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.js index bb8bfb5467f..ca58dedfa7e 100644 --- a/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.js +++ b/app/assets/javascripts/discourse/app/components/modal/json-schema-editor.js @@ -37,7 +37,7 @@ export default class JsonSchemaEditorModal extends Component { @action async buildJsonEditor(element) { - const promise = await import("@json-editor/json-editor"); + const promise = import("@json-editor/json-editor"); if (isTesting()) { waitForPromise(promise); } diff --git a/app/assets/javascripts/discourse/tests/unit/components/json-schema-editor-test.gjs b/app/assets/javascripts/discourse/tests/unit/components/json-schema-editor-test.gjs new file mode 100644 index 00000000000..412eed8c0fd --- /dev/null +++ b/app/assets/javascripts/discourse/tests/unit/components/json-schema-editor-test.gjs @@ -0,0 +1,44 @@ +import { click, fillIn, render } from "@ember/test-helpers"; +import { module, test } from "qunit"; +import JsonSchemaEditor from "discourse/components/modal/json-schema-editor"; +import { setupRenderingTest } from "discourse/tests/helpers/component-test"; + +const TEST_SCHEMA = { + type: "array", + uniqueItems: true, + items: { + type: "object", + properties: { color: { type: "string" }, icon: { type: "string" } }, + additionalProperties: false, + }, +}; + +module("Unit | Component | ", function (hooks) { + setupRenderingTest(hooks); + + test("modal functions correctly", async function (assert) { + let result; + const model = { + value: "[]", + settingName: "My setting name", + jsonSchema: TEST_SCHEMA, + updateValue: (val) => (result = val), + }; + + const closeModal = () => {}; + + await render(); + + await click(".json-editor-btn-add"); + await fillIn("[name='root[0][color]']", "red"); + await click(".d-modal__footer .btn-primary"); + + assert.deepEqual(JSON.parse(result), [{ color: "red", icon: "" }]); + }); +});