feat($sniffer): add hasEvent method for sniffing events

Skip changelog
This commit is contained in:
Vojta Jina 2012-04-02 12:26:57 -07:00
parent 28ff7c3a66
commit a22e0699be
2 changed files with 52 additions and 3 deletions

View file

@ -12,13 +12,23 @@
* @description * @description
* This is very simple implementation of testing browser's features. * This is very simple implementation of testing browser's features.
*/ */
function $SnifferProvider(){ function $SnifferProvider() {
this.$get = ['$window', function($window){ this.$get = ['$window', function($window) {
var eventSupport = {};
return { return {
history: !!($window.history && $window.history.pushState), history: !!($window.history && $window.history.pushState),
hashchange: 'onhashchange' in $window && hashchange: 'onhashchange' in $window &&
// IE8 compatible mode lies // 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];
}
}; };
}]; }];
} }

View file

@ -30,4 +30,43 @@ describe('$sniffer', function() {
expect(sniffer({onhashchange: true, document: {documentMode: 7}}).hashchange).toBe(false); 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();
});
});
}); });