I would like to start using a test framework, mocha + expect seem good to me.
Say i have this function,
/*
* Hides or shows loading .gif
* show : boolean
*/
function loadingMsg(show) {
var $loader = $("#loader");
if(show){
$loader.show();
}
else{
$loader.hide();
}
}
To make it testable, do I need to add Return statements like below?
/*
* Hides or shows loading .gif
* show : boolean
*/
function loadingMsg(show) {
var $loader = $("#loader");
if($loader){
if(show){
$loader.show();
return "loader shown";
}
else{
$loader.hide();
return "loader shown";
}
}
else {
return "$loader undefined";
}
}
Or should I be testing the dom element "loader"?
No you don't.
Instead check for loader element display property in the DOM.
Update after comment:
haven't used much of mocha but when in comes to testing UI, its similar fashion almost in every framework or libarary. Look for elements in the DOM using jQuery or any selector lib you have available and check their content if they are as desired. In your case you will check if loader element is display block or display none.
something like when loader is shown
when loader is hidden