Files
upstream-zulip/web/tests/navbar_alerts.test.cjs
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

155 lines
7.3 KiB
JavaScript

"use strict";
const assert = require("node:assert/strict");
const {addDays} = require("date-fns");
const {make_realm} = require("./lib/example_realm.cjs");
const {mock_esm, zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
const {page_params} = require("./lib/zpage_params.cjs");
const desktop_notifications = mock_esm("../src/desktop_notifications");
const unread = mock_esm("../src/unread");
const {localstorage} = zrequire("localstorage");
const navbar_alerts = zrequire("navbar_alerts");
const {set_current_user, set_realm} = zrequire("state_data");
const current_user = {};
set_current_user(current_user);
const realm = make_realm();
set_realm(realm);
function test(label, f) {
run_test(label, (helpers) => {
window.localStorage.clear();
f(helpers);
});
}
test("should_show_desktop_notifications_banner", ({override}) => {
const ls = localstorage();
// Show desktop notifications banner when following conditions are suitable:
// - Notification permission is not already granted.
// - The user is not a spectator.
// - The device is not mobile.
// - Notification permission is not denied.
// - The user has not said to never show banner on this device again.
ls.set("dontAskForNotifications", undefined);
page_params.is_spectator = false;
override(desktop_notifications, "has_notification_support", () => true);
override(desktop_notifications, "granted_desktop_notifications_permission", () => false);
override(desktop_notifications, "permission_state", () => "default");
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), true);
// Don't ask for permission if user has said to never show banner on this device again.
ls.set("dontAskForNotifications", true);
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
ls.set("dontAskForNotifications", undefined);
// Don't ask for permission if UA has no Notification API support.
override(desktop_notifications, "has_notification_support", () => false);
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
override(desktop_notifications, "has_notification_support", () => true);
// Don't ask for permission if notification is denied by user.
override(desktop_notifications, "permission_state", () => "denied");
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
// Don't ask for permission if notification is already granted by user.
override(desktop_notifications, "granted_desktop_notifications_permission", () => true);
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
// Don't ask for permission if user is a spectator.
page_params.is_spectator = true;
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
});
test("should_show_bankruptcy_banner", ({override}) => {
// Show bankruptcy banner when following conditions are suitable:
// - The user has read at least one message, i.e., furthest_read_time is defined.
// - The user has more than 500 unread messages.
// - The user has not read any message in the last 2 days.
const start_time = new Date("2024-01-01T10:00:00.000Z"); // Wednesday 1/1/2024 10:00:00 AM (UTC+0)
override(page_params, "furthest_read_time", start_time.getTime() / 1000);
override(Date, "now", () => addDays(start_time, 3).getTime()); // Saturday 1/4/2024 10:00:00 AM (UTC+0)
override(unread, "get_unread_message_count", () => 501);
assert.equal(navbar_alerts.should_show_bankruptcy_banner(), true);
// Don't show bankruptcy banner if user has not read any message.
override(page_params, "furthest_read_time", undefined);
assert.equal(navbar_alerts.should_show_bankruptcy_banner(), false);
override(page_params, "furthest_read_time", start_time.getTime() / 1000);
// Don't show bankruptcy banner if user has read any message in the last 2 days.
override(Date, "now", () => addDays(start_time, 1).getTime()); // Thursday 1/2/2024 10:00:00 AM (UTC+0)
assert.equal(navbar_alerts.should_show_bankruptcy_banner(), false);
// Don't show bankruptcy banner if user has less <= 500 unread messages.
override(unread, "get_unread_message_count", () => 500);
assert.equal(navbar_alerts.should_show_bankruptcy_banner(), false);
});
test("should_show_organization_profile_incomplete_banner", ({override}) => {
// Show organization profile incomplete banner when following conditions are suitable:
// - The user is an admin.
// - The organization is created >= 15 days ago.
override(current_user, "is_admin", true);
const start_time = new Date("2024-01-01T10:00:00.000Z"); // Wednesday 1/1/2024 10:00:00 AM (UTC+0)
override(realm, "realm_date_created", start_time.getTime() / 1000);
override(Date, "now", () => addDays(start_time, 15).getTime());
assert.equal(
navbar_alerts.should_show_organization_profile_incomplete_banner(realm.realm_date_created),
true,
);
// Don't show banner if user is not an admin.
override(current_user, "is_admin", false);
assert.equal(
navbar_alerts.should_show_organization_profile_incomplete_banner(realm.realm_date_created),
false,
);
override(current_user, "is_admin", true);
// Don't show banner if organization is created < 15 days ago.
override(Date, "now", () => addDays(start_time, 14).getTime());
assert.equal(
navbar_alerts.should_show_organization_profile_incomplete_banner(realm.realm_date_created),
false,
);
});
test("is_organization_profile_incomplete", ({override}) => {
// The organization profile is incomplete when the realm description is
// empty or not updated after importing the organization from other product.
override(realm, "realm_description", "Organization imported from Slack!");
assert.equal(navbar_alerts.is_organization_profile_incomplete(), true);
override(realm, "realm_description", "");
assert.equal(navbar_alerts.is_organization_profile_incomplete(), true);
// The organization profile is complete if the realm description is updated.
override(realm, "realm_description", "Organization description already set!");
assert.equal(navbar_alerts.is_organization_profile_incomplete(), false);
});
test("should_show_server_upgrade_banner", ({override}) => {
const ls = localstorage();
// Set the initial date, which will be set as the last upgrade nag dismissal time.
const start_time = new Date("2024-01-01T10:00:00.000Z"); // Wednesday 1/1/2024 10:00:00 AM (UTC+0)
override(Date, "now", () => start_time.getTime());
ls.set("lastUpgradeNagDismissalTime", undefined);
assert.equal(navbar_alerts.should_show_server_upgrade_banner(ls), true);
navbar_alerts.set_last_upgrade_nag_dismissal_time(ls);
// Set the date to <= 7 days from the last upgrade nag dismissal time.
override(Date, "now", () => addDays(start_time, 7).getTime()); // Wednesday 1/8/2024 10:00:00 AM (UTC+0)
assert.equal(navbar_alerts.should_show_server_upgrade_banner(ls), false);
// Set the date to > 7 days from the last upgrade nag dismissal time.
override(Date, "now", () => addDays(start_time, 8).getTime()); // Thursday 1/9/2024 10:00:00 AM (UTC+0)
assert.equal(navbar_alerts.should_show_server_upgrade_banner(ls), true);
});