diff options
| author | Braden Shepherdson | 2013-03-08 09:38:29 -0500 |
|---|---|---|
| committer | Igor Minar | 2013-04-11 13:01:24 -0700 |
| commit | 5e0f876c39099adb6a0300c429b8df1f6b544846 (patch) | |
| tree | 3c5fc1b7b55f466e232e4b3a2c0d81e8bc40313c /test/ngMobile/directive/ngSwipeSpec.js | |
| parent | f24cf4b58f3c2e56df188738356b21e6c898ff0c (diff) | |
| download | angular.js-5e0f876c39099adb6a0300c429b8df1f6b544846.tar.bz2 | |
feat(ngSwipe): Add ngSwipeRight/Left directives to ngMobile
These directives fire an event handler on a touch-and-drag or
click-and-drag to the left or right. Includes unit tests and docs
update. Manually tested on Chrome 26, IE8, Android Chrome and iOS
Safari.
Diffstat (limited to 'test/ngMobile/directive/ngSwipeSpec.js')
| -rw-r--r-- | test/ngMobile/directive/ngSwipeSpec.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/test/ngMobile/directive/ngSwipeSpec.js b/test/ngMobile/directive/ngSwipeSpec.js new file mode 100644 index 00000000..6bc7d300 --- /dev/null +++ b/test/ngMobile/directive/ngSwipeSpec.js @@ -0,0 +1,110 @@ +'use strict'; + +// Wrapper to abstract over using touch events or mouse events. +var swipeTests = function(description, restrictBrowsers, startEvent, moveEvent, endEvent) { + describe('ngSwipe with ' + description + ' events', function() { + var element; + + if (restrictBrowsers) { + // TODO(braden): Once we have other touch-friendly browsers on CI, allow them here. + // Currently Firefox and IE refuse to fire touch events. + var chrome = /chrome/.test(navigator.userAgent.toLowerCase()); + if (!chrome) { + return; + } + } + + // Skip tests on IE < 9. These versions of IE don't support createEvent(), and so + // we cannot control the (x,y) position of events. + // It works fine in IE 8 under manual testing. + var msie = +((/msie (\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]); + if (msie < 9) { + return; + } + + beforeEach(function() { + module('ngMobile'); + }); + + afterEach(function() { + dealoc(element); + }); + + it('should swipe to the left', inject(function($rootScope, $compile) { + element = $compile('<div ng-swipe-left="swiped = true"></div>')($rootScope); + $rootScope.$digest(); + expect($rootScope.swiped).toBeUndefined(); + + browserTrigger(element, startEvent, [], 100, 20); + browserTrigger(element, endEvent, [], 20, 20); + expect($rootScope.swiped).toBe(true); + })); + + it('should swipe to the right', inject(function($rootScope, $compile) { + element = $compile('<div ng-swipe-right="swiped = true"></div>')($rootScope); + $rootScope.$digest(); + expect($rootScope.swiped).toBeUndefined(); + + browserTrigger(element, startEvent, [], 20, 20); + browserTrigger(element, endEvent, [], 90, 20); + expect($rootScope.swiped).toBe(true); + })); + + it('should not swipe if you move too far vertically', inject(function($rootScope, $compile, $rootElement) { + element = $compile('<div ng-swipe-left="swiped = true"></div>')($rootScope); + $rootElement.append(element); + $rootScope.$digest(); + + expect($rootScope.swiped).toBeUndefined(); + + browserTrigger(element, startEvent, [], 90, 20); + browserTrigger(element, moveEvent, [], 70, 200); + browserTrigger(element, endEvent, [], 20, 20); + + expect($rootScope.swiped).toBeUndefined(); + })); + + it('should not swipe if you slide only a short distance', inject(function($rootScope, $compile, $rootElement) { + element = $compile('<div ng-swipe-left="swiped = true"></div>')($rootScope); + $rootElement.append(element); + $rootScope.$digest(); + + expect($rootScope.swiped).toBeUndefined(); + + browserTrigger(element, startEvent, [], 90, 20); + browserTrigger(element, endEvent, [], 80, 20); + + expect($rootScope.swiped).toBeUndefined(); + })); + + it('should not swipe if the swipe leaves the element', inject(function($rootScope, $compile, $rootElement) { + element = $compile('<div ng-swipe-right="swiped = true"></div>')($rootScope); + $rootElement.append(element); + $rootScope.$digest(); + + expect($rootScope.swiped).toBeUndefined(); + + browserTrigger(element, startEvent, [], 20, 20); + browserTrigger(element, moveEvent, [], 40, 20); + + expect($rootScope.swiped).toBeUndefined(); + })); + + it('should not swipe if the swipe starts outside the element', inject(function($rootScope, $compile, $rootElement) { + element = $compile('<div ng-swipe-right="swiped = true"></div>')($rootScope); + $rootElement.append(element); + $rootScope.$digest(); + + expect($rootScope.swiped).toBeUndefined(); + + browserTrigger(element, moveEvent, [], 10, 20); + browserTrigger(element, endEvent, [], 90, 20); + + expect($rootScope.swiped).toBeUndefined(); + })); + }); +} + +swipeTests('touch', true /* restrictBrowers */, 'touchstart', 'touchmove', 'touchend'); +swipeTests('mouse', false /* restrictBrowers */, 'mousedown', 'mousemove', 'mouseup'); + |
