Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Adds isNode check. #277

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/tungsten-event-intent/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env browser */

/**
* Module to handle "intent" delay on events
*/
Expand Down
17 changes: 17 additions & 0 deletions plugins/tungsten-event-mouseenter/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
/* eslint-env browser */

/**
* Module to handle the shimming in delegated mouseenter events
*/
'use strict';

/**
* Check if passed object is a valid DOM Node
* @param {Object} node object to be validated
* @return {Boolean} whether object is a DOM node
*/
function isNode(node) {
return typeof Node === 'object' ? node instanceof Node :
node && typeof node === 'object' && typeof node.nodeType === 'number' &&
typeof node.nodeName === 'string';
}

/**
* Fallback contains check from http://stackoverflow.com/a/6131052
* @param {Element} container Container element
* @param {Element} maybe Element that may be within container
* @return {Boolean} Whether container contains maybe
*/
function contains(container, maybe) {
if (!isNode(container) || !isNode(maybe)) {
return false;
}

// 16 is for a nodeType constant: window.Node.DOCUMENT_POSITION_CONTAINED_BY,
// but the window.Node object is not available in IE8
return container.contains ? container.contains(maybe) :
Expand Down
38 changes: 33 additions & 5 deletions test/plugins/tungsten-event-mouseenter/index_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var mouseenterleaveBind = require('../../../plugins/tungsten-event-mouseenter');
var mouseEnterLeaveBind = require('../../../plugins/tungsten-event-mouseenter');
describe('mouseenter_and_mouseleave_events', function() {
var elem, otherElem, handler, obj;
beforeEach(function() {
Expand All @@ -23,14 +23,28 @@ describe('mouseenter_and_mouseleave_events', function() {
});
};
spyOn(obj, 'bindEventFn').and.callThrough();
mouseenterleaveBind(elem, 'mouseenter', '.js-foo', handler, {}, obj.bindEventFn);
mouseEnterLeaveBind(elem, 'mouseenter', '.js-foo', handler, {}, obj.bindEventFn);
jasmineExpect(obj.bindEventFn).toHaveBeenCalledWith(elem, 'mouseover', '.js-foo', jasmine.any(Function), {});
});
it('should not call bindVirtualEvent when event is not mouseenter event', function() {
var spy = jasmine.createSpy('spy');
mouseenterleaveBind(elem, 'notmouseenter', '.js-foo', handler, {}, spy);
mouseEnterLeaveBind(elem, 'notmouseenter', '.js-foo', handler, {}, spy);
jasmineExpect(spy).not.toHaveBeenCalled();
});
it('should not call handler function when currentTarget is not a DOM node', function() {
obj.bindEventFn = function(el, eventName, selector, method) {
method({
currentTarget: {},
target: elem,
originalEvent: {
relatedTarget: otherElem
}
});
};
handler = jasmine.createSpy('handler');
mouseEnterLeaveBind(elem, 'mouseenter', '.js-foo', handler, {}, obj.bindEventFn);
jasmineExpect(handler).not.toHaveBeenCalled();
});

});

Expand All @@ -46,14 +60,28 @@ describe('mouseenter_and_mouseleave_events', function() {
});
};
spyOn(obj, 'bindEventFn').and.callThrough();
mouseenterleaveBind(elem, 'mouseleave', '.js-foo', handler, {}, obj.bindEventFn);
mouseEnterLeaveBind(elem, 'mouseleave', '.js-foo', handler, {}, obj.bindEventFn);
jasmineExpect(obj.bindEventFn).toHaveBeenCalledWith(elem, 'mouseout', '.js-foo', jasmine.any(Function), {});
});
it('should not call bindVirtualEvent when event is not mouseleave event', function() {
var spy = jasmine.createSpy('spy');
mouseenterleaveBind(elem, 'notmouseleave', '.js-foo', handler, {}, spy);
mouseEnterLeaveBind(elem, 'notmouseleave', '.js-foo', handler, {}, spy);
jasmineExpect(spy).not.toHaveBeenCalled();
});
it('should not call handler function when currentTarget is not a DOM node', function() {
obj.bindEventFn = function(el, eventName, selector, method) {
method({
currentTarget: {},
target: elem,
originalEvent: {
relatedTarget: otherElem
}
});
};
handler = jasmine.createSpy('handler');
mouseEnterLeaveBind(elem, 'mouseleave', '.js-foo', handler, {}, obj.bindEventFn);
jasmineExpect(handler).not.toHaveBeenCalled();
});

});
});