Skip to content

Commit

Permalink
Cover DomEvent with unit tests (Leaflet#8088)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stephenspol authored Mar 27, 2022
1 parent 918b65f commit 4e6604c
Showing 1 changed file with 299 additions and 2 deletions.
301 changes: 299 additions & 2 deletions spec/suites/dom/DomEventSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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'};
Expand All @@ -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);

Expand All @@ -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 () {});

Expand All @@ -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();
Expand All @@ -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'};
Expand All @@ -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'};
Expand Down

0 comments on commit 4e6604c

Please sign in to comment.