Skip to content

Commit

Permalink
Unbind events on clean
Browse files Browse the repository at this point in the history
  • Loading branch information
viddo committed May 26, 2015
1 parent 8608cd9 commit bbbd7ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ui/common/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ cdb.ui.common.Dropdown = cdb.core.View.extend({
this.isOpen = true;
},

clean: function() {
$(this.options.target).unbind({"click": this._handleClick});
$(document).unbind('keydown', this._keydown);
cdb.core.View.prototype.clean.apply(this, arguments);
},

_fireClick: function(ev) {
this.trigger("optionClicked", ev, this.el);
}
Expand Down
2 changes: 2 additions & 0 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@

<script src="spec/ui/common/dialog.spec.js"></script>

<script src="spec/ui/common/dropdown.spec.js"></script>

<script src="spec/ui/common/notification.spec.js"></script>

<script src="spec/ui/common/table.spec.js"></script>
Expand Down
44 changes: 44 additions & 0 deletions test/spec/ui/common/dropdown.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
describe('common.ui.Dropdown', function() {
beforeEach(function() {
this.$el = $('<div><button id="btn"></button></div>');
this.view = new cdb.ui.common.Dropdown({
el: $('<div>'),
target: this.$el.find('#btn')
});
});

describe('.clean', function() {
it('should unbind click handler on target', function() {
this.targetClickSpy = jasmine.createSpy('click');
this.$el.on('click', this.targetClickSpy);

// Event should not bubble up since there is a handler that prevents it
this.$el.find('#btn').click();
expect(this.targetClickSpy).not.toHaveBeenCalled();

// Verify click bubbles up as expected again
this.view.clean();
this.$el.find('#btn').click();
expect(this.targetClickSpy).toHaveBeenCalled();
});

it('should unbind event handlers on document', function() {
// spy on internal call, since spying on _keydown fn do not work for some reason
spyOn(this.view, 'hide');
var keyEsc = function() {
var e = $.Event('keydown');
e.keyCode = 27; // ESC
$(document).trigger(e);
};

// Should hide on ESC
keyEsc();
expect(this.view.hide).toHaveBeenCalled();

// Callback should not be triggered again
this.view.clean();
keyEsc();
expect(this.view.hide.calls.count()).toEqual(1);
});
});
});

0 comments on commit bbbd7ad

Please sign in to comment.