DEV: Make the `logIn` helper replace current-user service (#30010)

This commit is contained in:
Jarek Radosz 2024-11-30 17:23:51 +01:00 committed by GitHub
parent dfb74d90c3
commit 1396aef99f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 34 deletions

View File

@ -129,8 +129,15 @@ export function updateCurrentUser(properties) {
} }
// Note: do not use this in acceptance tests. Use `loggedIn: true` instead // Note: do not use this in acceptance tests. Use `loggedIn: true` instead
export function logIn() { export function logIn(owner) {
return User.resetCurrent(currentUser()); 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 // Note: Only use if `loggedIn: true` has been used in an acceptance test

View File

@ -155,7 +155,7 @@ module("Unit | Utility | url", function (hooks) {
}); });
test("routeTo does not rewrite routes started with /my", async function (assert) { test("routeTo does not rewrite routes started with /my", async function (assert) {
logIn(); logIn(this.owner);
sinon.stub(DiscourseURL, "router").get(() => { sinon.stub(DiscourseURL, "router").get(() => {
return { currentURL: "/" }; return { currentURL: "/" };
}); });

View File

@ -28,13 +28,9 @@ module("Unit | Service | document-title", function (hooks) {
}); });
test("it displays notification counts for logged in users", function (assert) { test("it displays notification counts for logged in users", function (assert) {
const currentUser = logIn(); const currentUser = logIn(this.owner);
this.owner.unregister("service:current-user");
this.owner.register("service:current-user", currentUser, {
instantiate: false,
});
currentUser.user_option.dynamic_favicon = false; currentUser.user_option.dynamic_favicon = false;
this.documentTitle.setTitle("test notifications"); this.documentTitle.setTitle("test notifications");
this.documentTitle.updateNotificationCount(5); this.documentTitle.updateNotificationCount(5);
assert.strictEqual(document.title, "test notifications"); 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) { test("it doesn't display notification counts for users in do not disturb", function (assert) {
const currentUser = logIn(); const currentUser = logIn(this.owner);
this.owner.unregister("service:current-user");
this.owner.register("service:current-user", currentUser, {
instantiate: false,
});
const date = new Date(); const date = new Date();
date.setHours(date.getHours() + 1); date.setHours(date.getHours() + 1);

View File

@ -8,11 +8,7 @@ module("Unit | Service | user-tips", function (hooks) {
setupTest(hooks); setupTest(hooks);
test("hideUserTipForever() makes a single request", async function (assert) { test("hideUserTipForever() makes a single request", async function (assert) {
const currentUser = logIn(); logIn(this.owner);
this.owner.unregister("service:current-user");
this.owner.register("service:current-user", currentUser, {
instantiate: false,
});
const site = getOwner(this).lookup("service:site"); const site = getOwner(this).lookup("service:site");
site.set("user_tips", { first_notification: 1 }); site.set("user_tips", { first_notification: 1 });

View File

@ -1,8 +1,6 @@
import { getOwner } from "@ember/owner";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import { module, test } from "qunit"; import { module, test } from "qunit";
import { withPluginApi } from "discourse/lib/plugin-api"; import { withPluginApi } from "discourse/lib/plugin-api";
import User from "discourse/models/user";
import pretender from "discourse/tests/helpers/create-pretender"; import pretender from "discourse/tests/helpers/create-pretender";
import { logIn } from "discourse/tests/helpers/qunit-helpers"; import { logIn } from "discourse/tests/helpers/qunit-helpers";
import ChatMessageInteractor, { import ChatMessageInteractor, {
@ -36,21 +34,12 @@ module("Chat | Unit | Utility | plugin-api", function (hooks) {
"function" "function"
); );
logIn(); const user = logIn(this.owner);
const currentUser = User.current(); const message = new ChatFabricators(this.owner).message({ user });
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 interactor = new ChatMessageInteractor( const interactor = new ChatMessageInteractor(
getOwner(this), this.owner,
message, message,
context "channel"
); );
// assert that the initial secondary actions are present // assert that the initial secondary actions are present