A platform for community discussion. Free, open, simple.
DEV: Add topTags and categoryTopTags getters to Site model (#37472)
Alan Guo Xiang Tane9413a1eda25
diff --git a/frontend/discourse/app/models/site.js b/frontend/discourse/app/models/site.jsindex 8ec45ad03a9..8c3a123c8ab 100644--- a/frontend/discourse/app/models/site.js+++ b/frontend/discourse/app/models/site.js@@ -251,6 +251,21 @@ export default class Site extends RestModel {return this.get("topicFlagByIdLookup.action" + id);}+ #transformTags(tags) {+ if (!tags) {+ return [];+ }+ return tags.map((tag) => this.store.createRecord("tag", tag));+ }++ get topTags() {+ return this.#transformTags(this.top_tags);+ }++ get categoryTopTags() {+ return this.#transformTags(this.category_top_tags);+ }+removeCategory(id) {const categories = this.categories;const existingCategory = categories.find((c) => c.id === id);diff --git a/frontend/discourse/tests/unit/models/site-test.js b/frontend/discourse/tests/unit/models/site-test.jsindex bc4ef3e1b5d..a3020ae0784 100644--- a/frontend/discourse/tests/unit/models/site-test.js+++ b/frontend/discourse/tests/unit/models/site-test.js@@ -3,6 +3,7 @@ import { setupTest } from "ember-qunit";import { module, test } from "qunit";import { removeValuesFromArray } from "discourse/lib/array-tools";import Site from "discourse/models/site";+import Tag from "discourse/models/tag";module("Unit | Model | site", function (hooks) {setupTest(hooks);@@ -106,4 +107,50 @@ module("Unit | Model | site", function (hooks) {]);});++ test("topTags returns Tag model instances", function (assert) {+ const store = getOwner(this).lookup("service:store");+ const site = store.createRecord("site", {+ top_tags: [+ { id: 1, name: "bug", slug: "bug" },+ { id: 2, name: "feature", slug: "feature" },+ ],+ });++ const topTags = site.topTags;++ assert.strictEqual(topTags.length, 2);+ assert.true(topTags[0] instanceof Tag);+ assert.true(topTags[1] instanceof Tag);+ });++ test("topTags returns empty array when top_tags is not set", function (assert) {+ const store = getOwner(this).lookup("service:store");+ const site = store.createRecord("site", {});++ assert.deepEqual(site.topTags, []);+ });++ test("categoryTopTags returns Tag model instances", function (assert) {+ const store = getOwner(this).lookup("service:store");+ const site = store.createRecord("site", {+ category_top_tags: [+ { id: 3, name: "support", slug: "support" },+ { id: 4, name: "help", slug: "help" },+ ],+ });++ const categoryTopTags = site.categoryTopTags;++ assert.strictEqual(categoryTopTags.length, 2);+ assert.true(categoryTopTags[0] instanceof Tag);+ assert.true(categoryTopTags[1] instanceof Tag);+ });++ test("categoryTopTags returns empty array when category_top_tags is not set", function (assert) {+ const store = getOwner(this).lookup("service:store");+ const site = store.createRecord("site", {});++ assert.deepEqual(site.categoryTopTags, []);+ });});