discourse/plugins/chat/test/javascripts/unit/services/chat-state-manager-test.js
David Taylor b15c1d28c9
DEV: Introduce experimental viewport-based mobile mode (#32859)
Introduces the viewport_based_mobile_mode experimental site setting.
When enabled, user-agent-based mobile/desktop detection will be replaced
with viewport-width logic. 'mobile mode' is enabled for any viewport
less than our 'sm' breakpoint (40rem, or 640px at default font size).

When this mode is enabled, mobile/desktop toggle buttons are hidden,
since they are non-functional.

Tests are also updated to use a consistent method for force-enabling the
legacy mobile mode. All state is now stored in `lib/mobile`, and the
`Site` model references that via a getter.
2025-05-23 13:01:04 +01:00

152 lines
4 KiB
JavaScript
Vendored

import { getOwner } from "@ember/owner";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
import { forceMobile } from "discourse/lib/mobile";
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.false(this.subject.isFullPagePreferred);
this.subject.prefersFullPage();
assert.true(this.subject.isFullPagePreferred);
this.subject.prefersDrawer();
assert.false(this.subject.isFullPagePreferred);
this.subject.prefersDrawer();
forceMobile();
assert.true(this.subject.isFullPagePreferred);
});
test("isDrawerPreferred", function (assert) {
assert.true(this.subject.isDrawerPreferred);
this.subject.prefersFullPage();
assert.false(this.subject.isDrawerPreferred);
this.subject.prefersDrawer();
assert.true(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.false(this.subject.isFullPageActive);
sinon.stub(this.subject.router, "currentRouteName").value("chat");
assert.true(this.subject.isFullPageActive);
});
test("didCollapseDrawer", function (assert) {
this.subject.didCollapseDrawer();
assert.false(this.subject.isDrawerExpanded);
assert.true(this.subject.isDrawerActive);
});
test("didExpandDrawer", function (assert) {
const stub = sinon.stub(
this.owner.lookup("service:chat"),
"updatePresence"
);
this.subject.didExpandDrawer();
assert.true(this.subject.isDrawerExpanded);
assert.true(this.subject.isDrawerActive);
sinon.assert.calledOnce(stub);
});
test("didCloseDrawer", function (assert) {
const stub = sinon.stub(
this.owner.lookup("service:chat"),
"updatePresence"
);
this.subject.didCloseDrawer();
assert.false(this.subject.isDrawerExpanded);
assert.false(this.subject.isDrawerActive);
sinon.assert.calledOnce(stub);
});
test("didOpenDrawer", function (assert) {
const stub = sinon.stub(
this.owner.lookup("service:chat"),
"updatePresence"
);
this.subject.didOpenDrawer();
assert.true(this.subject.isDrawerExpanded);
assert.true(this.subject.isDrawerActive);
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.true(this.state.isDrawerActive);
assert.true(this.state.isDrawerExpanded);
this.subject.didCloseDrawer();
assert.false(this.state.isDrawerActive);
assert.false(this.state.isDrawerExpanded);
resetChatDrawerStateCallbacks();
});
}
);