diff --git a/app/assets/javascripts/discourse/app/components/user-menu/messages-list.js b/app/assets/javascripts/discourse/app/components/user-menu/messages-list.js index 192967c3ca..1b04b49145 100644 --- a/app/assets/javascripts/discourse/app/components/user-menu/messages-list.js +++ b/app/assets/javascripts/discourse/app/components/user-menu/messages-list.js @@ -7,8 +7,11 @@ import UserMenuNotificationItem from "discourse/lib/user-menu/notification-item" import UserMenuMessageItem from "discourse/lib/user-menu/message-item"; import Topic from "discourse/models/topic"; import { mergeSortedLists } from "discourse/lib/utilities"; +import { inject as service } from "@ember/service"; export default class UserMenuMessagesList extends UserMenuNotificationsList { + @service store; + get dismissTypes() { return this.filterByTypes; } @@ -22,7 +25,7 @@ export default class UserMenuMessagesList extends UserMenuNotificationsList { } get showDismiss() { - return this.#unreadMessaagesNotifications > 0; + return this.#unreadMessagesNotifications > 0; } get dismissTitle() { @@ -37,7 +40,7 @@ export default class UserMenuMessagesList extends UserMenuNotificationsList { return "user-menu/messages-list-empty-state"; } - get #unreadMessaagesNotifications() { + get #unreadMessagesNotifications() { const key = `grouped_unread_notifications.${this.site.notification_types.private_message}`; // we're retrieving the value with get() so that Ember tracks the property // and re-renders the UI when it changes. @@ -66,7 +69,7 @@ export default class UserMenuMessagesList extends UserMenuNotificationsList { ); }); - const topics = data.topics.map((t) => Topic.create(t)); + const topics = data.topics.map((t) => this.store.createRecord("topic", t)); await Topic.applyTransformations(topics); const readNotifications = await Notification.initializeNotifications( @@ -100,7 +103,7 @@ export default class UserMenuMessagesList extends UserMenuNotificationsList { modalController.set( "confirmationMessage", I18n.t("notifications.dismiss_confirmation.body.messages", { - count: this.#unreadMessaagesNotifications, + count: this.#unreadMessagesNotifications, }) ); return modalController; diff --git a/app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.js b/app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.js index 0d436f578c..f952008a6a 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/select-kit/pinned-options-test.js @@ -1,29 +1,27 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { render } from "@ember/test-helpers"; -import Topic from "discourse/models/topic"; import { hbs } from "ember-cli-htmlbars"; import selectKit from "discourse/tests/helpers/select-kit-helper"; - -const buildTopic = function (pinned = true) { - return Topic.create({ - id: 1234, - title: "Qunit Test Topic", - deleted_at: new Date(), - pinned, - }); -}; +import { getOwner } from "discourse-common/lib/get-owner"; module("Integration | Component | select-kit/pinned-options", function (hooks) { setupRenderingTest(hooks); - hooks.beforeEach(function () { - this.set("subject", selectKit()); - }); - test("unpinning", async function (assert) { this.siteSettings.automatically_unpin_topics = false; - this.set("topic", buildTopic()); + this.set("subject", selectKit()); + + const store = getOwner(this).lookup("service:store"); + this.set( + "topic", + store.createRecord("topic", { + id: 1234, + title: "Qunit Test Topic", + deleted_at: new Date(), + pinned: true, + }) + ); await render( hbs`` @@ -39,7 +37,17 @@ module("Integration | Component | select-kit/pinned-options", function (hooks) { test("pinning", async function (assert) { this.siteSettings.automatically_unpin_topics = false; - this.set("topic", buildTopic(false)); + this.set("subject", selectKit()); + const store = getOwner(this).lookup("service:store"); + this.set( + "topic", + store.createRecord("topic", { + id: 1234, + title: "Qunit Test Topic", + deleted_at: new Date(), + pinned: false, + }) + ); await render( hbs`` diff --git a/app/assets/javascripts/discourse/tests/integration/components/select-kit/topic-notifications-button-test.js b/app/assets/javascripts/discourse/tests/integration/components/select-kit/topic-notifications-button-test.js index 1a507c33ec..d605269d55 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/select-kit/topic-notifications-button-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/select-kit/topic-notifications-button-test.js @@ -2,15 +2,14 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { render } from "@ember/test-helpers"; import I18n from "I18n"; -import Topic from "discourse/models/topic"; import { query } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; import selectKit from "discourse/tests/helpers/select-kit-helper"; +import { getOwner } from "discourse-common/lib/get-owner"; -const buildTopic = function (opts) { - return Topic.create({ +function buildTopic(opts) { + return this.store.createRecord("topic", { id: 4563, - }).updateFromJson({ title: "Qunit Test Topic", details: { notification_level: opts.level, @@ -20,7 +19,7 @@ const buildTopic = function (opts) { category_id: opts.category_id || null, tags: opts.tags || [], }); -}; +} const originalTranslation = I18n.translations.en.js.topic.notifications.tracking_pm.title; @@ -30,13 +29,17 @@ module( function (hooks) { setupRenderingTest(hooks); + hooks.beforeEach(function () { + this.store = getOwner(this).lookup("service:store"); + }); + hooks.afterEach(function () { I18n.translations.en.js.topic.notifications.tracking_pm.title = originalTranslation; }); test("the header has a localized title", async function (assert) { - this.set("topic", buildTopic({ level: 1 })); + this.set("topic", buildTopic.call(this, { level: 1 })); await render(hbs` `); - this.set("topic", buildTopic({ level: 3, reason: 999 })); + this.set("topic", buildTopic.call(this, { level: 3, reason: 999 })); assert.strictEqual( query(".topic-notifications-button .text").innerText, @@ -117,7 +123,10 @@ module( test("notification reason text - user tracking category", async function (assert) { this.currentUser.set("tracked_category_ids", [88]); - this.set("topic", buildTopic({ level: 2, reason: 8, category_id: 88 })); + this.set( + "topic", + buildTopic.call(this, { level: 2, reason: 8, category_id: 88 }) + ); await render(hbs` el.querySelector(".desc").textContent.trim()); @@ -34,7 +22,18 @@ module( setupRenderingTest(hooks); test("regular topic notification level descriptions", async function (assert) { - this.set("topic", buildTopic("regular")); + const store = getOwner(this).lookup("service:store"); + this.set( + "topic", + store.createRecord("topic", { + id: 4563, + title: "Qunit Test Topic", + archetype: "regular", + details: { + notification_level: 1, + }, + }) + ); await render(hbs` { return { @@ -36,8 +36,13 @@ module( moderator: true, id: 123, }); - const topic = Topic.create({ user_id: this.currentUser.id }); + + const store = getOwner(this).lookup("service:store"); + const topic = store.createRecord("topic", { + user_id: this.currentUser.id, + }); topic.set("category_id", Category.create({ read_restricted: true }).id); + this.siteSettings.allow_featured_topic_on_user_profiles = true; this.set("args", createArgs(topic)); @@ -54,8 +59,13 @@ module( moderator: false, id: 123, }); - const topic = Topic.create({ user_id: this.currentUser.id }); + + const store = getOwner(this).lookup("service:store"); + const topic = store.createRecord("topic", { + user_id: this.currentUser.id, + }); topic.set("category_id", Category.create({ read_restricted: true }).id); + this.siteSettings.allow_featured_topic_on_user_profiles = true; this.set("args", createArgs(topic)); diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js index 803f6586e8..ce9696e272 100644 --- a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js +++ b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js @@ -4,12 +4,12 @@ import { settled } from "@ember/test-helpers"; import pretender, { response } from "discourse/tests/helpers/create-pretender"; import EmberObject from "@ember/object"; import { Placeholder } from "discourse/lib/posts-with-placeholders"; -import Topic from "discourse/models/topic"; import User from "discourse/models/user"; import { next } from "@ember/runloop"; +import { getOwner } from "discourse-common/lib/get-owner"; function topicWithStream(streamDetails) { - let topic = Topic.create(); + const topic = this.store.createRecord("topic"); topic.postStream.setProperties(streamDetails); return topic; } @@ -17,9 +17,13 @@ function topicWithStream(streamDetails) { module("Unit | Controller | topic", function (hooks) { setupTest(hooks); + hooks.beforeEach(function () { + this.store = getOwner(this).lookup("service:store"); + }); + test("editTopic", function (assert) { - const controller = this.owner.lookup("controller:topic"); - const model = Topic.create(); + const controller = getOwner(this).lookup("controller:topic"); + const model = this.store.createRecord("topic"); controller.setProperties({ model }); assert.notOk(controller.editingTopic, "we are not editing by default"); @@ -50,15 +54,15 @@ module("Unit | Controller | topic", function (hooks) { }); test("deleteTopic", function (assert) { - const model = Topic.create(); + const model = this.store.createRecord("topic"); let destroyed = false; let modalDisplayed = false; model.destroy = async () => (destroyed = true); - const siteSettings = this.owner.lookup("service:site-settings"); + const siteSettings = getOwner(this).lookup("service:site-settings"); siteSettings.min_topic_views_for_delete_confirm = 5; - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model, deleteTopicModal: () => (modalDisplayed = true), @@ -75,8 +79,8 @@ module("Unit | Controller | topic", function (hooks) { }); test("toggleMultiSelect", async function (assert) { - const model = Topic.create(); - const controller = this.owner.lookup("controller:topic"); + const model = this.store.createRecord("topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); assert.notOk( @@ -118,8 +122,10 @@ module("Unit | Controller | topic", function (hooks) { }); test("selectedPosts", function (assert) { - const model = topicWithStream({ posts: [{ id: 1 }, { id: 2 }, { id: 3 }] }); - const controller = this.owner.lookup("controller:topic"); + const model = topicWithStream.call(this, { + posts: [{ id: 1 }, { id: 2 }, { id: 3 }], + }); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); controller.set("selectedPostIds", [1, 2, 42]); @@ -136,8 +142,8 @@ module("Unit | Controller | topic", function (hooks) { }); test("selectedAllPosts", function (assert) { - const model = topicWithStream({ stream: [1, 2, 3] }); - const controller = this.owner.lookup("controller:topic"); + const model = topicWithStream.call(this, { stream: [1, 2, 3] }); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); controller.set("selectedPostIds", [1, 2]); @@ -163,7 +169,7 @@ module("Unit | Controller | topic", function (hooks) { }); test("selectedPostsUsername", function (assert) { - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [ { id: 1, username: "gary" }, { id: 2, username: "gary" }, @@ -171,7 +177,7 @@ module("Unit | Controller | topic", function (hooks) { ], stream: [1, 2, 3], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); assert.strictEqual( @@ -210,13 +216,13 @@ module("Unit | Controller | topic", function (hooks) { }); test("showSelectedPostsAtBottom", function (assert) { - const model = Topic.create({ posts_count: 3 }); - const controller = this.owner.lookup("controller:topic"); + const model = this.store.createRecord("topic", { posts_count: 3 }); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); assert.notOk(controller.showSelectedPostsAtBottom, "false on desktop"); - const site = this.owner.lookup("service:site"); + const site = getOwner(this).lookup("service:site"); site.set("mobileView", true); assert.notOk( @@ -233,7 +239,7 @@ module("Unit | Controller | topic", function (hooks) { test("canDeleteSelected", function (assert) { const currentUser = User.create({ admin: false }); - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [ { id: 1, can_delete: false }, { id: 2, can_delete: true }, @@ -242,7 +248,7 @@ module("Unit | Controller | topic", function (hooks) { stream: [1, 2, 3], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model, currentUser, @@ -279,7 +285,7 @@ module("Unit | Controller | topic", function (hooks) { }); test("Can split/merge topic", function (assert) { - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [ { id: 1, post_number: 1, post_type: 1 }, { id: 2, post_number: 2, post_type: 4 }, @@ -289,7 +295,7 @@ module("Unit | Controller | topic", function (hooks) { }); model.set("details.can_move_posts", false); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); assert.notOk( @@ -326,7 +332,7 @@ module("Unit | Controller | topic", function (hooks) { test("canChangeOwner", function (assert) { const currentUser = User.create({ admin: false }); - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [ { id: 1, username: "gary" }, { id: 2, username: "lili" }, @@ -335,7 +341,7 @@ module("Unit | Controller | topic", function (hooks) { }); model.set("currentUser", currentUser); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model, currentUser }); assert.notOk(controller.canChangeOwner, "false when no posts are selected"); @@ -358,7 +364,7 @@ module("Unit | Controller | topic", function (hooks) { test("modCanChangeOwner", function (assert) { const currentUser = User.create({ moderator: false }); - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [ { id: 1, username: "gary" }, { id: 2, username: "lili" }, @@ -367,10 +373,10 @@ module("Unit | Controller | topic", function (hooks) { }); model.set("currentUser", currentUser); - const siteSettings = this.owner.lookup("service:site-settings"); + const siteSettings = getOwner(this).lookup("service:site-settings"); siteSettings.moderators_change_post_ownership = true; - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model, currentUser }); assert.notOk(controller.canChangeOwner, "false when no posts are selected"); @@ -392,7 +398,7 @@ module("Unit | Controller | topic", function (hooks) { }); test("canMergePosts", function (assert) { - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [ { id: 1, username: "gary", can_delete: true }, { id: 2, username: "lili", can_delete: true }, @@ -402,7 +408,7 @@ module("Unit | Controller | topic", function (hooks) { stream: [1, 2, 3], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); assert.notOk(controller.canMergePosts, "false when no posts are selected"); @@ -433,8 +439,8 @@ module("Unit | Controller | topic", function (hooks) { }); test("Select/deselect all", function (assert) { - const controller = this.owner.lookup("controller:topic"); - const model = topicWithStream({ stream: [1, 2, 3] }); + const controller = getOwner(this).lookup("controller:topic"); + const model = topicWithStream.call(this, { stream: [1, 2, 3] }); controller.setProperties({ model }); assert.strictEqual( @@ -459,7 +465,7 @@ module("Unit | Controller | topic", function (hooks) { }); test("togglePostSelection", function (assert) { - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); assert.strictEqual( controller.selectedPostIds[0], @@ -483,10 +489,10 @@ module("Unit | Controller | topic", function (hooks) { }); test("selectBelow", function (assert) { - const site = this.owner.lookup("service:site"); + const site = getOwner(this).lookup("service:site"); site.set("post_types", { small_action: 3, whisper: 4 }); - const model = topicWithStream({ + const model = topicWithStream.call(this, { stream: [1, 2, 3, 4, 5, 6, 7, 8], posts: [ { id: 5, cooked: "whisper post", post_type: 4 }, @@ -495,7 +501,7 @@ module("Unit | Controller | topic", function (hooks) { ], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); assert.deepEqual( @@ -513,11 +519,11 @@ module("Unit | Controller | topic", function (hooks) { response([{ id: 2, level: 1 }]) ); - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [{ id: 1 }, { id: 2 }], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); controller.send("selectReplies", { id: 1 }); @@ -552,10 +558,10 @@ module("Unit | Controller | topic", function (hooks) { }); test("topVisibleChanged", function (assert) { - const model = topicWithStream({ + const model = topicWithStream.call(this, { posts: [{ id: 1 }], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model }); const placeholder = new Placeholder("post-placeholder"); @@ -581,12 +587,12 @@ module("Unit | Controller | topic", function (hooks) { }); const currentUser = EmberObject.create({ moderator: true }); - const model = topicWithStream({ + const model = topicWithStream.call(this, { stream: [2, 3, 4], posts: [post, { id: 3 }, { id: 4 }], }); - const controller = this.owner.lookup("controller:topic"); + const controller = getOwner(this).lookup("controller:topic"); controller.setProperties({ model, currentUser }); const done = assert.async(); diff --git a/app/assets/javascripts/discourse/tests/unit/models/topic-details-test.js b/app/assets/javascripts/discourse/tests/unit/models/topic-details-test.js index 23093db921..5daeb9f65b 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/topic-details-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/topic-details-test.js @@ -1,21 +1,21 @@ import { module, test } from "qunit"; -import Topic from "discourse/models/topic"; import User from "discourse/models/user"; - -function buildDetails(id, topicParams = {}) { - const topic = Topic.create(Object.assign({ id }, topicParams)); - return topic.get("details"); -} +import { getOwner } from "discourse-common/lib/get-owner"; module("Unit | Model | topic-details", function () { test("defaults", function (assert) { - let details = buildDetails(1234); + const store = getOwner(this).lookup("service:store"); + const topic = store.createRecord("topic", { id: 1234 }); + const details = topic.details; + assert.present(details, "the details are present by default"); assert.ok(!details.get("loaded"), "details are not loaded by default"); }); test("updateFromJson", function (assert) { - let details = buildDetails(1234); + const store = getOwner(this).lookup("service:store"); + const topic = store.createRecord("topic", { id: 1234 }); + const details = topic.details; details.updateFromJson({ allowed_users: [{ username: "eviltrout" }], diff --git a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js index e37011fec3..434972596a 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js @@ -11,13 +11,14 @@ import { import { NotificationLevels } from "discourse/lib/notification-levels"; import TopicTrackingState from "discourse/models/topic-tracking-state"; import User from "discourse/models/user"; -import Topic from "discourse/models/topic"; import createStore from "discourse/tests/helpers/create-store"; import sinon from "sinon"; +import { getOwner } from "discourse-common/lib/get-owner"; discourseModule("Unit | Model | topic-tracking-state", function (hooks) { hooks.beforeEach(function () { this.clock = fakeTime("2012-12-31 12:00"); + this.store = getOwner(this).lookup("service:store"); }); hooks.afterEach(function () { @@ -295,7 +296,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { trackingState.updateSeen(111, 7); const list = { topics: [ - Topic.create({ + this.store.createRecord("topic", { highest_post_number: null, id: 111, unread_posts: 10, @@ -325,7 +326,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { const list = { topics: [ - Topic.create({ + this.store.createRecord("topic", { id: 111, unseen: false, seen: true, @@ -366,12 +367,12 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { const list = { topics: [ - Topic.create({ + this.store.createRecord("topic", { id: 111, last_read_post_number: null, unseen: true, }), - Topic.create({ + this.store.createRecord("topic", { id: 222, last_read_post_number: null, unseen: true, @@ -400,7 +401,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { const list = { topics: [ - Topic.create({ + this.store.createRecord("topic", { id: 111, unseen: true, seen: false, @@ -409,7 +410,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { category_id: 1, tags: ["pending"], }), - Topic.create({ + this.store.createRecord("topic", { id: 222, unseen: false, seen: true, @@ -588,12 +589,12 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { assert.strictEqual(trackingState.filterTag, "test"); assert.strictEqual(trackingState.filter, "latest"); - trackingState.trackIncoming("c/cat/subcat/6/l/latest"); + trackingState.trackIncoming("c/cat/sub-cat/6/l/latest"); assert.strictEqual(trackingState.filterCategory.id, 6); assert.strictEqual(trackingState.filterTag, undefined); assert.strictEqual(trackingState.filter, "latest"); - trackingState.trackIncoming("tags/c/cat/subcat/6/test/l/latest"); + trackingState.trackIncoming("tags/c/cat/sub-cat/6/test/l/latest"); assert.strictEqual(trackingState.filterCategory.id, 6); assert.strictEqual(trackingState.filterTag, "test"); assert.strictEqual(trackingState.filter, "latest");