Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine can't trigger click event

I am trying to implement a simple Jasmine test in which Jasmine will test if some code is run on click of a input button. But I can't figure why the click is not triggering? I realize it will if i just have the .click() function in beforeEach but I don't think that's how it's suppose to work.

specs

describe("export citations", function (){
  var btn
  beforeEach(function(){            
    btn= $("input#myButton").eq(0);
  });

  it("should call click function", function() {
    btn.trigger("click");
    expect($("#content").length).toBeGreaterThan(0);
  });
});

fixture

$(function(){
  $("input#myButton").click(function(e){
  //Run a bunch of code here
  }
});
like image 544
Bitneko Avatar asked Oct 08 '22 14:10

Bitneko


1 Answers

Have you actually added an element to the DOM in the fixture? Also you are missing a ); in the fixture to close the click callback.

This worked for me:

describe("export citations", function () {
  var btn;
  beforeEach(function() {
    btn= $("input#myButton").eq(0);
  });

  it("should call click function", function() {
    btn.click();
    expect($("#content").length).toBeGreaterThan(0);
  });
});

fixture

(function() {
  $("body").html("<input id='myButton' type='button'>");

  $("body").html("<div id='content'></div>");

  $("input#myButton").click(function() {
    $("#content").html("<p>Hello</p>");
  });
})();
like image 103
ebaxt Avatar answered Oct 12 '22 20:10

ebaxt