Skip to content

Commit

Permalink
Added i18n notification service
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Nov 2, 2012
1 parent ab7bf40 commit 040f592
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ angular.module('app').config(['$routeProvider', '$locationProvider', function ($
$routeProvider.otherwise({redirectTo:'/projectsinfo'});
}]);

angular.module('app').controller('AppCtrl', ['$scope', 'notifications', 'localizedMessages', function($scope, i18nNotifications) {
angular.module('app').controller('AppCtrl', ['$scope', 'i18nNotifications', 'localizedMessages', function($scope, i18nNotifications) {

$scope.notifications = i18nNotifications;

$scope.removeNotification = function (notification) {
notification.$remove();
i18nNotifications.remove(notification);
};

$scope.$on('$routeChangeError', function(event, current, previous, rejection){
Expand Down
3 changes: 3 additions & 0 deletions src/common/services/i18nNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ angular.module('services.i18nNotifications').factory('i18nNotifications', ['loca
},
getCurrent:function () {
return notifications.getCurrent();
},
remove:function (notification) {
return notifications.remove(notification);
}
};

Expand Down
12 changes: 5 additions & 7 deletions src/common/services/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ angular.module('services.notifications', []).factory('notifications', function (
};
var notificationsService = {};

var addNotification = function(notificationsArray, notificationObj) {
if (notificationObj) {
notificationObj.$remove = function() {
notificationsService.remove(notificationObj);
};
notificationsArray.push(notificationObj);
var addNotification = function (notificationsArray, notificationObj) {
if (!angular.isObject(notificationObj)) {
throw new Error("Only object can be added to the notification service");
}
notificationsArray.push(notificationObj);
return notificationObj;
};

Expand All @@ -25,7 +23,7 @@ angular.module('services.notifications', []).factory('notifications', function (
});

notificationsService.getCurrent = function(){
return [].concat(notifications.STICKY).concat(notifications.ROUTE_CURRENT);
return [].concat(notifications.STICKY, notifications.ROUTE_CURRENT);
};

notificationsService.pushSticky = function(notification) {
Expand Down
34 changes: 33 additions & 1 deletion test/unit/common/services/i18nNotificationsSpec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
describe('i18nNotifications', function () {

var i18nNotifications, notifications, localizedMessages;
beforeEach(module('services.i18nNotifications'));
beforeEach(function () {
angular.module('test', ['services.i18nNotifications']).value('I18N.MESSAGES', {});
});
beforeEach(module('test'));
beforeEach(inject(function (_i18nNotifications_, _notifications_, _localizedMessages_) {
i18nNotifications = _i18nNotifications_;
notifications = _notifications_;
localizedMessages = _localizedMessages_;
}));

describe('creating new notification based on localized messages', function () {

it('should add a new sticky notification based on a localized message and its type', function () {
var notifications;
i18nNotifications.pushSticky('i18n.key', 'success');

notifications = i18nNotifications.getCurrent();
expect(notifications.length).toEqual(1);
expect(notifications[0].message).toEqual('?i18n.key?');
expect(notifications[0].type).toEqual('success');
});
});

describe('proxy getCurrent and remove methods', function () {

it('should proxy getCurent method', function () {
spyOn(notifications, 'getCurrent');
i18nNotifications.getCurrent();
expect(notifications.getCurrent).toHaveBeenCalled();
});

it('should proxy remove method', function () {
var notification = {};
spyOn(notifications, 'remove');
i18nNotifications.remove(notification);
expect(notifications.remove).toHaveBeenCalledWith(notification);
});
});

});
28 changes: 12 additions & 16 deletions test/unit/common/services/notificationsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('notifications', function () {
describe('global notifications crud', function () {

it('should allow to add, get and remove notifications', function () {
var not1 = notifications.pushSticky('Watch out!');
var not2 = notifications.pushSticky('Just an info!');
var not1 = notifications.pushSticky({msg:'Watch out!'});
var not2 = notifications.pushSticky({msg:'Just an info!'});
expect(notifications.getCurrent().length).toBe(2);

notifications.remove(not2);
Expand All @@ -25,13 +25,19 @@ describe('notifications', function () {
it('removal of a non-existing notification doesnt trigger errors', function () {
notifications.remove({});
});

it('should reject notifications that are not objects', function () {
expect(function(){
notifications.pushSticky("not an object");
}).toThrow(new Error("Only object can be added to the notification service"));
});
});

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

it('should remove notification after route change', function () {
var sticky = notifications.pushSticky('Will stick around after route change');
var currentRoute = notifications.pushForCurrentRoute('Will go away after route change');
var sticky = notifications.pushSticky({msg:'Will stick around after route change'});
var currentRoute = notifications.pushForCurrentRoute({msg:'Will go away after route change'});
expect(notifications.getCurrent().length).toEqual(2);
$scope.$emit('$routeChangeSuccess');
expect(notifications.getCurrent().length).toEqual(1);
Expand All @@ -43,23 +49,13 @@ describe('notifications', function () {
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 () {
notifications.pushSticky('Will stick around after route change');
notifications.pushForNextRoute('Will not be there till after route change');
notifications.pushSticky({msg:'Will stick around after route change'});
notifications.pushForNextRoute({msg:'Will not be there till after route change'});
expect(notifications.getCurrent().length).toEqual(1);
$scope.$emit('$routeChangeSuccess');
expect(notifications.getCurrent().length).toEqual(2);
$scope.$emit('$routeChangeSuccess');
expect(notifications.getCurrent().length).toEqual(1);
});
});

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

it('should allow removal of notification instances', function () {
var sticky = notifications.pushSticky({msg:'Will be removed!'});
expect(notifications.getCurrent().length).toEqual(1);
notifications.getCurrent()[0].$remove();
expect(notifications.getCurrent().length).toEqual(0);
});
});
});

0 comments on commit 040f592

Please sign in to comment.