Bug 1918062 - Handle clear notification event in in-app-notification-manager. r=arschmitz!,micahilbery

Differential Revision: https://phabricator.services.mozilla.com/D221835

--HG--
extra : rebase_source : b3c6cf505d5e3bc82cc39538be0a9b02bb260b5f
This commit is contained in:
Martin Giger 2024-09-13 11:22:51 +00:00
Родитель be4dba287a
Коммит daee29e03e
2 изменённых файлов: 54 добавлений и 8 удалений

Просмотреть файл

@ -26,6 +26,10 @@ class InAppNotificationManager extends HTMLElement {
lazy.NotificationManager.NEW_NOTIFICATION_EVENT,
this
);
lazy.InAppNotifications.notificationManager.addEventListener(
lazy.NotificationManager.CLEAR_NOTIFICATION_EVENT,
this
);
window.addEventListener("unload", this, { once: true });
}
@ -40,7 +44,10 @@ class InAppNotificationManager extends HTMLElement {
this.#removeManagerListeners();
break;
case lazy.NotificationManager.NEW_NOTIFICATION_EVENT:
this.showNotification(event.detail);
this.#showNotification(event.detail);
break;
case lazy.NotificationManager.CLEAR_NOTIFICATION_EVENT:
this.#hideNotification();
break;
}
}
@ -54,16 +61,18 @@ class InAppNotificationManager extends HTMLElement {
lazy.NotificationManager.NEW_NOTIFICATION_EVENT,
this
);
lazy.InAppNotifications.notificationManager.removeEventListener(
lazy.NotificationManager.CLEAR_NOTIFICATION_EVENT,
this
);
}
/**
* Display a new in-app notification. Replaces the existing notification.
*
* TODO: consider making this and hideNotification private methods.
*
* @param {object} notification - Notification data from the back-end.
*/
showNotification(notification) {
#showNotification(notification) {
const notificationElement = document.createElement("in-app-notification");
//TODO Unconditionally expect this to be a method once in-app-notification
@ -82,7 +91,7 @@ class InAppNotificationManager extends HTMLElement {
/**
* Remove any notification currently displayed.
*/
hideNotification() {
#hideNotification() {
this.#activeNotification?.remove();
this.#activeNotification = null;
}

Просмотреть файл

@ -56,6 +56,15 @@ function showNotification(id, title, description) {
);
}
/**
* Dispatch the event to clear all notifications on the notification manager.
*/
function hideNotification() {
InAppNotifications.notificationManager.dispatchEvent(
new CustomEvent(NotificationManager.CLEAR_NOTIFICATION_EVENT)
);
}
add_task(function test_initialManagerTree() {
Assert.equal(manager.childElementCount, 0, "Should have no children");
});
@ -72,7 +81,7 @@ add_task(function test_showAndHideNotification() {
);
Assert.ok(notification, "Should find a notification");
manager.hideNotification();
hideNotification();
Assert.equal(manager.childElementCount, 0, "Should have no more children");
@ -92,7 +101,7 @@ add_task(function test_showAndHideNotification() {
"Should create a new notification"
);
manager.hideNotification();
hideNotification();
});
add_task(function test_showNotificationReplaces() {
@ -124,7 +133,7 @@ add_task(function test_showNotificationReplaces() {
"Should replace notification"
);
manager.hideNotification();
hideNotification();
});
add_task(function test_disconnected() {
@ -139,6 +148,20 @@ add_task(function test_disconnected() {
);
browser.contentWindow.document.body.append(manager);
showNotification("test", "Test", "Test notification");
manager.remove();
hideNotification();
Assert.equal(
manager.childElementCount,
1,
"Should not hide notification when disconnected"
);
browser.contentWindow.document.body.append(manager);
hideNotification();
});
add_task(function test_unload() {
@ -153,4 +176,18 @@ add_task(function test_unload() {
);
manager.connectedCallback();
showNotification("test", "Test", "Test notification");
browser.contentWindow.dispatchEvent(new Event("unload"));
hideNotification();
Assert.equal(
manager.childElementCount,
1,
"Should not hide notification after the document said it is unloading"
);
manager.connectedCallback();
hideNotification();
});