Jasmine cannot trigger click event

I am trying to implement a simple Jasmine test in which Jasmine will check if any code is executed when I press the enter button. But I can not understand why the click does not start? I understand that this will happen if I only have a function .click()in beforeEach, but I don't think it should work.

the functions

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
  }
});
+5
source share
2 answers

Have you really added an element in the DOM to the instrument? Also you are missing a); on the device 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);
  });
});

fittings

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

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

  $("input#myButton").click(function() {
    $("#content").html("<p>Hello</p>");
  });
})();
+3
source

I think your device should be such that it matches your test case.

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

        $("input#myButton").click(function() {
            $("body").append("<div id='content'>")
        });
    })();
0
source

All Articles