views:

61

answers:

2

Hi all.

I inherited a legacy JavaScript library simply written as a list of functions as follow:

function checkSubtree(targetList, objId)
{
    ...
}

function checkRootSubtree(targetList, rootLength, rootInfo, level)
{
    ...
}

To test it with JsTestDriver do I have to 'clean' it to adhere to some JavaScript best practice or can I test it without modification?

Thanks

A: 

You can still test it using JsTestDriver. JsTestDriver is only a test runner, it doesn't require your code to be written in any special way. It's hard to give any advice on actual testing without seeing some code (i.e. function bodies).

Christian
<pre>function checkSubtree(targetList, objId){ var obj=document.getElementById(objId); var len = targetList.length; if (!len) { targetList.checked=obj.checked; return; } var i=0; for(i=0; i<len; i++) { if (targetList[i].id.indexOf(objId)!=-1 ) { targetList[i].checked=obj.checked; } if (!obj.checked) { if (objId.indexOf(targetList[i].id)!=-1 ) { targetList[i].checked=obj.checked; } } } }</pre>
Giorgio Vespucci
<pre> tag does'n work in Comments(?)
Giorgio Vespucci
@Giorgio Put it in the question -___-"
M28
A: 

Hello Giorgio!

The HtmlDoc document fragment feature of jsTestDriver helps when unit testing DOM-dependent JavaScript code. You probably want a little HTML chunk to apply those functions to, as you go along.

When you've proven that they work, you'll see ways of making the functions more testable. This is one of the hidden gems of unit testing: emergent software design. Since you want to be able to test your code in isolation, you'll have an incentive to reduce coupling.

olleolleolle