mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat($sniffer): add hasEvent method for sniffing events
Skip changelog
This commit is contained in:
parent
28ff7c3a66
commit
a22e0699be
2 changed files with 52 additions and 3 deletions
|
|
@ -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];
|
||||
}
|
||||
};
|
||||
}];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue