1

I am attempting to write unit tests for the following function using Jasmine:

function toggleDisplay(divIdToShowHide, showHide) {
var divToShowHide = document.getElementById(divIdToShowHide);
if (showHide == "show")
    divToShowHide.style.display = "block";
else if (showHide == "hide")
    divToShowHide.style.display = "none";
else
    divToShowHide.style.display = (divToShowHide.style.display == "block") ? "none" : "block";
var buttonForDiv = document.getElementById(divIdToShowHide + 'Button');
if (divToShowHide.style.display == "block")
    buttonForDiv.innerHTML = "-";
else
    buttonForDiv.innerHTML = "+";
}

I need to test this against a sample HTML code I have written containing various butttons. I am lost on how to get the jasmine tests for the code to actually run on the HTML(since it needs to getElementById). Is there a simple way to implement this?

1 Answer 1

1

You should look in Jasmine Jquery , it allows you to create tests against specific HTML, so you can see if your javascript is working or not.

Using Jasmine jquery, you can create a html fixture(just a real or generated html page), and then use their provided jquery-based methods to can check and see if your javascript did what it was supposed to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.