ankitr
ankitr

Reputation: 6172

How to write Mocha test case for HTML elements?

I am testing my JavaScript code with Mocha / PhantomJS setup to run the tests.

There is a function:

function getNodeClickWithExpand(idElement, treeName, autoCompleteName) {
    jQuery("input[type='checkbox'][name='c_" + idElement + "']").trigger("click");
    var treeView = jQuery("#" + treeName).data("kendoTreeView");
    treeView.expand(document.getElementById(idElement));
    jQuery("#" + idElement).closest("div").find("span:last").addClass("k-state-selected");
    kendoUiHoverAutoScrolling(idElement, treeName, autoCompleteName);
}

I am writing a test case for this function in Mocha:

describe("getNodeClickWithExpand", function () {
    it("should pass with correct inputs", function () {
        var processJsonObject = getNodeClickWithExpand(idElement, treeName, autoCompleteName);
        console.log(processJsonObject);
    });
});

when I run this test case it gives me an error. I know this is not correct. Please tell me how to run test case for HTML elements and events.

EDIT:

Error I am getting:

Upvotes: 1

Views: 2877

Answers (1)

booleanhunter
booleanhunter

Reputation: 5880

I believe the issue is here-

it("should pass with correct inputs", function () {
    var processJsonObject = getNodeClickWithExpand(idElement, treeName, autoCompleteName);

The test error 'Cannot read property 'expand' of null' indicates that there is no element in the dom with id = idElement. You need to pass an element's id as a string when you are running the test. You are also not passing any value for the parameters 'treeName' and 'autoCompleteName'.

Upvotes: 1

Related Questions