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
  }
});
                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>");
  });
})();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With