-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from OpenLMIS-Angola/OAM-249
OAM-249: removed caching notifications
- Loading branch information
Showing
2 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/referencedata-system-notification/system-notification.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* This program is part of the OpenLMIS logistics management information system platform software. | ||
* Copyright © 2017 VillageReach | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Affero General Public License as published by the Free Software Foundation, either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero General Public License for more details. You should have received a copy of | ||
* the GNU Affero General Public License along with this program. If not, see | ||
* http://www.gnu.org/licenses. For additional information contact [email protected]. | ||
*/ | ||
|
||
(function() { | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @ngdoc service | ||
* @name systemNotification.systemNotificationService | ||
* | ||
* @description | ||
* Stores system notifications locally. | ||
*/ | ||
angular | ||
.module('referencedata-system-notification') | ||
.service('systemNotificationService', systemNotificationService); | ||
|
||
systemNotificationService.$inject = [ | ||
'localStorageFactory', 'localStorageService', '$q', 'SystemNotificationResource' | ||
]; | ||
|
||
function systemNotificationService(localStorageFactory, localStorageService, $q, SystemNotificationResource) { | ||
|
||
var cachedSystemNotifications = localStorageFactory('systemNotifications'), | ||
SYSTEM_NOTIFICATIONS = 'systemNotifications'; | ||
|
||
this.getSystemNotifications = getSystemNotifications; | ||
this.clearCachedSystemNotifications = clearCachedSystemNotifications; | ||
|
||
/** | ||
* @ngdoc method | ||
* @methodOf systemNotification.systemNotificationService | ||
* @name getSystemNotifications | ||
* | ||
* @description | ||
* Retrieves notifications and saves them in the local storage. | ||
* | ||
* @return {Object} Array of system notifications | ||
*/ | ||
function getSystemNotifications() { | ||
return new SystemNotificationResource().query({ | ||
isDisplayed: true, | ||
expand: 'author' | ||
}) | ||
.then(function(systemNotifications) { | ||
return $q.resolve(systemNotifications.content); | ||
}); | ||
|
||
} | ||
|
||
/** | ||
* @ngdoc method | ||
* @methodOf systemNotification.systemNotificationService | ||
* @name removeById | ||
* | ||
* @description | ||
* Remove all system notifications from local storage. | ||
*/ | ||
function clearCachedSystemNotifications() { | ||
cachedSystemNotifications.remove(SYSTEM_NOTIFICATIONS); | ||
localStorageService.remove(SYSTEM_NOTIFICATIONS); | ||
} | ||
|
||
} | ||
})(); |
106 changes: 106 additions & 0 deletions
106
src/referencedata-system-notification/system-notification.service.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* This program is part of the OpenLMIS logistics management information system platform software. | ||
* Copyright © 2017 VillageReach | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Affero General Public License as published by the Free Software Foundation, either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero General Public License for more details. You should have received a copy of | ||
* the GNU Affero General Public License along with this program. If not, see | ||
* http://www.gnu.org/licenses. For additional information contact [email protected]. | ||
*/ | ||
|
||
describe('systemNotificationService', function() { | ||
beforeEach(function() { | ||
module('referencedata-system-notification'); | ||
|
||
inject(function($injector) { | ||
this.$q = $injector.get('$q'); | ||
this.systemNotificationService = $injector.get('systemNotificationService'); | ||
this.SystemNotificationResource = $injector.get('SystemNotificationResource'); | ||
this.$rootScope = $injector.get('$rootScope'); | ||
this.localStorageService = $injector.get('localStorageService'); | ||
this.UserDataBuilder = $injector.get('UserDataBuilder'); | ||
this.SystemNotificationDataBuilder = $injector.get('SystemNotificationDataBuilder'); | ||
this.ObjectReferenceDataBuilder = $injector.get('ObjectReferenceDataBuilder'); | ||
this.PageDataBuilder = $injector.get('PageDataBuilder'); | ||
}); | ||
|
||
this.localStorageKey = 'systemNotifications'; | ||
|
||
this.systemNotifications = [ | ||
new this.SystemNotificationDataBuilder().build(), | ||
new this.SystemNotificationDataBuilder().build() | ||
]; | ||
|
||
this.systemNotificationsPage = new this.PageDataBuilder() | ||
.withContent(this.systemNotifications) | ||
.build(); | ||
|
||
spyOn(this.SystemNotificationResource.prototype, 'query') | ||
.andReturn(this.$q.resolve(this.systemNotificationsPage)); | ||
|
||
spyOn(this.localStorageService, 'get'); | ||
spyOn(this.localStorageService, 'remove'); | ||
spyOn(this.localStorageService, 'add'); | ||
}); | ||
|
||
describe('getSystemNotifications', function() { | ||
|
||
it('should return list of active system notifications', function() { | ||
var result; | ||
this.systemNotificationService.getSystemNotifications() | ||
.then(function(notifications) { | ||
result = notifications; | ||
}); | ||
this.$rootScope.$apply(); | ||
|
||
expect(result).toEqual(this.systemNotifications); | ||
}); | ||
|
||
it('should fetch system notifications with correct params', function() { | ||
this.systemNotificationService.getSystemNotifications(); | ||
this.$rootScope.$apply(); | ||
|
||
expect(this.SystemNotificationResource.prototype.query).toHaveBeenCalledWith({ | ||
isDisplayed: true, | ||
expand: 'author' | ||
}); | ||
}); | ||
|
||
it('should reject if fetching system notifications fails', function() { | ||
this.SystemNotificationResource.prototype.query.andReturn(this.$q.reject()); | ||
|
||
var rejected; | ||
this.systemNotificationService.getSystemNotifications() | ||
.catch(function() { | ||
rejected = true; | ||
}); | ||
this.$rootScope.$apply(); | ||
|
||
expect(rejected).toEqual(true); | ||
}); | ||
|
||
}); | ||
|
||
describe('clearCachedSystemNotifications', function() { | ||
|
||
it('should clear cache', function() { | ||
this.systemNotificationService.getSystemNotifications(); | ||
this.$rootScope.$apply(); | ||
|
||
expect(this.SystemNotificationResource.prototype.query.callCount).toEqual(1); | ||
|
||
this.systemNotificationService.clearCachedSystemNotifications(); | ||
this.systemNotificationService.getSystemNotifications(); | ||
this.$rootScope.$apply(); | ||
|
||
expect(this.localStorageService.remove).toHaveBeenCalledWith(this.localStorageKey); | ||
expect(this.SystemNotificationResource.prototype.query.callCount).toEqual(2); | ||
}); | ||
|
||
}); | ||
}); |