discourse/plugins/chat/test/javascripts/unit/services/chat-history-test.js
Régis Hanol b9e762981a
FIX: Chat back button loses starred context after drawer reopen (#40219)
When the chat drawer is closed and reopened on a starred channel, the
back button took the user to the default channels list instead of the
starred-channels list they had originally come from.

Root cause: `chatHistory.visit(route)` always appended to history. When
the drawer reopens to `lastKnownChatURL` (the channel itself), the same
route is appended a second time, shifting `previousRoute` off the
original `chat.starred-channels` entry. The back-button fallback in
`drawer-routes/channel.gjs` then routed to `chat.channels`.

Fix: dedupe consecutive identical route visits in `chatHistory.visit` by
comparing name and params (via `deepEqual`). Reopening to the same URL
no longer clobbers the user's real navigation context.

Adds a unit test for the dedup behavior, a system spec covering the
close/reopen scenario, and a view-agnostic `open_channel_row` helper on
the drawer page object (the existing `open_channel` is scoped to the
channels-list view only and doesn't work from the starred view).

https://meta.discourse.org/t/403431
2026-05-21 17:55:46 +02:00

37 lines
1.3 KiB
JavaScript
Vendored

import { getOwner } from "@ember/owner";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
module("Unit | Service | chat-history", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this.subject = getOwner(this).lookup("service:chat-history");
});
test("visit dedupes consecutive identical routes", function (assert) {
this.subject.visit({ name: "chat.starred-channels", params: {} });
this.subject.visit({ name: "chat.channel", params: { channelId: "1" } });
this.subject.visit({ name: "chat.channel", params: { channelId: "1" } });
assert.strictEqual(
this.subject.history.length,
2,
"the duplicate visit is skipped"
);
assert.strictEqual(
this.subject.previousRoute.name,
"chat.starred-channels",
"previousRoute reflects the real prior step, not the duplicate"
);
});
test("visit treats same name with different params as distinct", function (assert) {
this.subject.visit({ name: "chat.channel", params: { channelId: "1" } });
this.subject.visit({ name: "chat.channel", params: { channelId: "2" } });
assert.strictEqual(this.subject.history.length, 2);
assert.strictEqual(this.subject.previousRoute.params.channelId, "1");
assert.strictEqual(this.subject.currentRoute.params.channelId, "2");
});
});