mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
REFACTOR: Move Page title / focus / counts logic to service
We had a handful of methods attached to the root `Discourse` object related to focus and notification counts. This patch pulls them out into a service called `document-title` for updating the title, and a component called `d-document` to attach and listen for browser events related to focus. It also removes some computed properties and observers in favor of plain old Javascript objects.
This commit is contained in:
66
test/javascripts/services/document-title-test.js
Normal file
66
test/javascripts/services/document-title-test.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { discourseModule } from "helpers/qunit-helpers";
|
||||
import { currentUser } from "helpers/qunit-helpers";
|
||||
|
||||
discourseModule("service:document-title", {
|
||||
beforeEach() {
|
||||
this.documentTitle = this.container.lookup("service:document-title");
|
||||
this.documentTitle.currentUser = null;
|
||||
this.container.lookup("session:main").hasFocus = true;
|
||||
},
|
||||
afterEach() {
|
||||
this.documentTitle.reset();
|
||||
}
|
||||
});
|
||||
|
||||
QUnit.test("it updates the document title", function(assert) {
|
||||
this.documentTitle.setTitle("Test Title");
|
||||
assert.equal(document.title, "Test Title", "title is correct");
|
||||
});
|
||||
|
||||
QUnit.test(
|
||||
"it doesn't display notification counts for anonymous users",
|
||||
function(assert) {
|
||||
this.documentTitle.setTitle("test notifications");
|
||||
this.documentTitle.updateNotificationCount(5);
|
||||
assert.equal(document.title, "test notifications");
|
||||
this.documentTitle.setFocus(false);
|
||||
this.documentTitle.updateNotificationCount(6);
|
||||
assert.equal(document.title, "test notifications");
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test("it displays notification counts for logged in users", function(
|
||||
assert
|
||||
) {
|
||||
this.documentTitle.currentUser = currentUser();
|
||||
this.documentTitle.currentUser.dynamic_favicon = false;
|
||||
this.documentTitle.setTitle("test notifications");
|
||||
this.documentTitle.updateNotificationCount(5);
|
||||
assert.equal(document.title, "test notifications");
|
||||
this.documentTitle.setFocus(false);
|
||||
this.documentTitle.updateNotificationCount(6);
|
||||
assert.equal(document.title, "(6) test notifications");
|
||||
this.documentTitle.setFocus(true);
|
||||
assert.equal(document.title, "test notifications");
|
||||
});
|
||||
|
||||
QUnit.test(
|
||||
"it doesn't increment background context counts when focused",
|
||||
function(assert) {
|
||||
this.documentTitle.setTitle("background context");
|
||||
this.documentTitle.setFocus(true);
|
||||
this.documentTitle.incrementBackgroundContextCount();
|
||||
assert.equal(document.title, "background context");
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test("it increments background context counts when not focused", function(
|
||||
assert
|
||||
) {
|
||||
this.documentTitle.setTitle("background context");
|
||||
this.documentTitle.setFocus(false);
|
||||
this.documentTitle.incrementBackgroundContextCount();
|
||||
assert.equal(document.title, "(1) background context");
|
||||
this.documentTitle.setFocus(true);
|
||||
assert.equal(document.title, "background context");
|
||||
});
|
||||
Reference in New Issue
Block a user