Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat($sniffer): add hasEvent method for sniffing events
Browse files Browse the repository at this point in the history
Skip changelog
  • Loading branch information
vojtajina committed Apr 3, 2012
1 parent 28ff7c3 commit a22e069
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/ng/sniffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
};
}];
}
39 changes: 39 additions & 0 deletions test/ng/snifferSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit a22e069

Please sign in to comment.