Skip to content

Commit

Permalink
If no context is provided in jwerty.event, default to "this" not "glo…
Browse files Browse the repository at this point in the history
…bal"

Also add tests for context passing through returned jwerty.event function.
  • Loading branch information
keithamus committed Oct 10, 2011
1 parent 1193661 commit 19bac3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jwerty.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
// one in sequence), then fire the callback
} else {
returnValue = callbackFunction.call(
callbackContext || global, event, jwertyCodeIs);
callbackContext || this, event, jwertyCodeIs);

// If the callback returned false, then we should run
// preventDefault();
Expand Down
33 changes: 33 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,39 @@ test("Test jwerty.fire, firing correct events to an eventListener", function ()
jwerty.fire('⌃+shift+F1', this.input);
});

test("Test context passing defaulting to window", function () {
expect(1);

jwerty.key('space', function () {
ok(this.__proto__.constructor.toString().match(/DOMWindow/), 'Expects this to be window');
}, this.input);
buildEvent(32, false, false, false, false, this.input);
});

test("Test context passing when context is set", function () {
expect(1);

jwerty.key('space', function () {
ok(this.myContext, 'Expects this to be set to passed obj');
}, { myContext: true }, this.input);
buildEvent(32, false, false, false, false, this.input);
});

test("Test context passing to bound function context of event function", function () {
expect(1);

var event = jwerty.event('space', function () {
ok(this.myContext, 'Expects this to be set to passed obj');
});

this.input.addEventListener('keydown', function () {
event.apply({ myContext: true }, arguments);
});

buildEvent(32, false, false, false, false, this.input);
});


test("Test key binding without element, binding to `document`", function () {
expect(1);

Expand Down

0 comments on commit 19bac3b

Please sign in to comment.