Skip to content

Commit

Permalink
fix: notification store without duplicates (#7135)
Browse files Browse the repository at this point in the history
closes #7133
- ensure we don't add duplicate notifications to the in process notification store
  • Loading branch information
kirrg001 authored and ErisDS committed Aug 11, 2016
1 parent dd5775c commit 9cd9e03
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
16 changes: 12 additions & 4 deletions core/server/api/notifications.js
Expand Up @@ -41,6 +41,9 @@ notifications = {
*
*
* **takes:** a notification object of the form
*
* If notification message already exists, we return the existing notification object.
*
* ```
* msg = { notifications: [{
* type: 'error', // this can be 'error', 'success', 'warn' and 'info'
Expand Down Expand Up @@ -84,18 +87,23 @@ notifications = {
location: 'bottom',
status: 'alert'
},
addedNotifications = [];
addedNotifications = [], existingNotification;

_.each(options.data.notifications, function (notification) {
notificationCounter = notificationCounter + 1;

notification = _.assign(defaults, notification, {
id: notificationCounter
// status: 'alert'
});

notificationsStore.push(notification);
addedNotifications.push(notification);
existingNotification = _.find(notificationsStore, {message:notification.message});

if (!existingNotification) {
notificationsStore.push(notification);
addedNotifications.push(notification);
} else {
addedNotifications.push(existingNotification);
}
});

return addedNotifications;
Expand Down
6 changes: 3 additions & 3 deletions core/test/integration/api/api_notifications_spec.js
Expand Up @@ -39,7 +39,7 @@ describe('Notifications API', function () {
it('can add, adds defaults (owner)', function (done) {
var msg = {
type: 'info',
message: 'Hello, this is dog'
message: 'Hello, this is another dog'
};

NotificationsAPI.add({notifications: [msg]}, testUtils.context.owner).then(function (result) {
Expand All @@ -60,7 +60,7 @@ describe('Notifications API', function () {
it('can add, adds id and status (internal)', function (done) {
var msg = {
type: 'info',
message: 'Hello, this is dog',
message: 'Hello, this is dog number 3',
id: 99
};

Expand Down Expand Up @@ -157,7 +157,7 @@ describe('Notifications API', function () {
custom: true,
uuid: uuid.v4(),
dismissible: true,
message: 'Hello, this is dog'
message: 'Hello, this is dog number 4'
};

NotificationsAPI.add({notifications: [customNotification]}, testUtils.context.internal).then(function (result) {
Expand Down
36 changes: 36 additions & 0 deletions core/test/unit/api/notifications_spec.js
@@ -0,0 +1,36 @@
var rewire = require('rewire'),
/*jshint unused:false*/
should = require('should'),
NotificationAPI = rewire('../../../server/api/notifications');

describe('UNIT: Notification API', function () {
it('ensure non duplicates', function (done) {
var options = {context: {internal: true}},
notifications = [{
type: 'info',
message: 'Hello, this is dog'
}],
notificationStore = NotificationAPI.__get__('notificationsStore');

NotificationAPI.add({notifications: notifications}, options)
.then(function () {
notificationStore.length.should.eql(1);
return NotificationAPI.add({notifications: notifications}, options);
})
.then(function () {
notificationStore.length.should.eql(1);

notifications.push({
type: 'info',
message: 'Hello, this is cat'
});

return NotificationAPI.add({notifications: notifications}, options);
})
.then(function () {
notificationStore.length.should.eql(2);
done();
})
.catch(done);
});
});

0 comments on commit 9cd9e03

Please sign in to comment.