Skip to content

Commit

Permalink
notification service changes after PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Oct 21, 2012
1 parent eb9b4f7 commit 165f5d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
26 changes: 17 additions & 9 deletions src/common/services/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ angular.module('services.notifications', []).factory('notifications', function (
};
var notificationsService = {};

var clearMixIn = function(notification) {
//TODO: do it better, without creating new function instances each time...
notification.$clear = function() {
notificationsService.clear(this);
};
return notification;
};

$rootScope.$on('$routeChangeSuccess', function () {
notifications.ROUTE.length = 0;

notifications.ROUTE = angular.copy(notifications.NEXT_ROUTE);
notifications.NEXT_ROUTE.length = 0;
});

notificationsService.all = function(){
notificationsService.get = function(){
return [].concat(notifications.GLOBAL).concat(notifications.ROUTE);
};

notificationsService.remove = function(notification){
notificationsService.clear = function(notification){
angular.forEach(notifications, function (notificationsByType) {
var idx = notificationsByType.indexOf(notification);
if (idx>-1){
Expand All @@ -27,22 +35,22 @@ angular.module('services.notifications', []).factory('notifications', function (
});
};

notificationsService.removeAll = function(){
notificationsService.clearAll = function(){
angular.forEach(notifications, function (notificationsByType) {
notificationsByType.length = 0;
});
};

notificationsService.pushGlobal = function(notification) {
notifications.GLOBAL.push(notification);
notificationsService.addFixed = function(notification) {
notifications.GLOBAL.push(clearMixIn(notification));
};

notificationsService.pushRouteChange = function(notification) {
notifications.ROUTE.push(notification);
notificationsService.addRouteChange = function(notification) {
notifications.ROUTE.push(clearMixIn(notification));
};

notificationsService.pushNextRouteChange = function(notification) {
notifications.NEXT_ROUTE.push(notification);
notificationsService.addNextRouteChange = function(notification) {
notifications.NEXT_ROUTE.push(clearMixIn(notification));
};

return notificationsService;
Expand Down
55 changes: 34 additions & 21 deletions test/unit/common/services/notificationsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,63 @@ describe('notifications', function () {

describe('global notifications crud', function () {

it('should allow to add, get and remove all notifications', function () {
it('should allow to add, get and clear all notifications', function () {
var msg1 = {type: 'alert', msg: 'Watch out!'};
var msg2 = {type: 'info', msg: 'Just an info!'};
notifications.pushGlobal(msg1);
notifications.pushGlobal(msg2);
notifications.addFixed(msg1);
notifications.addFixed(msg2);

expect(notifications.all().length).toEqual(2);
expect(notifications.all()[0]).toBe(msg1);
expect(notifications.get().length).toEqual(2);
expect(notifications.get()[0]).toBe(msg1);

notifications.remove(msg2);
expect(notifications.all().length).toEqual(1);
expect(notifications.all()[0]).toBe(msg1);
notifications.clear(msg2);
expect(notifications.get().length).toEqual(1);
expect(notifications.get()[0]).toBe(msg1);

notifications.removeAll();
expect(notifications.all().length).toEqual(0);
notifications.clearAll();
expect(notifications.get().length).toEqual(0);
});
});

describe('notifications expiring after route change', function () {

it('should remove notification after route change', function () {
it('should clear notification after route change', function () {
var msg = {type: 'info', msg: 'Will go away after route change'};

notifications.pushGlobal(msg);
notifications.pushRouteChange(msg);
expect(notifications.all().length).toEqual(2);
notifications.addFixed(msg);
notifications.addRouteChange(msg);
expect(notifications.get().length).toEqual(2);
$scope.$emit('$routeChangeSuccess');
expect(notifications.all().length).toEqual(1);
expect(notifications.get().length).toEqual(1);
});
});


describe('notifications showing on next route change and expiring on a subsequent one', function () {

it('should advertise a notification after a route change and remove on the subsequent route change', function () {
it('should advertise a notification after a route change and clear on the subsequent route change', function () {
var msg = {type: 'info', msg: 'Will go away after route change'};

notifications.pushGlobal(msg);
notifications.pushNextRouteChange(msg);
expect(notifications.all().length).toEqual(1);
notifications.addFixed(msg);
notifications.addNextRouteChange(msg);
expect(notifications.get().length).toEqual(1);
$scope.$emit('$routeChangeSuccess');
expect(notifications.all().length).toEqual(2);
expect(notifications.get().length).toEqual(2);
$scope.$emit('$routeChangeSuccess');
expect(notifications.all().length).toEqual(1);
expect(notifications.get().length).toEqual(1);
});
});

describe('canceling a notification instance', function () {

it('should allow cancelation of notification instances', function () {
var msg = {type:'info', msg:'To be canceled'};

notifications.addFixed(msg);
expect(notifications.get().length).toEqual(1);

notifications.get()[0].$clear();
expect(notifications.get().length).toEqual(0);
});
});
});

0 comments on commit 165f5d1

Please sign in to comment.