From 8c59454311c14b596d090a03f7567c25b8d33f7b Mon Sep 17 00:00:00 2001 From: Denys Mamchura Date: Thu, 16 Jul 2026 15:52:59 +0200 Subject: [PATCH] OLMIS-8257: Guard against missing window.applicationCache in app-cache controller --- .../openlmis-app-cache.controller.js | 6 ++- .../openlmis-app-cache.controller.spec.js | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/openlmis-app-cache/openlmis-app-cache.controller.js b/src/openlmis-app-cache/openlmis-app-cache.controller.js index f6657aea..5e730a36 100644 --- a/src/openlmis-app-cache/openlmis-app-cache.controller.js +++ b/src/openlmis-app-cache/openlmis-app-cache.controller.js @@ -60,7 +60,9 @@ * Initialization method of the OpenlmisAppCacheController. */ function onInit() { - appCache.addEventListener('updateready', setUpdateReady); + if (appCache) { + appCache.addEventListener('updateready', setUpdateReady); + } vm.buildDate = OPENLMIS_BUILD_DATE; setUpdateReady(); } @@ -97,7 +99,7 @@ } function setUpdateReady() { - vm.updateReady = appCache.status === appCache.UPDATEREADY; + vm.updateReady = !!appCache && appCache.status === appCache.UPDATEREADY; } } diff --git a/src/openlmis-app-cache/openlmis-app-cache.controller.spec.js b/src/openlmis-app-cache/openlmis-app-cache.controller.spec.js index bcd92c53..8306484f 100644 --- a/src/openlmis-app-cache/openlmis-app-cache.controller.spec.js +++ b/src/openlmis-app-cache/openlmis-app-cache.controller.spec.js @@ -24,6 +24,7 @@ describe('OpenlmisAppCacheController', function() { this.$rootScope = $injector.get('$rootScope'); this.$controller = $injector.get('$controller'); this.$q = $injector.get('$q'); + this.OPENLMIS_BUILD_DATE = $injector.get('OPENLMIS_BUILD_DATE'); }); this.applicationCacheMock = jasmine.createSpyObj('applicationCache', [ @@ -146,4 +147,47 @@ describe('OpenlmisAppCacheController', function() { }); + // The deprecated window.applicationCache has been removed from modern browsers, + // so $window.applicationCache can be undefined. The controller must not crash. + describe('when applicationCache is not available', function() { + + beforeEach(function() { + this.vm = this.$controller('OpenlmisAppCacheController', { + $window: { + applicationCache: undefined, + location: this.locationMock + } + }); + }); + + it('should not throw on init', function() { + var vm = this.vm; + + expect(function() { + vm.$onInit(); + }).not.toThrow(); + }); + + it('should still set the build date', function() { + this.vm.$onInit(); + + expect(this.vm.buildDate).toBe(this.OPENLMIS_BUILD_DATE); + }); + + it('should set updateReady flag to false', function() { + this.vm.$onInit(); + + expect(this.vm.updateReady).toBe(false); + }); + + it('should do nothing when updateCache is called', function() { + this.vm.$onInit(); + + this.vm.updateCache(); + + expect(this.confirmService.confirm).not.toHaveBeenCalled(); + }); + + }); + });