discourse/plugins/chat/test/javascripts/unit/services/chat-state-manager-test.js
Jarek Radosz a1b35601fc
FIX: Improve chat route cleanup (#20557)
1. `this.chat.activeChannel = null` was being done in twice
2. using `willTransition()` and checking transition.to.name prefix for route cleanup rather than using `deactivate()` was unnecessarily verbose and could be premature (if something aborted the transition you'd end up in a broken state)
3. `activeChannel` on Chat service can be null, check for that before accessing
2023-04-26 18:18:23 +02:00

153 lines
4.2 KiB
JavaScript

import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import Site from "discourse/models/site";
import sinon from "sinon";
import { getOwner } from "discourse-common/lib/get-owner";
import {
addChatDrawerStateCallback,
resetChatDrawerStateCallbacks,
} from "discourse/plugins/chat/discourse/services/chat-state-manager";
module(
"Discourse Chat | Unit | Service | chat-state-manager",
function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this.subject = getOwner(this).lookup("service:chat-state-manager");
});
hooks.afterEach(function () {
this.subject.reset();
});
test("isFullPagePreferred", function (assert) {
assert.notOk(this.subject.isFullPagePreferred);
this.subject.prefersFullPage();
assert.ok(this.subject.isFullPagePreferred);
this.subject.prefersDrawer();
assert.notOk(this.subject.isFullPagePreferred);
this.subject.prefersDrawer();
Site.currentProp("mobileView", true);
assert.ok(this.subject.isFullPagePreferred);
});
test("isDrawerPreferred", function (assert) {
assert.ok(this.subject.isDrawerPreferred);
this.subject.prefersFullPage();
assert.notOk(this.subject.isDrawerPreferred);
this.subject.prefersDrawer();
assert.ok(this.subject.isDrawerPreferred);
});
test("lastKnownChatURL", function (assert) {
assert.strictEqual(this.subject.lastKnownChatURL, "/chat");
this.subject.storeChatURL("/bar");
assert.strictEqual(this.subject.lastKnownChatURL, "/bar");
});
test("lastKnownAppURL", function (assert) {
assert.strictEqual(this.subject.lastKnownAppURL, "/latest");
sinon.stub(this.subject.router, "currentURL").value("/foo");
this.subject.storeAppURL();
assert.strictEqual(this.subject.lastKnownAppURL, "/foo");
this.subject.storeAppURL("/bar");
assert.strictEqual(this.subject.lastKnownAppURL, "/bar");
});
test("isFullPageActive", function (assert) {
sinon.stub(this.subject.router, "currentRouteName").value("foo");
assert.notOk(this.subject.isFullPageActive);
sinon.stub(this.subject.router, "currentRouteName").value("chat");
assert.ok(this.subject.isFullPageActive);
});
test("didCollapseDrawer", function (assert) {
this.subject.didCollapseDrawer();
assert.strictEqual(this.subject.isDrawerExpanded, false);
assert.strictEqual(this.subject.isDrawerActive, true);
});
test("didExpandDrawer", function (assert) {
const stub = sinon.stub(
this.owner.lookup("service:chat"),
"updatePresence"
);
this.subject.didExpandDrawer();
assert.strictEqual(this.subject.isDrawerExpanded, true);
assert.strictEqual(this.subject.isDrawerActive, true);
sinon.assert.calledOnce(stub);
});
test("didCloseDrawer", function (assert) {
const stub = sinon.stub(
this.owner.lookup("service:chat"),
"updatePresence"
);
this.subject.didCloseDrawer();
assert.strictEqual(this.subject.isDrawerExpanded, false);
assert.strictEqual(this.subject.isDrawerActive, false);
sinon.assert.calledOnce(stub);
});
test("didOpenDrawer", function (assert) {
const stub = sinon.stub(
this.owner.lookup("service:chat"),
"updatePresence"
);
this.subject.didOpenDrawer();
assert.strictEqual(this.subject.isDrawerExpanded, true);
assert.strictEqual(this.subject.isDrawerActive, true);
assert.strictEqual(this.subject.lastKnownChatURL, "/chat");
this.subject.didOpenDrawer("/foo");
assert.strictEqual(this.subject.lastKnownChatURL, "/foo");
sinon.assert.calledTwice(stub);
});
test("callbacks", function (assert) {
this.state = null;
addChatDrawerStateCallback((state) => {
this.state = state;
});
this.subject.didOpenDrawer();
assert.strictEqual(this.state.isDrawerActive, true);
assert.strictEqual(this.state.isDrawerExpanded, true);
this.subject.didCloseDrawer();
assert.strictEqual(this.state.isDrawerActive, false);
assert.strictEqual(this.state.isDrawerExpanded, false);
resetChatDrawerStateCallbacks();
});
}
);