mercredi 24 février 2016

Unit testing a jquery event

How would one unit test a jQuery event with sinon spys or stubs? For example:

Class

_openClamp() {...}
_closeClamp() {...}

// bind toggle fixture
    $('#accordion').on({
      'show.bs.collapse': function (e) {
        var index = getIndex(e.target.id);
        var $el = $(`a[href="#${e.target.id}"]`);
        this._openClamp(index, $el);
      }.bind(this),
      'hide.bs.collapse': function (e) {
        var index = getIndex(e.target.id);
        var $el = $(`a[href="#${e.target.id}"]`);
        this._closeClamp(index, $el);
      }.bind(this)

Test

it('toggles clamp on button selection', () => {
    var open = sinon.spy(site._openClamp);
    var close = sinon.spy(site._closeClamp);
    var link = site.ui.$learnLinks.first();
    // click on the link
    link.trigger('click');
    expect(open.called).to.be.ok;
    link.trigger('click');
    expect(close.called).to.be.ok;
  });

Aucun commentaire:

Enregistrer un commentaire