From 8248e77a7b910bcbc71ca25c06bef44dd6712990 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Thu, 30 Sep 2010 23:07:36 +0800 Subject: 'A' tag widget and ng:click propagation change * added a widget for A (anchor) tag, that modifies the default behavior and prevent default action (location change and page reload) for tags with empty href attribute * stopped event propagation for all ng:click handlers --- test/directivesSpec.js | 13 +++++++++++++ test/widgetsSpec.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'test') diff --git a/test/directivesSpec.js b/test/directivesSpec.js index f0eb5c09..72f7b2f2 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -169,6 +169,19 @@ describe("directives", function(){ element.trigger('click'); expect(scope.$get('clicked')).toEqual(true); }); + + it('should stop event propagation', function() { + var scope = compile('
'); + scope.$eval(); + expect(scope.$get('outer')).not.toBeDefined(); + expect(scope.$get('inner')).not.toBeDefined(); + + var innerDiv = jqLite(element.children()[0]); + + innerDiv.trigger('click'); + expect(scope.$get('outer')).not.toBeDefined(); + expect(scope.$get('inner')).toEqual(true); + }) }); it('should ng:class', function(){ diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index d113e6ee..edaa5061 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -498,5 +498,40 @@ describe("widget", function(){ expect(element.text()).toEqual(''); }); }); + + describe('a', function() { + it('should prevent default action to be executed when href is empty', function() { + var orgLocation = document.location.href, + preventDefaultCalled = false, + event; + + compile('empty link'); + + if (msie) { + + event = document.createEventObject(); + expect(event.returnValue).not.toBeDefined(); + element[0].fireEvent('onclick', event); + expect(event.returnValue).toEqual(false); + + } else { + + event = document.createEvent('MouseEvent'); + event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, _null); + + event.preventDefaultOrg = event.preventDefault; + event.preventDefault = function() { + preventDefaultCalled = true; + if (this.preventDefaultOrg) this.preventDefaultOrg(); + }; + + element[0].dispatchEvent(event); + + expect(preventDefaultCalled).toEqual(true); + } + + expect(document.location.href).toEqual(orgLocation); + }); + }) }); -- cgit v1.2.3