interactiveBisectTest.html 2.79 KB
Newer Older
afe's avatar
afe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<!DOCTYPE html>
<meta charset="utf-8">

<link href="../src/nv.d3.css" rel="stylesheet" type="text/css">
<link href="teststyle.css" rel="stylesheet" type='text/css'>

<body>
  <h3>Unit tests for nv.interactiveBisect - this function is important for rendering tooltips and the guideline on charts.</h3>
  <div class='navigation'>
      Tests:
      <a href="lineChartTest.html">Line Chart</a>
      <a href="stackedAreaChartTest.html">Stacked Area</a>
      <a href="../examples/cumulativeLineChart.html">Cumulative Line</a>
  </div>

<script src="../lib/d3.v3.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/interactiveLayer.js"></script>

<script>
function runTest(description, dataArray, searchVal, expectedResult, xAccessor) {
    var result = nv.interactiveBisect(dataArray, searchVal, xAccessor);
    var content = "";
    if (result === expectedResult) {
        content = "PASSED: " + description;
    }
    else {
        content = "FAILED: " + description;
    }

    var node = document.createElement("div");
    node.innerHTML = content;
    document.getElementsByTagName("body")[0].appendChild(node);
}

var x = function(d) {return d;};

runTest("Basic test",
      [0,1,2,3,4,5],
      3,
      3,
      x
  );

runTest("Basic test - zero bound",
      [0,1,2,3,4,5],
      0,
      0,
      x
  );

runTest("Basic test - length bound",
      [0,1,2,3,4,5],
      5,
      5,
      x
  );

runTest("Basic test - negative number",
      [0,1,2,3,4,5],
      -4,
      0,
      x
  );

runTest("Basic test - past the end",
      [0,1,2,3,4,5],
      10,
      5,
      x
  );

runTest("Floating point number",
      [0,1,2,3,4,5],
      0.34,
      0,
      x
  );

runTest("Floating point number part 2",
      [0,1,2,3,4,5],
      1.50001,
      2,
      x
  );

runTest("Fibonacci - existing item search",
      [0,1,1,2,3,5,8,13,21,34],
      8,
      6,
      x
  );

runTest("Fibonacci - inbetween item (left)",
      [0,1,1,2,3,5,8,13,21,34],
      15,
      7,
      x
  );

runTest("Fibonacci - inbetween item (right)",
      [0,1,1,2,3,5,8,13,21,34],
      20,
      8,
      x
  );

x = function(d,i) { return i};

runTest("xAccessor is index mode - existing item",
      [0,1,1,2,3,5,8,13,21,34],
      7,
      7,
      x
  );

runTest("xAccessor is index mode - inbetween item",
      [0,1,1,2,3,5,8,13,21,34],
      7.3,
      7,
      x
  );

runTest("xAccessor is index mode - inbetween item part 2",
      [0,1,1,2,3,5,8,13,21,34],
      7.500001,
      8,
      x
  );

runTest("Empty array",
      [],
      4,
      0,
      x
  );

runTest("Single element array",
      [0],
      0,
      0,
      x
  );

runTest("Single element array - negative bound",
      [0],
      -10,
      0,
      x
  );

runTest("Single element array - past the end",
      [0],
      1,
      0,
      x
  );
</script>