Reputation: 123
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
}
});
Upvotes: 5
Views: 24703
Reputation: 245
I think your fixture should be like this to suit your Test Case
(function() {
$("body").html("<input id='myButton' type='button'>");
$("input#myButton").click(function() {
$("body").append("<div id='content'>")
});
})();
Upvotes: 0
Reputation: 8417
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>");
});
})();
Upvotes: 5