Cover DomEvent with unit tests (#8088)

* Added Additional Events

- dblclick, mousedown, mouseup, mouseover, mouseout and mousemove added to unit testing for DomEvent On function

* Added On and Off DomEvents Tests

- Added more types for DomEvent On
- Added tests for dealing with multiple events and listeners for DomEvent On
- Added tests for DomEvent Off

* Cleaned up .only

* Ran Lint Fix

Apologies, should have ran that earlier
This commit is contained in:
stephenspol
2022-03-27 04:26:38 -04:00
committed by GitHub
parent 918b65f1cc
commit 4e6604c70f

View File

@ -26,22 +26,207 @@ describe('DomEvent', function () {
});
describe('#on (addListener)', function () {
it('throws when types/fn are undefined/null/false', function () {
it('throws when type is undefined and context is falseFn', function () {
expect(L.DomEvent.on).withArgs(el, undefined, L.Util.falseFn)
.to.throwException();
});
it('throws when type is null and context is falseFn', function () {
expect(L.DomEvent.on).withArgs(el, null, L.Util.falseFn)
.to.throwException();
});
it('throws when type is false and context is falseFn', function () {
expect(L.DomEvent.on).withArgs(el, false, L.Util.falseFn)
.to.throwException();
});
it('throws when type is "click" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'click', undefined)
.to.throwException();
});
it('throws when type is "click" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'click', null)
.to.throwException();
});
it('throws when type is "click" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'click', false)
.to.throwException();
});
it('adds a listener and calls it on event', function () {
it('throws when type is "click" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'click', undefined)
.to.throwException();
});
it('throws when type is "dblclick" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'dblclick', null)
.to.throwException();
});
it('throws when type is "dblclick" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'dblclick', false)
.to.throwException();
});
it('throws when type is "dblclick" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'dblclick', undefined)
.to.throwException();
});
it('throws when type is "mousedown" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'mousedown', null)
.to.throwException();
});
it('throws when type is "mousedown" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'mousedown', false)
.to.throwException();
});
it('throws when type is "mousedown" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'mousedown', undefined)
.to.throwException();
});
it('throws when type is "mouseup" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseup', null)
.to.throwException();
});
it('throws when type is "mouseup" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseup', false)
.to.throwException();
});
it('throws when type is "mouseup" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseup', undefined)
.to.throwException();
});
it('throws when type is "mouseover" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseover', null)
.to.throwException();
});
it('throws when type is "mouseover" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseover', false)
.to.throwException();
});
it('throws when type is "mouseover" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseover', undefined)
.to.throwException();
});
it('throws when type is "mouseout" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseout', null)
.to.throwException();
});
it('throws when type is "mouseout" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseout', false)
.to.throwException();
});
it('throws when type is "mouseout" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'mouseout', undefined)
.to.throwException();
});
it('throws when type is "mousemove" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'mousemove', null)
.to.throwException();
});
it('throws when type is "mousemove" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'mousemove', false)
.to.throwException();
});
it('throws when type is "mousemove" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'mousemove', undefined)
.to.throwException();
});
it('throws when type is "contextmenu" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'contextmenu', null)
.to.throwException();
});
it('throws when type is "contextmenu" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'contextmenu', false)
.to.throwException();
});
it('throws when type is "contextmenu" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'contextmenu', undefined)
.to.throwException();
});
it('throws when type is "keyup" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'keyup', null)
.to.throwException();
});
it('throws when type is "keyup" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'keyup', false)
.to.throwException();
});
it('throws when type is "keyup" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'keyup', undefined)
.to.throwException();
});
it('throws when type is "keypress" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'keypress', null)
.to.throwException();
});
it('throws when type is "keypress" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'keypress', false)
.to.throwException();
});
it('throws when type is "keypress" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'keypress', undefined)
.to.throwException();
});
it('throws when type is "keydown" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'keydown', null)
.to.throwException();
});
it('throws when type is "keydown" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'keydown', false)
.to.throwException();
});
it('throws when type is "keydown" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'keydown', undefined)
.to.throwException();
});
it('throws when type is "keydown and click" and context is null', function () {
expect(L.DomEvent.on).withArgs(el, 'keydown click', null)
.to.throwException();
});
it('throws when type is "keydown and click" and context is false', function () {
expect(L.DomEvent.on).withArgs(el, 'keydown click', false)
.to.throwException();
});
it('throws when type is "keydown and click" and context is undefined', function () {
expect(L.DomEvent.on).withArgs(el, 'keydown click', undefined)
.to.throwException();
});
it('adds a listener and calls it on event with click', function () {
var listener2 = sinon.spy();
L.DomEvent.on(el, 'click', listener);
L.DomEvent.on(el, 'click', listener2);
@ -52,6 +237,18 @@ describe('DomEvent', function () {
expect(listener2.called).to.be.ok();
});
it('adds a listener and calls it on event with click and keypress', function () {
var listener2 = sinon.spy();
L.DomEvent.on(el, 'click keypress', listener);
L.DomEvent.on(el, 'click', listener2);
happen.click(el);
happen.keypress(el);
expect(listener.called).to.be.ok();
expect(listener2.called).to.be.ok();
});
it('adds a listener when passed an event map', function () {
var listener = sinon.spy();
@ -62,6 +259,18 @@ describe('DomEvent', function () {
sinon.assert.called(listener);
});
it('adds 2 listener when passed an event map with multiple events', function () {
var listener2 = sinon.spy();
L.DomEvent.on(el, {click: listener, keypress: listener2});
happen.click(el);
happen.keypress(el);
sinon.assert.called(listener);
sinon.assert.called(listener2);
});
it('binds "this" to the given context', function () {
var obj = {foo: 'bar'};
L.DomEvent.on(el, 'click', listener, obj);
@ -71,6 +280,16 @@ describe('DomEvent', function () {
expect(listener.calledOn(obj)).to.be.ok();
});
it('binds "this" to the given context with multiple types', function () {
var obj = {foo: 'bar'};
L.DomEvent.on(el, 'click keypress', listener, obj);
happen.click(el);
happen.keypress(el);
expect(listener.calledOn(obj)).to.be.ok();
});
it('binds "this" to the given context when passed an event map', function () {
var listener = sinon.spy(),
ctx = {foo: 'bar'};
@ -82,6 +301,19 @@ describe('DomEvent', function () {
sinon.assert.calledOn(listener, ctx);
});
it('binds "this" to the given context when passed an event map with multiple events', function () {
var listener2 = sinon.spy(),
ctx = {foo: 'bar'};
L.DomEvent.on(el, {click: listener, keypress: listener2}, ctx);
happen.click(el);
happen.keypress(el);
sinon.assert.calledOn(listener, ctx);
sinon.assert.calledOn(listener2, ctx);
});
it('passes an event object to the listener', function () {
L.DomEvent.on(el, 'click', listener);
@ -90,6 +322,16 @@ describe('DomEvent', function () {
expect(listener.lastCall.args[0].type).to.eql('click');
});
it('passes two event objects to the listener', function () {
L.DomEvent.on(el, 'click keypress', listener);
happen.click(el);
happen.keypress(el);
expect(listener.firstCall.args[0].type).to.eql('click');
expect(listener.secondCall.args[0].type).to.eql('keypress');
});
it('is chainable', function () {
var res = L.DomEvent.on(el, 'click', function () {});
@ -111,6 +353,16 @@ describe('DomEvent', function () {
expect(listener.notCalled).to.be.ok();
});
it('removes a previously added event', function () {
L.DomEvent.on(el, 'click keypress', listener);
L.DomEvent.off(el, 'click', listener);
happen.click(el);
happen.keypress(el);
expect(listener.lastCall.args[0].type).to.eql('keypress');
});
it('only removes the specified listener', function () {
var listenerA = sinon.spy(),
listenerB = sinon.spy();
@ -137,6 +389,35 @@ describe('DomEvent', function () {
sinon.assert.notCalled(listener);
});
it('removes a previously added listener when passed an event map with multiple events', function () {
var listener2 = sinon.spy(),
events = {click: listener, keypress: listener2};
L.DomEvent.on(el, events);
L.DomEvent.off(el, events);
happen.click(el);
happen.keypress(el);
sinon.assert.notCalled(listener);
sinon.assert.notCalled(listener2);
});
it('removes a previously added event when passed an event map with multiple events', function () {
var listener2 = sinon.spy(),
events = {click: listener, keypress: listener2},
events2 = {click: listener};
L.DomEvent.on(el, events);
L.DomEvent.off(el, events2);
happen.click(el);
happen.keypress(el);
sinon.assert.notCalled(listener);
expect(listener2.lastCall.args[0].type).to.eql('keypress');
});
it('removes listener added with context', function () {
var listener = sinon.spy(),
ctx = {foo: 'bar'};
@ -162,6 +443,22 @@ describe('DomEvent', function () {
sinon.assert.notCalled(listener);
});
it('removes listener added with context when passed an event map with multiple events', function () {
var listener2 = sinon.spy(),
events = {click: listener, keypress: listener2},
events2 = {click: listener},
ctx = {foo: 'bar'};
L.DomEvent.on(el, events, ctx);
L.DomEvent.off(el, events2, ctx);
happen.click(el);
happen.keypress(el);
sinon.assert.notCalled(listener);
expect(listener2.lastCall.args[0].type).to.eql('keypress');
});
it('only removes listener when proper context specified', function () {
var listener = sinon.spy(),
ctx = {foo: 'bar'};