diff --git a/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs b/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs index 4f0e2e1492a..e3b195b7aba 100644 --- a/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs +++ b/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs @@ -5,15 +5,17 @@ > <:body> {{#if @model.badge.errors}} -

{{i18n - "admin.badges.preview.sql_error_header" - }}

+

+ {{i18n "admin.badges.preview.sql_error_header"}} +

{{@model.badge.errors}}
{{else}}

- {{#if this.count}} + {{#if @model.badge.grant_count}} {{html-safe - (i18n "admin.badges.preview.grant_count" count=@model.badge.count) + (i18n + "admin.badges.preview.grant_count" count=@model.badge.grant_count + ) }} {{else}} {{html-safe (i18n "admin.badges.preview.no_grant_count")}} @@ -32,7 +34,7 @@ {{/if}} - {{#if this.sample}} + {{#if @model.badge.sample}}

{{i18n "admin.badges.preview.sample"}}

diff --git a/app/assets/javascripts/admin/addon/components/modal/badge-preview.js b/app/assets/javascripts/admin/addon/components/modal/badge-preview.js index f46a8611d62..399eee7d97e 100644 --- a/app/assets/javascripts/admin/addon/components/modal/badge-preview.js +++ b/app/assets/javascripts/admin/addon/components/modal/badge-preview.js @@ -33,7 +33,7 @@ export default class BadgePreview extends Component { this.args.model.badge.grant_count ); } else { - return this.args.model.badge.sample.length !== 10; + return this.args.model.badge.sample?.length !== 10; } } diff --git a/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs b/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs index c909f9b9923..e76a32e7cfa 100644 --- a/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs +++ b/app/assets/javascripts/admin/addon/templates/admin-badges/show.hbs @@ -175,11 +175,17 @@ {{i18n "admin.badges.preview.link_text"}} + class="preview-badge" + > + {{i18n "admin.badges.preview.link_text"}} | - {{i18n - "admin.badges.preview.plan_text" - }} + + {{i18n "admin.badges.preview.plan_text"}} + {{#if this.preview_loading}} {{i18n "loading"}} {{/if}} diff --git a/app/assets/javascripts/discourse/tests/acceptance/admin-badges-show-test.js b/app/assets/javascripts/discourse/tests/acceptance/admin-badges-show-test.js index 7789f953b11..87574fea2dd 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/admin-badges-show-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/admin-badges-show-test.js @@ -3,11 +3,20 @@ import { exists, query, } from "discourse/tests/helpers/qunit-helpers"; -import { click, fillIn, settled, visit } from "@ember/test-helpers"; +import { click, fillIn, visit } from "@ember/test-helpers"; import { test } from "qunit"; acceptance("Admin - Badges - Show", function (needs) { needs.user(); + needs.settings({ + enable_badge_sql: true, + }); + needs.pretender((server, helper) => { + server.post("/admin/badges/preview.json", () => + helper.response(200, { grant_count: 3, sample: [] }) + ); + }); + test("new badge page", async function (assert) { await visit("/admin/badges/new"); assert.ok( @@ -38,11 +47,10 @@ acceptance("Admin - Badges - Show", function (needs) { "image uploader becomes visible after clicking the upload image radio button" ); - // SQL fields - assert.false(exists("label[for=query]"), "sql input is hidden by default"); - this.siteSettings.enable_badge_sql = true; - await settled(); - assert.true(exists("label[for=query]"), "sql input shows when enabled"); + assert.true( + exists("label[for=query]"), + "sql input is visible when enabled" + ); assert.false( exists("input[name=auto_revoke]"), @@ -112,4 +120,18 @@ acceptance("Admin - Badges - Show", function (needs) { assert.ok(!exists(".image-uploader"), "image uploader becomes hidden"); assert.strictEqual(query(".icon-picker").textContent.trim(), "fa-rocket"); }); + + test("sql input is hidden by default", async function (assert) { + this.siteSettings.enable_badge_sql = false; + await visit("/admin/badges/new"); + assert.dom("label[for=query]").doesNotExist(); + }); + + test("Badge preview displays the grant count", async function (assert) { + await visit("/admin/badges/3"); + await click("a.preview-badge"); + assert + .dom(".badge-query-preview .grant-count") + .hasText("3 badges to be assigned."); + }); }); diff --git a/app/assets/javascripts/discourse/tests/fixtures/badges-fixture.js b/app/assets/javascripts/discourse/tests/fixtures/badges-fixture.js index 489fd752b2d..e944bb4081e 100644 --- a/app/assets/javascripts/discourse/tests/fixtures/badges-fixture.js +++ b/app/assets/javascripts/discourse/tests/fixtures/badges-fixture.js @@ -1727,6 +1727,7 @@ export default { icon: "fa-rocket", image_url: "/images/avatar.png?3", multiple_grant: true, + query: "SELECT 1", }, ], },