From b5aefa8291960b30d8d1528358c402d16a3e124c Mon Sep 17 00:00:00 2001 From: Ulrich Sossou Date: Sat, 23 Nov 2013 13:36:50 +0100 Subject: [PATCH] ionicList directives cleanup --- dist/js/ionic-angular.js | 12 +- js/ext/angular/src/directive/ionicList.js | 12 +- .../angular/test/directive/ionicList.unit.js | 108 +++++++++++++++++- 3 files changed, 127 insertions(+), 5 deletions(-) diff --git a/dist/js/ionic-angular.js b/dist/js/ionic-angular.js index 586ff6f444..c8dd25c9d9 100644 --- a/dist/js/ionic-angular.js +++ b/dist/js/ionic-angular.js @@ -24750,7 +24750,7 @@ angular.module('ionic.ui.list', ['ngAnimate']) button.onButtonClicked && button.onButtonClicked($scope.item, button); }; - list.scope.$watch('isEditing', function(v) { + var deregisterListWatch = list.scope.$watch('isEditing', function(v) { $scope.isEditing = v; // Add a delay before we allow the options layer to show, to avoid any odd @@ -24763,6 +24763,10 @@ angular.module('ionic.ui.list', ['ngAnimate']) $scope.showOptions = false; } }); + + $scope.$on('$destroy', function () { + deregisterListWatch(); + }); } }; }]) @@ -24821,7 +24825,7 @@ angular.module('ionic.ui.list', ['ngAnimate']) button.onButtonClicked && button.onButtonClicked($scope.item, button); }; - list.scope.$watch('isEditing', function(v) { + var deregisterListWatch = list.scope.$watch('isEditing', function(v) { $scope.isEditing = v; // Add a delay before we allow the options layer to show, to avoid any odd @@ -24834,6 +24838,10 @@ angular.module('ionic.ui.list', ['ngAnimate']) $scope.showOptions = false; } }); + + $scope.$on('$destroy', function () { + deregisterListWatch(); + }); } }; }]) diff --git a/js/ext/angular/src/directive/ionicList.js b/js/ext/angular/src/directive/ionicList.js index 9736d2d851..a5fa63be98 100644 --- a/js/ext/angular/src/directive/ionicList.js +++ b/js/ext/angular/src/directive/ionicList.js @@ -62,7 +62,7 @@ angular.module('ionic.ui.list', ['ngAnimate']) button.onButtonClicked && button.onButtonClicked($scope.item, button); }; - list.scope.$watch('isEditing', function(v) { + var deregisterListWatch = list.scope.$watch('isEditing', function(v) { $scope.isEditing = v; // Add a delay before we allow the options layer to show, to avoid any odd @@ -75,6 +75,10 @@ angular.module('ionic.ui.list', ['ngAnimate']) $scope.showOptions = false; } }); + + $scope.$on('$destroy', function () { + deregisterListWatch(); + }); } }; }]) @@ -133,7 +137,7 @@ angular.module('ionic.ui.list', ['ngAnimate']) button.onButtonClicked && button.onButtonClicked($scope.item, button); }; - list.scope.$watch('isEditing', function(v) { + var deregisterListWatch = list.scope.$watch('isEditing', function(v) { $scope.isEditing = v; // Add a delay before we allow the options layer to show, to avoid any odd @@ -146,6 +150,10 @@ angular.module('ionic.ui.list', ['ngAnimate']) $scope.showOptions = false; } }); + + $scope.$on('$destroy', function () { + deregisterListWatch(); + }); } }; }]) diff --git a/js/ext/angular/test/directive/ionicList.unit.js b/js/ext/angular/test/directive/ionicList.unit.js index d43b41062a..08525c39e1 100644 --- a/js/ext/angular/test/directive/ionicList.unit.js +++ b/js/ext/angular/test/directive/ionicList.unit.js @@ -1,3 +1,5 @@ +'use strict'; + describe('Ionic List', function() { var compile, scope; @@ -9,7 +11,7 @@ describe('Ionic List', function() { })); it('Should init', function() { - element = compile('' + + var element = compile('' + '' + '' + '')(scope); @@ -17,3 +19,107 @@ describe('Ionic List', function() { expect(element.children().length).toBe(2); }); }); + +describe('Ionic Link Item Directive', function () { + var $rootScope, element, listCtrl, options, scope; + + beforeEach(module('ionic.ui.list')); + + beforeEach(inject(function (_$compile_, _$rootScope_) { + $rootScope = _$rootScope_; + $rootScope.isEditing = false; + + var list = angular.element(''); + list = _$compile_(list)($rootScope); + + listCtrl = list.controller('list'); + + $rootScope.buttons = []; + + element = angular.element('').appendTo(list); + element = _$compile_(element)($rootScope); + + $rootScope.$digest(); + scope = element.isolateScope(); + })); + + it('Should show options when the list is not in edit mode', inject(function ($timeout) { + scope.canSwipe = true; + $rootScope.$digest(); + $timeout.flush(); + + expect(scope.isEditing).toBe(false); + expect(element.find('.item-options').length).toBe(1); + })); + + it('Should hide options when the list is in edit mode', inject(function ($timeout) { + scope.canSwipe = true; + $rootScope.isEditing = true; + $rootScope.$digest(); + $timeout.flush(); + + expect(scope.isEditing).toBe(true); + expect(element.find('.item-options').length).toBe(0); + })); + + it('Should deregister watcher when scope destroyed', inject(function ($timeout) { + $rootScope.isEditing = true; + scope.$destroy(); + $rootScope.$digest(); + $timeout.flush(); + + expect(scope.isEditing).toBe(false); + })); +}); + +describe('Ionic Item Directive', function () { + var $rootScope, element, listCtrl, options, scope; + + beforeEach(module('ionic.ui.list')); + + beforeEach(inject(function (_$compile_, _$rootScope_) { + $rootScope = _$rootScope_; + $rootScope.isEditing = false; + + var list = angular.element(''); + list = _$compile_(list)($rootScope); + + listCtrl = list.controller('list'); + + $rootScope.buttons = []; + + element = angular.element('').appendTo(list); + element = _$compile_(element)($rootScope); + + $rootScope.$digest(); + scope = element.isolateScope(); + })); + + it('Should show options when the list is not in edit mode', inject(function ($timeout) { + scope.canSwipe = true; + $rootScope.$digest(); + $timeout.flush(); + + expect(scope.isEditing).toBe(false); + expect(element.find('.item-options').length).toBe(1); + })); + + it('Should hide options when the list is in edit mode', inject(function ($timeout) { + scope.canSwipe = true; + $rootScope.isEditing = true; + $rootScope.$digest(); + $timeout.flush(); + + expect(scope.isEditing).toBe(true); + expect(element.find('.item-options').length).toBe(0); + })); + + it('Should deregister watcher when scope destroyed', inject(function ($timeout) { + $rootScope.isEditing = true; + scope.$destroy(); + $rootScope.$digest(); + $timeout.flush(); + + expect(scope.isEditing).toBe(false); + })); +});