Reputation: 838
I am relatively new to Javascript and using QUnit for the first time. I have created some global functions in a file and included that file into my javaScriptTests.html file using the 'script' tag.
Within javaScriptTests.html I have declared an object to pass into the declared functions and return the result. However, the test is failing. At first glance it seemed like it was QUnit but I think the actual problem is probably my poor javascript skills in defining functions in a fool proof way.
The QUnit error is:
My declared functions inside of bradri.js look like this:
var findNodeIndex = function(id, nodes){
// return node index with a given id
var length = nodes.length || 0;
for (var index=0; index < length; index++){
if (nodes[index].name === id){
return index;
};
};
};
var buildLinks = function (nodes){
// build the links table for d3
var links = [],
length = nodes.length;
for (var i=0; i < length; i++){
if (nodes[i].mother){
//console.log(i, nodes[i].mother);
links.push({source: i, target: findNodeIndex(nodes[i].mother)});
};
if (nodes[i].father){
links.push({source: i, target: findNodeIndex(nodes[i].farther)});
};
}
return links;
};
nodes is not defined in this file. The error seems to suggest that QUnit expects the var 'nodes' to be defined in bradri.js even though that are only internally used and 'nodes' is meant to be passed in from javaScriptTests.html
This is what my test looks like:
QUnit.module("unrelated test", {
setup: function() {
// add it to current context 'this'
this.testNodes = [
{name: 'a', mother: '', farther: ''},
{name: 'b', mother: '', farther: ''},
{name: 'c', mother: '', farther: ''},
{name: 'd', mother: '', farther: ''},
{name: 'e', mother: '', farther: ''},
{name: 'f', mother: 'a', farther: 'b'},
{name: 'g', mother: 'a', farther: 'b'},
{name: 'h', mother: 'a', farther: 'b'},
{name: 'i', mother: 'c', farther: 'd'},
{name: 'j', mother: 'c', farther: 'd'},
{name: 'k', mother: '', farther: ''},
{name: 'l', mother: 'e', farther: 'f'},
{name: 'm', mother: 'j', farther: 'k'},
{name: 'n', mother: 'l', farther: 'm'}
];
}
});
QUnit.test( "Unit testing of custom D3 code", function( assert ) {
var result = '[{"source":5,"target":0},{"source":6,"target":0},{"source":7,"target":0},{"source":8,"target":2},{"source":9,"target":2},{"source":11,"target":4},{"source":12,"target":9},{"source":13,"target":11}]';
var temp = buildLinks(this.testNodes); // IT FAILS HERE
//JSON.stringify(temp)
//assert.equal(result, result, "We expect value to be hello" );
});
Upvotes: 0
Views: 70
Reputation: 7619
You're missing the second nodes
argument when calling findNodeIndex(nodes[i].mother);
Change those calls to findNodeIndex(nodes[i].mother, nodes);
Upvotes: 1