2021-05-05 19:54:26 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
|
|
const {addDays} = require("date-fns");
|
|
|
|
|
2021-06-16 15:58:34 +02:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2021-05-05 19:54:26 +02:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-05-07 11:45:19 +02:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2021-05-05 19:54:26 +02:00
|
|
|
|
2020-12-24 06:10:37 +01:00
|
|
|
page_params.is_spectator = false;
|
|
|
|
|
2021-05-05 19:54:26 +02:00
|
|
|
const ls_container = new Map();
|
|
|
|
|
|
|
|
const localStorage = set_global("localStorage", {
|
|
|
|
getItem(key) {
|
|
|
|
return ls_container.get(key);
|
|
|
|
},
|
|
|
|
setItem(key, val) {
|
|
|
|
ls_container.set(key, val);
|
|
|
|
},
|
|
|
|
removeItem(key) {
|
|
|
|
ls_container.delete(key);
|
|
|
|
},
|
|
|
|
clear() {
|
|
|
|
ls_container.clear();
|
|
|
|
},
|
|
|
|
});
|
2021-05-07 11:45:19 +02:00
|
|
|
// Dependencies
|
|
|
|
set_global("document", {
|
|
|
|
hasFocus() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
2021-05-05 19:54:26 +02:00
|
|
|
|
|
|
|
const {localstorage} = zrequire("localstorage");
|
2021-05-17 10:01:02 +02:00
|
|
|
const navbar_alerts = zrequire("navbar_alerts");
|
2021-05-07 11:45:19 +02:00
|
|
|
const notifications = zrequire("notifications");
|
|
|
|
const util = zrequire("util");
|
2021-05-05 19:54:26 +02:00
|
|
|
|
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-05-05 19:54:26 +02:00
|
|
|
localStorage.clear();
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-05-05 19:54:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-07 11:45:19 +02:00
|
|
|
test("allow_notification_alert", () => {
|
|
|
|
const ls = localstorage();
|
|
|
|
|
|
|
|
// Show alert.
|
|
|
|
assert.equal(ls.get("dontAskForNotifications"), undefined);
|
|
|
|
util.is_mobile = () => false;
|
|
|
|
notifications.granted_desktop_notifications_permission = () => false;
|
|
|
|
notifications.permission_state = () => "granted";
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if the user said to never show alert on this computer again.
|
|
|
|
ls.set("dontAskForNotifications", true);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if device is mobile.
|
|
|
|
ls.set("dontAskForNotifications", undefined);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
util.is_mobile = () => true;
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
2021-05-18 14:44:05 +02:00
|
|
|
// Avoid showing if notification permission is denied.
|
2021-05-07 11:45:19 +02:00
|
|
|
util.is_mobile = () => false;
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
notifications.permission_state = () => "denied";
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if notification is already granted.
|
|
|
|
notifications.permission_state = () => "granted";
|
|
|
|
notifications.granted_desktop_notifications_permission = () => "granted";
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2020-12-24 06:10:37 +01:00
|
|
|
|
|
|
|
// Don't ask for permission to spectator.
|
|
|
|
util.is_mobile = () => false;
|
|
|
|
notifications.granted_desktop_notifications_permission = () => false;
|
|
|
|
notifications.permission_state = () => "granted";
|
|
|
|
page_params.is_spectator = true;
|
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("profile_incomplete_alert", () => {
|
|
|
|
// Show alert.
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.realm_description = "Organization imported from Slack!";
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if the user is not admin.
|
|
|
|
page_params.is_admin = false;
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if the realm description is already updated.
|
|
|
|
page_params.is_admin = true;
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
page_params.realm_description = "Organization description already set!";
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("server_upgrade_alert hide_duration_expired", ({override}) => {
|
2021-05-05 19:54:26 +02:00
|
|
|
const ls = localstorage();
|
|
|
|
const start_time = new Date(1620327447050); // Thursday 06/5/2021 07:02:27 AM (UTC+0)
|
|
|
|
|
|
|
|
override(Date, "now", () => start_time);
|
|
|
|
assert.equal(ls.get("lastUpgradeNagDismissalTime"), undefined);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), true);
|
|
|
|
navbar_alerts.dismiss_upgrade_nag(ls);
|
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), false);
|
2021-05-05 19:54:26 +02:00
|
|
|
|
|
|
|
override(Date, "now", () => addDays(start_time, 8)); // Friday 14/5/2021 07:02:27 AM (UTC+0)
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), true);
|
|
|
|
navbar_alerts.dismiss_upgrade_nag(ls);
|
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), false);
|
2021-05-05 19:54:26 +02:00
|
|
|
});
|