From a22e0699bef61a7083b0b628fb6043531c0ca1c0 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Mon, 2 Apr 2012 12:26:57 -0700 Subject: [PATCH] feat($sniffer): add hasEvent method for sniffing events Skip changelog --- src/ng/sniffer.js | 16 +++++++++++++--- test/ng/snifferSpec.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/ng/sniffer.js b/src/ng/sniffer.js index eebb29032ee7..3249b816e7c7 100644 --- a/src/ng/sniffer.js +++ b/src/ng/sniffer.js @@ -12,13 +12,23 @@ * @description * This is very simple implementation of testing browser's features. */ -function $SnifferProvider(){ - this.$get = ['$window', function($window){ +function $SnifferProvider() { + this.$get = ['$window', function($window) { + var eventSupport = {}; + return { history: !!($window.history && $window.history.pushState), hashchange: 'onhashchange' in $window && // IE8 compatible mode lies - (!$window.document.documentMode || $window.document.documentMode > 7) + (!$window.document.documentMode || $window.document.documentMode > 7), + hasEvent: function(event) { + if (isUndefined(eventSupport[event])) { + var divElm = $window.document.createElement('div'); + eventSupport[event] = 'on' + event in divElm; + } + + return eventSupport[event]; + } }; }]; } diff --git a/test/ng/snifferSpec.js b/test/ng/snifferSpec.js index b359e118c420..4e5f50ec8fd9 100644 --- a/test/ng/snifferSpec.js +++ b/test/ng/snifferSpec.js @@ -30,4 +30,43 @@ describe('$sniffer', function() { expect(sniffer({onhashchange: true, document: {documentMode: 7}}).hashchange).toBe(false); }); }); + + + describe('hasEvent', function() { + var mockDocument, mockDivElement, $sniffer; + + beforeEach(function() { + mockDocument = {createElement: jasmine.createSpy('createElement')}; + mockDocument.createElement.andCallFake(function(elm) { + if (elm === 'div') return mockDivElement; + }); + + $sniffer = sniffer({document: mockDocument}); + }); + + + it('should return true if "oninput" is present in a div element', function() { + mockDivElement = {oninput: noop}; + + expect($sniffer.hasEvent('input')).toBe(true); + }); + + + it('should return false if "oninput" is not present in a div element', function() { + mockDivElement = {}; + + expect($sniffer.hasEvent('input')).toBe(false); + }); + + + it('should only create the element once', function() { + mockDivElement = {}; + + $sniffer.hasEvent('input'); + $sniffer.hasEvent('input'); + $sniffer.hasEvent('input'); + + expect(mockDocument.createElement).toHaveBeenCalledOnce(); + }); + }); });