From 1396aef99f58e16b069956ab541e4210f22f70bb Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Sat, 30 Nov 2024 17:23:51 +0100 Subject: [PATCH] DEV: Make the `logIn` helper replace current-user service (#30010) --- .../discourse/tests/helpers/qunit-helpers.js | 11 +++++++++-- .../discourse/tests/unit/lib/url-test.js | 2 +- .../unit/services/document-title-test.js | 14 +++----------- .../tests/unit/services/user-tips-test.js | 6 +----- .../unit/utility/plugin-api-test.js | 19 ++++--------------- 5 files changed, 18 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index 3957c35a167..000ad2bc874 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -129,8 +129,15 @@ export function updateCurrentUser(properties) { } // Note: do not use this in acceptance tests. Use `loggedIn: true` instead -export function logIn() { - return User.resetCurrent(currentUser()); +export function logIn(owner) { + const user = User.resetCurrent(currentUser()); + + owner?.unregister("service:current-user"); + owner?.register("service:current-user", user, { + instantiate: false, + }); + + return user; } // Note: Only use if `loggedIn: true` has been used in an acceptance test diff --git a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js index 9beedd6c0fd..f6540783b40 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js @@ -155,7 +155,7 @@ module("Unit | Utility | url", function (hooks) { }); test("routeTo does not rewrite routes started with /my", async function (assert) { - logIn(); + logIn(this.owner); sinon.stub(DiscourseURL, "router").get(() => { return { currentURL: "/" }; }); diff --git a/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js b/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js index 6aba2d1e4cc..d82fe788195 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js @@ -28,13 +28,9 @@ module("Unit | Service | document-title", function (hooks) { }); test("it displays notification counts for logged in users", function (assert) { - const currentUser = logIn(); - this.owner.unregister("service:current-user"); - this.owner.register("service:current-user", currentUser, { - instantiate: false, - }); - + const currentUser = logIn(this.owner); currentUser.user_option.dynamic_favicon = false; + this.documentTitle.setTitle("test notifications"); this.documentTitle.updateNotificationCount(5); assert.strictEqual(document.title, "test notifications"); @@ -46,11 +42,7 @@ module("Unit | Service | document-title", function (hooks) { }); test("it doesn't display notification counts for users in do not disturb", function (assert) { - const currentUser = logIn(); - this.owner.unregister("service:current-user"); - this.owner.register("service:current-user", currentUser, { - instantiate: false, - }); + const currentUser = logIn(this.owner); const date = new Date(); date.setHours(date.getHours() + 1); diff --git a/app/assets/javascripts/discourse/tests/unit/services/user-tips-test.js b/app/assets/javascripts/discourse/tests/unit/services/user-tips-test.js index f21f211f3c4..4a78b94dbf9 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/user-tips-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/user-tips-test.js @@ -8,11 +8,7 @@ module("Unit | Service | user-tips", function (hooks) { setupTest(hooks); test("hideUserTipForever() makes a single request", async function (assert) { - const currentUser = logIn(); - this.owner.unregister("service:current-user"); - this.owner.register("service:current-user", currentUser, { - instantiate: false, - }); + logIn(this.owner); const site = getOwner(this).lookup("service:site"); site.set("user_tips", { first_notification: 1 }); diff --git a/plugins/chat/test/javascripts/unit/utility/plugin-api-test.js b/plugins/chat/test/javascripts/unit/utility/plugin-api-test.js index 2294c6237e7..077f2698c1e 100644 --- a/plugins/chat/test/javascripts/unit/utility/plugin-api-test.js +++ b/plugins/chat/test/javascripts/unit/utility/plugin-api-test.js @@ -1,8 +1,6 @@ -import { getOwner } from "@ember/owner"; import { setupTest } from "ember-qunit"; import { module, test } from "qunit"; import { withPluginApi } from "discourse/lib/plugin-api"; -import User from "discourse/models/user"; import pretender from "discourse/tests/helpers/create-pretender"; import { logIn } from "discourse/tests/helpers/qunit-helpers"; import ChatMessageInteractor, { @@ -36,21 +34,12 @@ module("Chat | Unit | Utility | plugin-api", function (hooks) { "function" ); - logIn(); - const currentUser = User.current(); - getOwner(this).unregister("service:current-user"); - getOwner(this).register("service:current-user", currentUser, { - instantiate: false, - }); - - const message = new ChatFabricators(getOwner(this)).message({ - user: currentUser, - }); - const context = "channel"; + const user = logIn(this.owner); + const message = new ChatFabricators(this.owner).message({ user }); const interactor = new ChatMessageInteractor( - getOwner(this), + this.owner, message, - context + "channel" ); // assert that the initial secondary actions are present