Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add simple test for json-schema-editor and remove extra `await` (#27827)

David Taylora4aebc83acc9
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"
}}
+ @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 | <JsonSchemaEditor />", 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(<template>
+ <JsonSchemaEditor
+ @inline={{true}}
+ @model={{model}}
+ @closeModal={{closeModal}}
+ />
+ </template>);
+
+ 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: "" }]);
+ });
+});