diff --git a/message-center.js b/message-center.js index 4005306..afb41b3 100644 --- a/message-center.js +++ b/message-center.js @@ -10,6 +10,7 @@ MessageCenterModule. function ($rootScope, $sce, $timeout) { return { mcMessages: this.mcMessages || [], + offlistener: this.offlistener || undefined, status: { unseen: 'unseen', shown: 'shown', @@ -65,11 +66,11 @@ MessageCenterModule. if (!this.mcMessages[index].processed) { if (this.mcMessages[index].status == this.status.unseen) { this.mcMessages[index].status = this.status.shown; + this.mcMessages[index].processed = true; } else if (this.mcMessages[index].status == this.status.next) { this.mcMessages[index].status = this.status.unseen; } - this.mcMessages[index].processed = true; } } }, @@ -110,8 +111,9 @@ MessageCenterModule. $rootScope.mcMessages = messageCenterService.mcMessages; messageCenterService.flush(); }; - $rootScope.$on('$locationChangeStart', changeReaction); - + if (messageCenterService.offlistener === undefined) { + messageCenterService.offlistener = $rootScope.$on('$locationChangeSuccess', changeReaction); + } scope.animation = attrs.animation || 'fade in'; } }; diff --git a/test/app/index.html b/test/app/index.html index 01b66ec..32c636c 100644 --- a/test/app/index.html +++ b/test/app/index.html @@ -63,6 +63,13 @@ $scope.saveFailure = function() { messageCenterService.add('danger', 'Something went wrong!'); }; + $scope.saveSuccessGoHome = function() { + messageCenterService.add('success', 'Saved successfully and went home!', {status: messageCenterService.status.next}); + $scope.goIndex(); + }; + $scope.goIndex = function() { + $location.path('/'); + }; }) .controller('AllowHTMLController', function($scope, $location, messageCenterService) { @@ -72,7 +79,14 @@ $scope.plainText = function() { messageCenterService.add('warning', 'HTML is NOT allowed.'); }; - }); + }) + + .controller('PermanentController', function($scope, $location, messageCenterService) { + $scope.goIndex = function() { + $location.path('/'); + }; + }) + ; @@ -100,6 +114,8 @@

Edit

+ + @@ -111,6 +127,13 @@

HTML

+ +
diff --git a/test/app/index3.html b/test/app/index3.html new file mode 100644 index 0000000..bb6208a --- /dev/null +++ b/test/app/index3.html @@ -0,0 +1,144 @@ + + + + + + + + + + + + + + + + + + + +
+ + diff --git a/test/scenarios.js b/test/scenarios.js index 1c7db14..723142d 100644 --- a/test/scenarios.js +++ b/test/scenarios.js @@ -1,4 +1,4 @@ -describe('Message Center', function() { +describe('Message Center with single directive', function() { beforeEach(module('MessageCenterModule')); @@ -88,7 +88,7 @@ describe('Message Center', function() { }); }); - describe('after navigating to multiple different views', function () { + describe('after navigating to multiple different views with permanent message', function () { beforeEach(function () { element('#goPermanent').click(); }); @@ -130,6 +130,21 @@ describe('Message Center', function() { }); }); + describe('after navigating to multiple different views with next message', function () { + it('clears the next message', function () { + element('#goEditSuccess').click(); + var messages = element('div#mc-messages-wrapper .alert'); + expect(messages.count()).toBe(1); + expect(messages.prop('className')).toEqual('alert alert-success fade in'); + expect(messages.text()).toMatch('You have reached the edit page!'); + element('#saveSuccessGoHome').click(); + var messages = element('div#mc-messages-wrapper .alert'); + expect(messages.count()).toBe(1); + expect(messages.prop('className')).toEqual('alert alert-success fade in'); + expect(messages.text()).toMatch('Saved successfully and went home!'); + }); + }); + describe('when going to the allowed HTML page', function(){ beforeEach(function() { element('#goHTML').click(); }); @@ -203,3 +218,31 @@ describe('Message Center', function() { }); }); }); + +describe('Message Center with multiple directives', function() { + + beforeEach(module('MessageCenterModule')); + + beforeEach(function() { browser().navigateTo('/index3.html'); }); + + it('renders an empty ordered list on its initial state', function() { + expect(element('div#mc-messages-wrapper').count()).toBe(1); + expect(element('div#mc-messages-wrapper .alert').count()).toBe(0); + }); + + describe('when navigating through two views', function() { + + it('renders a message with the default "success" level', function() { + element('#goEditSuccess').click(); + var messages = element('div#mc-messages-wrapper .alert'); + expect(messages.count()).toBe(1); + expect(messages.prop('className')).toEqual('alert alert-success fade in'); + expect(messages.text()).toMatch('You have reached the edit page!'); + element('#saveSuccessGoHome').click(); + messages = element('div#mc-messages-wrapper .alert'); + expect(messages.count()).toBe(1); + expect(messages.prop('className')).toEqual('alert alert-success fade in'); + expect(messages.text()).toMatch('Saved successfully and went home!'); + }); + }); +});