Skip to content

Commit

Permalink
Delegate events tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ediblecode committed Sep 26, 2016
1 parent dd1b755 commit 0fdff16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/javascripts/delegate-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ var delegateEventSplitter = /^(\S+)\s*(.*)$/;
* @return {Object} The instance
*/
export default function delegateEvents(instance) {
if(!instance) {
$.error("Instance must be non-null");
return;
}
if(!instance.events || typeof instance.events !== "function") {
$.error("Instance must be non-null");
return;
}

var events = instance.events();

Expand Down
24 changes: 12 additions & 12 deletions test/unit/delegate-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ const delegateEvents = require("../../src/javascripts/delegate-events.js").defau

describe("Delegate events", function() {

it("throws when function not found", function() {

var spy = sinon.spy($, "error");
it("throws when no instance", function() {
(() => {
delegateEvents();
}).should.throw();
});

it("throws when function not found", function() {
class Test {
constructor() {
delegateEvents(this);
Expand All @@ -21,13 +24,11 @@ describe("Delegate events", function() {
}

(() => {
var test = new Test();
new Test();
}).should.throw();

spy.should.be.calledOnce;
});

it("should call function", function() {
it("should call function with context", function() {

var spy = sinon.spy();

Expand All @@ -46,10 +47,10 @@ describe("Delegate events", function() {
var test = new Test();
test.$el.find(".inner").trigger("click");

spy.should.be.calledOnce;
spy.should.be.calledOnce.and.be.calledOn(test);
});

it("should call method", function() {
it("should call named instance method with context", function() {

class Test {
constructor() {
Expand All @@ -61,16 +62,15 @@ describe("Delegate events", function() {
"click .inner": "_handler"
};
}
_handler() {
}
_handler() { }
}

var spy = sinon.spy(Test.prototype, "_handler");

var test = new Test();
test.$el.find(".inner").trigger("click");

spy.should.be.calledOnce;
spy.should.be.calledOnce.and.be.calledOn(test);
});

});

0 comments on commit 0fdff16

Please sign in to comment.