diff --git a/karma/test-controllers.coffee b/karma/test-controllers.coffee index 350781aa0..a01d11455 100644 --- a/karma/test-controllers.coffee +++ b/karma/test-controllers.coffee @@ -335,9 +335,14 @@ describe('controllers:', () -> # Tests for MessagesController #======================================================================= describe('MessagesController', () -> + $intervalSpy = null beforeEach(() -> - $controller('MessagesController', {$scope: $scope}) + inject((_$interval_) -> + $intervalSpy = jasmine.createSpy('$interval', _$interval_).and.callThrough() + ) + + $controller('MessagesController', {$scope: $scope, $interval: $intervalSpy}) $inboxScope.init('inbox', serverTime) $scope.init() @@ -355,7 +360,7 @@ describe('controllers:', () -> expect($scope.inactiveLabels).toEqual([test.tea, test.coffee]) ) - it('loadOldItems', () -> + it('should loadOldItems and auto-refresh on interval', () -> fetchOld = spyOnPromise($q, $scope, MessageService, 'fetchOld') $scope.loadOldItems() @@ -366,12 +371,19 @@ describe('controllers:', () -> }, $scope.startTime, 1) fetchOld.resolve({results: [test.msg3, test.msg2], hasMore: true}) + + expect($intervalSpy).toHaveBeenCalledWith($scope.autoRefresh, 10000) expect($scope.items).toEqual([test.msg3, test.msg2]) expect($scope.oldItemsMore).toEqual(true) expect($scope.isInfiniteScrollEnabled()).toEqual(true) ) + it 'should cancel the intervals on destroy', -> + spyOn($intervalSpy, 'cancel') + $scope.$destroy() + expect($intervalSpy.cancel).toHaveBeenCalledWith($scope.autoRefresh) + it('getItemFilter', () -> filter = $scope.getItemFilter() expect(filter({archived: false})).toEqual(true) diff --git a/static/coffee/controllers.coffee b/static/coffee/controllers.coffee index f6059c8d0..cab6db19f 100644 --- a/static/coffee/controllers.coffee +++ b/static/coffee/controllers.coffee @@ -216,7 +216,7 @@ controllers.controller('BaseItemsController', ['$scope', 'UtilsService', ($scope #============================================================================ # Incoming messages controller #============================================================================ -controllers.controller('MessagesController', ['$scope', '$timeout', '$uibModal', '$controller', 'CaseService', 'MessageService', 'PartnerService', 'UserService', 'UtilsService', ($scope, $timeout, $uibModal, $controller, CaseService, MessageService, PartnerService, UserService, UtilsService) -> +controllers.controller('MessagesController', ['$scope', '$timeout', '$interval', '$uibModal', '$controller', 'CaseService', 'MessageService', 'PartnerService', 'UserService', 'UtilsService', ($scope, $timeout, $interval, $uibModal, $controller, CaseService, MessageService, PartnerService, UserService, UtilsService) -> $controller('BaseItemsController', {$scope: $scope}) $scope.advancedSearch = false @@ -270,7 +270,18 @@ controllers.controller('MessagesController', ['$scope', '$timeout', '$uibModal', ) $scope.fetchOldItems = (search, startTime, page) -> - return MessageService.fetchOld(search, startTime, page) + $scope.autoRefresh = -> + MessageService.fetchOld(search, startTime, page).then((data) -> + $scope.items = data.results + $scope.oldItemsMore = data.hasMore + $scope.oldItemsLoading = false + ) + $scope.autoRefresh() + $interval.cancel($scope.autoRefresh) + $interval($scope.autoRefresh, 10000) + + $scope.$on '$destroy', -> + $interval.cancel($scope.autoRefresh) $scope.onExpandMessage = (message) -> $scope.expandedMessageId = message.id