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

Commit a22e069

Browse files
committed
feat($sniffer): add hasEvent method for sniffing events
Skip changelog
1 parent 28ff7c3 commit a22e069

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/ng/sniffer.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@
1212
* @description
1313
* This is very simple implementation of testing browser's features.
1414
*/
15-
function $SnifferProvider(){
16-
this.$get = ['$window', function($window){
15+
function $SnifferProvider() {
16+
this.$get = ['$window', function($window) {
17+
var eventSupport = {};
18+
1719
return {
1820
history: !!($window.history && $window.history.pushState),
1921
hashchange: 'onhashchange' in $window &&
2022
// IE8 compatible mode lies
21-
(!$window.document.documentMode || $window.document.documentMode > 7)
23+
(!$window.document.documentMode || $window.document.documentMode > 7),
24+
hasEvent: function(event) {
25+
if (isUndefined(eventSupport[event])) {
26+
var divElm = $window.document.createElement('div');
27+
eventSupport[event] = 'on' + event in divElm;
28+
}
29+
30+
return eventSupport[event];
31+
}
2232
};
2333
}];
2434
}

test/ng/snifferSpec.js

+39
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,43 @@ describe('$sniffer', function() {
3030
expect(sniffer({onhashchange: true, document: {documentMode: 7}}).hashchange).toBe(false);
3131
});
3232
});
33+
34+
35+
describe('hasEvent', function() {
36+
var mockDocument, mockDivElement, $sniffer;
37+
38+
beforeEach(function() {
39+
mockDocument = {createElement: jasmine.createSpy('createElement')};
40+
mockDocument.createElement.andCallFake(function(elm) {
41+
if (elm === 'div') return mockDivElement;
42+
});
43+
44+
$sniffer = sniffer({document: mockDocument});
45+
});
46+
47+
48+
it('should return true if "oninput" is present in a div element', function() {
49+
mockDivElement = {oninput: noop};
50+
51+
expect($sniffer.hasEvent('input')).toBe(true);
52+
});
53+
54+
55+
it('should return false if "oninput" is not present in a div element', function() {
56+
mockDivElement = {};
57+
58+
expect($sniffer.hasEvent('input')).toBe(false);
59+
});
60+
61+
62+
it('should only create the element once', function() {
63+
mockDivElement = {};
64+
65+
$sniffer.hasEvent('input');
66+
$sniffer.hasEvent('input');
67+
$sniffer.hasEvent('input');
68+
69+
expect(mockDocument.createElement).toHaveBeenCalledOnce();
70+
});
71+
});
3372
});

0 commit comments

Comments
 (0)