[Notification] New tests + bugfix

New test suite for the notification service covering common use cases. Fixed the bug where autoDismiss would be set wrong for info notifications; also treating info notifications appropriately now.
This commit is contained in:
Alex M
2016-10-03 04:19:55 +03:00
parent 0442a31153
commit f2fe6a9885
2 changed files with 187 additions and 107 deletions

View File

@ -208,11 +208,11 @@ define(
* @private * @private
*/ */
NotificationService.prototype.dismissOrMinimize = function (notification) { NotificationService.prototype.dismissOrMinimize = function (notification) {
if (notification.minimizeInsteadOfDismiss) {
//For now minimize everything, and have discussion around which
//kind of messages should or should not be in the minimized
//notifications list
notification.minimize(); notification.minimize();
} else {
notification.dismiss();
}
}; };
/** /**
@ -226,7 +226,9 @@ define(
/** /**
* A convenience method for info notifications. Notifications * A convenience method for info notifications. Notifications
* created via this method will be auto-dismissed after a default * created via this method will be auto-dismissed after a default
* wait period * wait period unless explicitly forbidden by the caller through
* the {autoDismiss} property on the {NotificationModel}, in which
* case the notification will be minimized after the wait.
* @param {NotificationModel | string} message either a string for * @param {NotificationModel | string} message either a string for
* the title of the notification message, or a {@link NotificationModel} * the title of the notification message, or a {@link NotificationModel}
* defining the options notification to display * defining the options notification to display
@ -235,7 +237,6 @@ define(
*/ */
NotificationService.prototype.info = function (message) { NotificationService.prototype.info = function (message) {
var notificationModel = typeof message === "string" ? {title: message} : message; var notificationModel = typeof message === "string" ? {title: message} : message;
notificationModel.autoDismiss = notificationModel.autoDismiss || true;
notificationModel.severity = "info"; notificationModel.severity = "info";
return this.notify(notificationModel); return this.notify(notificationModel);
}; };
@ -306,27 +307,52 @@ define(
activeNotification = self.active.notification, activeNotification = self.active.notification,
topic = this.topic(); topic = this.topic();
notificationModel.severity = notificationModel.severity || "info";
notification = { notification = {
model: notificationModel, model: notificationModel,
minimize: function () { minimize: function () {
self.minimize(self, notification); self.minimize(self, notification);
}, },
dismiss: function () { dismiss: function () {
self.dismiss(self, notification); self.dismiss(self, notification);
topic.notify(); topic.notify();
}, },
dismissOrMinimize: function () { dismissOrMinimize: function () {
self.dismissOrMinimize(notification); self.dismissOrMinimize(notification);
}, },
onDismiss: function (callback) { onDismiss: function (callback) {
topic.listen(callback); topic.listen(callback);
} },
};
notificationModel.severity = notificationModel.severity || "info"; autoDismiss: (function () {
if (notificationModel.autoDismiss === true) { if (notificationModel.severity === "info") {
notificationModel.autoDismiss = this.AUTO_DISMISS_TIMEOUT; return true;
} }
return notificationModel.autoDismiss;
})(),
autoDismissTimeout: (function () {
if (typeof notificationModel.autoDismiss === "number") {
return notificationModel.autoDismiss;
}
return self.AUTO_DISMISS_TIMEOUT;
})(),
minimizeInsteadOfDismiss: (function () {
if (notificationModel.severity === "info") {
if (notificationModel.autoDismiss === false) {
return true;
}
return false;
}
return true;
})()
};
//Notifications support a 'dismissable' attribute. This is a //Notifications support a 'dismissable' attribute. This is a
// convenience to support adding a 'dismiss' option to the // convenience to support adding a 'dismiss' option to the
@ -370,27 +396,23 @@ define(
} }
return notification; return notification;
}; };
/** /**
* Used internally by the NotificationService * Used internally by the NotificationService
* @private * @private
*/ */
NotificationService.prototype.setActiveNotification = NotificationService.prototype.setActiveNotification = function (notification) {
function (notification) {
var timeout; var timeout;
this.active.notification = notification; this.active.notification = notification;
/* /*
If autoDismiss has been specified, OR there are other If autoDismiss has been specified, OR there are other
notifications queued for display, setup a timeout to notifications queued for display, setup a timeout to
dismiss the dialog. dismiss the dialog.
*/ */
if (notification && (notification.model.autoDismiss || if (notification && (notification.autoDismiss || this.selectNextNotification())) {
this.selectNextNotification())) { timeout = notification.autoDismissTimeout || this.AUTO_DISMISS_TIMEOUT;
timeout = notification.model.autoDismiss || this.AUTO_DISMISS_TIMEOUT;
this.active.timeout = this.$timeout(function () { this.active.timeout = this.$timeout(function () {
notification.dismissOrMinimize(); notification.dismissOrMinimize();
}, timeout); }, timeout);

View File

@ -33,8 +33,13 @@ define(
mockTopicFunction, mockTopicFunction,
mockTopicObject, mockTopicObject,
infoModel, infoModel,
alertModel,
errorModel; errorModel;
function elapseTimeout() {
mockTimeout.mostRecentCall.args[0]();
}
beforeEach(function () { beforeEach(function () {
mockTimeout = jasmine.createSpy("$timeout"); mockTimeout = jasmine.createSpy("$timeout");
mockTopicFunction = jasmine.createSpy("topic"); mockTopicFunction = jasmine.createSpy("topic");
@ -42,124 +47,180 @@ define(
mockTopicFunction.andReturn(mockTopicObject); mockTopicFunction.andReturn(mockTopicObject);
mockAutoDismiss = mockMinimizeTimeout = 1000; mockAutoDismiss = mockMinimizeTimeout = 1000;
notificationService = new NotificationService( notificationService = new NotificationService(mockTimeout, mockTopicFunction, mockAutoDismiss, mockMinimizeTimeout);
mockTimeout, mockTopicFunction, mockAutoDismiss, mockMinimizeTimeout);
infoModel = { infoModel = {
title: "Mock Info Notification", title: "Mock Info Notification",
severity: "info" severity: "info"
}; };
alertModel = {
title: "Mock Alert Notification",
severity: "alert"
};
errorModel = { errorModel = {
title: "Mock Error Notification", title: "Mock Error Notification",
severity: "error" severity: "error"
}; };
}); });
it("activates info notifications", function () {
var activeNotification;
notificationService.notify(infoModel);
activeNotification = notificationService.getActiveNotification();
expect(activeNotification.model).toBe(infoModel);
});
it("notifies listeners on dismissal of notification", function () { it("notifies listeners on dismissal of notification", function () {
var notification, var dismissListener = jasmine.createSpy("ondismiss");
dismissListener = jasmine.createSpy("ondismiss"); var notification = notificationService.notify(infoModel);
notification = notificationService.notify(infoModel);
notification.onDismiss(dismissListener); notification.onDismiss(dismissListener);
expect(mockTopicObject.listen).toHaveBeenCalled(); expect(mockTopicObject.listen).toHaveBeenCalled();
notification.dismiss(); notification.dismiss();
expect(mockTopicObject.notify).toHaveBeenCalled(); expect(mockTopicObject.notify).toHaveBeenCalled();
mockTopicObject.listen.mostRecentCall.args[0](); mockTopicObject.listen.mostRecentCall.args[0]();
expect(dismissListener).toHaveBeenCalled(); expect(dismissListener).toHaveBeenCalled();
}); });
it("activates an info notification built with just the title", function () { describe("when receiving info notifications", function () {
var activeNotification, it("minimizes info notifications if the caller disables auto-dismiss", function () {
notificationTitle = "Test info notification"; infoModel.autoDismiss = false;
notificationService.info(notificationTitle); var notification = notificationService.info(infoModel);
activeNotification = notificationService.getActiveNotification(); elapseTimeout();
expect(activeNotification.model.title).toBe(notificationTitle); // 2nd elapse for the minimize animation timeout
expect(activeNotification.model.severity).toBe("info"); elapseTimeout();
expect(notificationService.getActiveNotification()).toBeUndefined();
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
}); });
it("gets a new info notification with numerical auto-dismiss specified. ", function () { it("dismisses info notifications if the caller ignores auto-dismiss", function () {
var activeNotification; notificationService.info(infoModel);
infoModel.autoDismiss = 1000; elapseTimeout();
notificationService.notify(infoModel); expect(notificationService.getActiveNotification()).toBeUndefined();
activeNotification = notificationService.getActiveNotification(); expect(notificationService.notifications.length).toEqual(0);
expect(activeNotification.model).toBe(infoModel);
mockTimeout.mostRecentCall.args[0]();
expect(mockTimeout.calls.length).toBe(2);
mockTimeout.mostRecentCall.args[0]();
activeNotification = notificationService.getActiveNotification();
expect(activeNotification).toBeUndefined();
}); });
it("gets a new notification with boolean auto-dismiss specified. ", function () { it("dismisses info notifications if the caller requests auto-dismissal", function () {
var activeNotification;
infoModel.autoDismiss = true; infoModel.autoDismiss = true;
notificationService.notify(infoModel); notificationService.info(infoModel);
activeNotification = notificationService.getActiveNotification(); elapseTimeout();
expect(activeNotification.model).toBe(infoModel); expect(notificationService.getActiveNotification()).toBeUndefined();
mockTimeout.mostRecentCall.args[0](); expect(notificationService.notifications.length).toEqual(0);
expect(mockTimeout.calls.length).toBe(2);
mockTimeout.mostRecentCall.args[0]();
activeNotification = notificationService.getActiveNotification();
expect(activeNotification).toBeUndefined();
}); });
it("allows minimization of notifications", function () { it("uses a custom auto-dismiss timeout value if provided", function () {
var notification, var timeoutValueInModel = 1500;
activeNotification; var timeoutValueUsedByService = 0;
mockTimeout.andCallFake(function (callback, timeout) {
infoModel.autoDismiss = false; timeoutValueUsedByService = timeout;
notification = notificationService.notify(infoModel); });
infoModel.autoDismiss = timeoutValueInModel;
activeNotification = notificationService.getActiveNotification(); notificationService.info(infoModel);
expect(activeNotification.model).toBe(infoModel); expect(timeoutValueUsedByService).toEqual(timeoutValueInModel);
notification.minimize(); });
mockTimeout.mostRecentCall.args[0]();
activeNotification = notificationService.getActiveNotification();
expect(activeNotification).toBeUndefined();
expect(notificationService.notifications.length).toBe(1);
}); });
it("allows dismissal of notifications", function () { describe("when receiving alert notifications", function () {
var notification, it("minimizes alert notifications if the caller enables auto-dismiss", function () {
activeNotification; alertModel.autoDismiss = true;
var notification = notificationService.alert(alertModel);
elapseTimeout();
elapseTimeout();
expect(notificationService.getActiveNotification()).toBeUndefined();
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
});
infoModel.autoDismiss = false; it("keeps alert notifications active if the caller disables auto-dismiss", function () {
notification = notificationService.notify(infoModel); mockTimeout.andCallFake(function (callback, time) {
callback();
});
alertModel.autoDismiss = false;
var notification = notificationService.alert(alertModel);
expect(notificationService.getActiveNotification()).toEqual(notification);
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
});
activeNotification = notificationService.getActiveNotification(); it("keeps alert notifications active if the caller ignores auto-dismiss", function () {
expect(activeNotification.model).toBe(infoModel); mockTimeout.andCallFake(function (callback, time) {
notification.dismiss(); callback();
activeNotification = notificationService.getActiveNotification(); });
expect(activeNotification).toBeUndefined(); var notification = notificationService.alert(alertModel);
expect(notificationService.notifications.length).toBe(0); expect(notificationService.getActiveNotification()).toEqual(notification);
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
});
it("uses a custom auto-dismiss timeout value if provided", function () {
var timeoutValueInModel = 1500;
var timeoutValueUsedByService = 0;
mockTimeout.andCallFake(function (callback, timeout) {
timeoutValueUsedByService = timeout;
});
alertModel.autoDismiss = timeoutValueInModel;
notificationService.alert(alertModel);
expect(timeoutValueUsedByService).toEqual(timeoutValueInModel);
});
});
describe("when receiving error notifications", function () {
it("minimizes error notifications if the caller enables auto-dismiss", function () {
errorModel.autoDismiss = true;
var notification = notificationService.error(errorModel);
elapseTimeout();
elapseTimeout();
expect(notificationService.getActiveNotification()).toBeUndefined();
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
});
it("keeps error notifications active if the caller disables auto-dismiss", function () {
mockTimeout.andCallFake(function (callback, time) {
callback();
});
errorModel.autoDismiss = false;
var notification = notificationService.error(errorModel);
expect(notificationService.getActiveNotification()).toEqual(notification);
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
});
it("keeps error notifications active if the caller ignores auto-dismiss", function () {
mockTimeout.andCallFake(function (callback, time) {
callback();
});
var notification = notificationService.error(errorModel);
expect(notificationService.getActiveNotification()).toEqual(notification);
expect(notificationService.notifications.length).toEqual(1);
expect(notificationService.notifications[0]).toEqual(notification);
});
it("uses a custom auto-dismiss timeout value if provided", function () {
var timeoutValueInModel = 1500;
var timeoutValueUsedByService = 0;
mockTimeout.andCallFake(function (callback, timeout) {
timeoutValueUsedByService = timeout;
});
errorModel.autoDismiss = timeoutValueInModel;
notificationService.error(errorModel);
expect(timeoutValueUsedByService).toEqual(timeoutValueInModel);
});
}); });
describe("when called with multiple notifications", function () { describe("when called with multiple notifications", function () {
it("auto-dismisses the previously active notification, making the new notification active", function () { it("auto-dismisses the previously active notification, making the new notification active", function () {
var activeNotification; var activeNotification;
infoModel.autoDismiss = false;
//First pre-load with a info message //First pre-load with a info message
notificationService.notify(infoModel); notificationService.notify(infoModel);
activeNotification = activeNotification = notificationService.getActiveNotification();
notificationService.getActiveNotification();
//Initially expect the active notification to be info //Initially expect the active notification to be info
expect(activeNotification.model).toBe(infoModel); expect(activeNotification.model).toBe(infoModel);
//Then notify of an error //Then notify of an error
notificationService.notify(errorModel); notificationService.notify(errorModel);
//But it should be auto-dismissed and replaced with the //But it should be auto-dismissed and replaced with the
// error notification // error notification
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
//Two timeouts, one is to force minimization after //Two timeouts, one is to force minimization after
// displaying the message for a minimum period, the // displaying the message for a minimum period, the
// second is to allow minimization animation to take place. // second is to allow minimization animation to take place.
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
activeNotification = notificationService.getActiveNotification(); activeNotification = notificationService.getActiveNotification();
expect(activeNotification.model).toBe(errorModel); expect(activeNotification.model).toBe(errorModel);
}); });
@ -172,16 +233,15 @@ define(
notificationService.notify(infoModel); notificationService.notify(infoModel);
expect(notificationService.notifications.length).toEqual(2); expect(notificationService.notifications.length).toEqual(2);
//Mock the auto-minimize //Mock the auto-minimize
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
//Two timeouts, one is to force minimization after //Two timeouts, one is to force minimization after
// displaying the message for a minimum period, the // displaying the message for a minimum period, the
// second is to allow minimization animation to take place. // second is to allow minimization animation to take place.
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
//Previous error message should be minimized, not //Previous error message should be minimized, not
// dismissed // dismissed
expect(notificationService.notifications.length).toEqual(2); expect(notificationService.notifications.length).toEqual(2);
activeNotification = activeNotification = notificationService.getActiveNotification();
notificationService.getActiveNotification();
expect(activeNotification.model).toBe(infoModel); expect(activeNotification.model).toBe(infoModel);
expect(errorModel.minimized).toEqual(true); expect(errorModel.minimized).toEqual(true);
}); });
@ -204,27 +264,25 @@ define(
notificationService.notify(error3); notificationService.notify(error3);
expect(notificationService.notifications.length).toEqual(3); expect(notificationService.notifications.length).toEqual(3);
//Mock the auto-minimize //Mock the auto-minimize
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
//Two timeouts, one is to force minimization after //Two timeouts, one is to force minimization after
// displaying the message for a minimum period, the // displaying the message for a minimum period, the
// second is to allow minimization animation to take place. // second is to allow minimization animation to take place.
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
//Previous error message should be minimized, not //Previous error message should be minimized, not
// dismissed // dismissed
expect(notificationService.notifications.length).toEqual(3); expect(notificationService.notifications.length).toEqual(3);
activeNotification = activeNotification = notificationService.getActiveNotification();
notificationService.getActiveNotification();
expect(activeNotification.model).toBe(error2); expect(activeNotification.model).toBe(error2);
expect(errorModel.minimized).toEqual(true); expect(errorModel.minimized).toEqual(true);
//Mock the second auto-minimize //Mock the second auto-minimize
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
//Two timeouts, one is to force minimization after //Two timeouts, one is to force minimization after
// displaying the message for a minimum period, the // displaying the message for a minimum period, the
// second is to allow minimization animation to take place. // second is to allow minimization animation to take place.
mockTimeout.mostRecentCall.args[0](); elapseTimeout();
activeNotification = activeNotification = notificationService.getActiveNotification();
notificationService.getActiveNotification();
expect(activeNotification.model).toBe(error3); expect(activeNotification.model).toBe(error3);
expect(error2.minimized).toEqual(true); expect(error2.minimized).toEqual(true);
}); });