describe('Ionic Tap', function() { beforeEach(function() { window.console.debug = function(){}; ionic.tap.reset(); window._setTimeout = window.setTimeout; window.setTimeout = function(){}; }); afterEach(function(){ window.setTimeout = window._setTimeout; }); it('Should focus on an input if it hasnt scrolled', function() { var targetEle = { dispatchEvent: function() {}, focus: function() { this.isFocused = true; } }; ionic.tap.setTouchStart({clientX: 100, clientY: 100}); targetEle.tagName = 'INPUT'; var e = { clientX: 100, clientY: 100, preventDefault: function() {} }; ionic.tap.simulateClick(targetEle, e); expect(targetEle.isFocused).toEqual(true); }); it('Should setTouchStart and hasTouchScrolled true if >= touch tolerance', function() { ionic.tap.setTouchStart({ clientX: 100, clientY: 100 }); var s = ionic.tap.hasTouchScrolled({ clientX: 111, clientY: 100 }); expect(s).toEqual(true); s = ionic.tap.hasTouchScrolled({ clientX: 89, clientY: 100 }); expect(s).toEqual(true); s = ionic.tap.hasTouchScrolled({ clientX: 100, clientY: 107 }); expect(s).toEqual(true); s = ionic.tap.hasTouchScrolled({ clientX: 100, clientY: 93 }); expect(s).toEqual(true); s = ionic.tap.hasTouchScrolled({ clientX: 100, clientY: 200 }); expect(s).toEqual(true); }); it('Should setTouchStart and hasTouchScrolled false if less than touch tolerance', function() { ionic.tap.setTouchStart({ clientX: 100, clientY: 100 }); var s = ionic.tap.hasTouchScrolled({ clientX: 100, clientY: 100 }); expect(s).toEqual(false); s = ionic.tap.hasTouchScrolled({ clientX: 104, clientY: 100 }); expect(s).toEqual(false); s = ionic.tap.hasTouchScrolled({ clientX: 96, clientY: 100 }); expect(s).toEqual(false); s = ionic.tap.hasTouchScrolled({ clientX: 100, clientY: 102 }); expect(s).toEqual(false); s = ionic.tap.hasTouchScrolled({ clientX: 100, clientY: 98 }); expect(s).toEqual(false); }); it('Should not be hasTouchScrolled if 0 coordinates', function() { var s = ionic.tap.hasTouchScrolled({ clientX: 0, clientY: 0 }); expect(s).toEqual(false); }); it('Should dispatch a mouse event', function() { var targetEle = { dispatchEvent: function(clickEvent) { this.clickEvent = clickEvent; }, tagName: 'LABEL' }; var e = { clientX: 99, clientY: 88 }; ionic.tap.simulateClick(targetEle, e); expect(targetEle.clickEvent.clientX).toEqual(99); expect(targetEle.clickEvent.clientY).toEqual(88); }); it('Should get coordinates from client originalEvent changedTouches', function() { var e = { clientX: 99, clientY: 99, originalEvent: { changedTouches: [{ clientX: 77, clientY: 77 }] } }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:77, y: 77}); }); it('Should get coordinates from client mouse originalEvent', function() { var e = { clientX: 99, clientY: 99, originalEvent: { clientX: 88, clientY: 88, } }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:88, y: 88}); }); it('Should get coordinates from page mouse event', function() { var e = { pageX: 77, pageY: 77 }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:77, y: 77}); }); it('Should get coordinates from client mouse event', function() { var e = { clientX: 77, clientY: 77 }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:77, y: 77}); }); it('Should get coordinates from changedTouches touches', function() { var e = { touches: [{ clientX: 99, clientY: 99 }], changedTouches: [{ clientX: 88, clientY: 88 }] }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:88, y: 88}); }); it('Should get coordinates from page touches', function() { var e = { touches: [{ pageX: 99, pageY: 99 }] }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:99, y: 99}); }); it('Should get coordinates from client touches', function() { var e = { touches: [{ clientX: 99, clientY: 99 }] }; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:99, y: 99}); }); it('Should get 0 coordinates', function() { var e = {}; var c = ionic.tap.getCoordinates(e); expect(c).toEqual({x:0, y: 0}); }); it('Should ignoreSimulateClick for disabled elements', function() { // Disabled elements should not be tapped var targetEle = document.createElement('input'); targetEle.disabled = true; expect( ionic.tap.ignoreSimulateClick(targetEle) ).toEqual(true); }); it('Should ignoreSimulateClick for input[range] elements', function() { // Range and tap do not agree, probably because it doesn't have a delay to begin with var targetEle = document.createElement('input'); targetEle.type = 'range'; expect( ionic.tap.ignoreSimulateClick(targetEle) ).toEqual(true); }); it('Should not ignoreSimulateClick for common inputs', function() { var inputTypes = ['text', 'email', 'search', 'tel', 'number', 'date', 'month', 'password', null, undefined, '']; for(var x=0; x