Skip to content

Commit 5dfe636

Browse files
Merge pull request #87 from OpenLMIS/OLMIS-8257-fix-app-cache-controller-crash
OLMIS-8257: Guard against missing window.applicationCache in app-cache controller
2 parents 5b46afb + 8c59454 commit 5dfe636

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/openlmis-app-cache/openlmis-app-cache.controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
* Initialization method of the OpenlmisAppCacheController.
6161
*/
6262
function onInit() {
63-
appCache.addEventListener('updateready', setUpdateReady);
63+
if (appCache) {
64+
appCache.addEventListener('updateready', setUpdateReady);
65+
}
6466
vm.buildDate = OPENLMIS_BUILD_DATE;
6567
setUpdateReady();
6668
}
@@ -97,7 +99,7 @@
9799
}
98100

99101
function setUpdateReady() {
100-
vm.updateReady = appCache.status === appCache.UPDATEREADY;
102+
vm.updateReady = !!appCache && appCache.status === appCache.UPDATEREADY;
101103
}
102104

103105
}

src/openlmis-app-cache/openlmis-app-cache.controller.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('OpenlmisAppCacheController', function() {
2424
this.$rootScope = $injector.get('$rootScope');
2525
this.$controller = $injector.get('$controller');
2626
this.$q = $injector.get('$q');
27+
this.OPENLMIS_BUILD_DATE = $injector.get('OPENLMIS_BUILD_DATE');
2728
});
2829

2930
this.applicationCacheMock = jasmine.createSpyObj('applicationCache', [
@@ -146,4 +147,47 @@ describe('OpenlmisAppCacheController', function() {
146147

147148
});
148149

150+
// The deprecated window.applicationCache has been removed from modern browsers,
151+
// so $window.applicationCache can be undefined. The controller must not crash.
152+
describe('when applicationCache is not available', function() {
153+
154+
beforeEach(function() {
155+
this.vm = this.$controller('OpenlmisAppCacheController', {
156+
$window: {
157+
applicationCache: undefined,
158+
location: this.locationMock
159+
}
160+
});
161+
});
162+
163+
it('should not throw on init', function() {
164+
var vm = this.vm;
165+
166+
expect(function() {
167+
vm.$onInit();
168+
}).not.toThrow();
169+
});
170+
171+
it('should still set the build date', function() {
172+
this.vm.$onInit();
173+
174+
expect(this.vm.buildDate).toBe(this.OPENLMIS_BUILD_DATE);
175+
});
176+
177+
it('should set updateReady flag to false', function() {
178+
this.vm.$onInit();
179+
180+
expect(this.vm.updateReady).toBe(false);
181+
});
182+
183+
it('should do nothing when updateCache is called', function() {
184+
this.vm.$onInit();
185+
186+
this.vm.updateCache();
187+
188+
expect(this.confirmService.confirm).not.toHaveBeenCalled();
189+
});
190+
191+
});
192+
149193
});

0 commit comments

Comments
 (0)