PageRenderTime 25ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gallery-charts/tests/charts.html

https://github.com/nonano/yui3-gallery
HTML | 235 lines | 213 code | 22 blank | 0 comment | 0 complexity | da49cf9b2e5bdf5ee819ddfb9c56b0d0 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <style>
  6. /*Supplemental: CSS for the YUI distribution*/
  7. #custom-doc { width: 95%; min-width: 950px; }
  8. #pagetitle {background-image: url(../../assets/bg_hd.gif);}
  9. #mychart {
  10. padding:10px 10px 10px 10px;
  11. }
  12. </style>
  13. <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.1.2/build/cssfonts/fonts-min.css">
  14. </head>
  15. <body class="yui3-skin-sam">
  16. <h3>Unit Tests</h3>
  17. <div id="mychart"></div>
  18. <script type="text/javascript" src="http://yui.yahooapis.com/3.1.2/build/yui/yui-min.js"></script>
  19. <script type="text/javascript" src="../../../build/gallery-charts/gallery-charts-min.js"></script>
  20. <script>
  21. YUI().use('gallery-charts', 'test', 'console', function (Y)
  22. {
  23. Y.namespace("example.test");
  24. var suite = new Y.Test.Suite("Y.Charts");
  25. suite.add(new Y.Test.Case({
  26. name: "Chart Tests",
  27. setUp: function() {
  28. var myDataValues = [
  29. {category:"5/1/2010", values:2000, expenses:3700, revenue:2200},
  30. {category:"5/2/2010", values:50, expenses:9100, revenue:100},
  31. {category:"5/3/2010", values:400, expenses:1100, revenue:1500},
  32. {category:"5/4/2010", values:200, expenses:1900, revenue:2800},
  33. {category:"5/5/2010", values:5000, expenses:5000, revenue:2650}
  34. ];
  35. var mychart = new Y.Chart({width:400, height:300, dataProvider:myDataValues});
  36. mychart.render("#mychart");
  37. this.chart = mychart;
  38. },
  39. tearDown: function() {
  40. this.chart.destroy();
  41. },
  42. //Test to ensure that all items in the series collection are of the correct type.
  43. testGetSeriesByIndex: function()
  44. {
  45. var series = this.chart.getSeries(0),
  46. assert = Y.Assert;
  47. assert.isInstanceOf(Y.CartesianSeries, series);
  48. },
  49. //Test to ensure that all items in the series collection are of the correct type.
  50. testGetSeriesByKey: function()
  51. {
  52. var series = this.chart.getSeries("revenue"),
  53. assert = Y.Assert;
  54. assert.isInstanceOf(Y.CartesianSeries, series);
  55. },
  56. //Test to ensure the series axes are numeric and the category axis is of type category
  57. testGetAxesByKey: function()
  58. {
  59. var category = this.chart.getAxisByKey("category"),
  60. values = this.chart.getAxisByKey("values"),
  61. assert = Y.Assert;
  62. assert.isInstanceOf(Y.CategoryAxis, category);
  63. assert.isInstanceOf(Y.NumericAxis, values);
  64. },
  65. //Test to ensure that getCategoryAxis returns a category axis
  66. testGetCategoryAxis: function()
  67. {
  68. var category = this.chart.get("categoryAxis"),
  69. assert = Y.Assert;
  70. assert.isInstanceOf(Y.CategoryAxis, category);
  71. },
  72. //Test that the graph attribute is of type Graph
  73. testGetGraph: function()
  74. {
  75. Y.Assert.isInstanceOf(Y.Graph, this.chart.get("graph"));
  76. },
  77. //Test to ensure that the axes hash contains AxisRenderer instances
  78. testGetAxes: function()
  79. {
  80. var assert = Y.Assert,
  81. axes = this.chart.get("axes"),
  82. i;
  83. for(i in axes)
  84. {
  85. if(axes.hasOwnProperty(i))
  86. {
  87. assert.isInstanceOf(Y.Axis, axes[i]);
  88. }
  89. }
  90. },
  91. //Test to ensure that default series keys are correct
  92. testGetSeriesKeys: function()
  93. {
  94. var assert = Y.Assert,
  95. YArray = Y.Array,
  96. selectedIndex,
  97. testKeys = ['values', 'expenses', 'revenue'],
  98. newArray = [],
  99. actualKeys = this.chart.get("seriesKeys"),
  100. i = 0,
  101. len = testKeys.length;
  102. assert.areEqual(actualKeys.length, testKeys.length, "Actual seriesKeys array is not the correct length.");
  103. for(; i < len; ++i)
  104. {
  105. selectedIndex = YArray.indexOf(actualKeys, testKeys[i]);
  106. assert.isNotNull(selectedIndex + 1, "The seriesKeys array should contain the following key: " + testKeys[i] + ".");
  107. if(selectedIndex > -1)
  108. {
  109. newArray.push(actualKeys[selectedIndex]);
  110. }
  111. else
  112. {
  113. throw new Error("The actual seriesKeys array should but does not contain " + testKeys[i] + ".");
  114. }
  115. }
  116. assert.areEqual(newArray.length, actualKeys.length, "The seriesKeys array has more keys than it should.");
  117. },
  118. //Test to ensure default attributes are correct
  119. testGetDefaultAttributes: function()
  120. {
  121. var assert = Y.Assert,
  122. attrs = {
  123. direction: "horizontal",
  124. type: "combo",
  125. valueAxisName: "values",
  126. categoryAxisName: "category",
  127. categoryKey: "category"
  128. },
  129. chart = this.chart,
  130. i;
  131. for(i in attrs)
  132. {
  133. if(attrs.hasOwnProperty(i))
  134. {
  135. assert.areEqual(chart.get(i), attrs[i], "The attribute " + i + " should equal " + attrs[i] + ".");
  136. }
  137. }
  138. }
  139. }));
  140. suite.add(new Y.Test.Case({
  141. name: "Axes Tests",
  142. setUp: function() {
  143. var myDataValues = [
  144. {category:"5/1/2010", values:2000, expenses:3700, revenue:2200},
  145. {category:"5/2/2010", values:50, expenses:9100, revenue:100},
  146. {category:"5/3/2010", values:400, expenses:1100, revenue:1500},
  147. {category:"5/4/2010", values:200, expenses:1900, revenue:2800},
  148. {category:"5/5/2010", values:5000, expenses:5000, revenue:2650}
  149. ];
  150. var mychart = new Y.Chart({width:400, height:300, dataProvider:myDataValues, seriesKeys:["values", "revenue"]});
  151. mychart.render("#mychart");
  152. this.chart = mychart;
  153. },
  154. tearDown: function() {
  155. this.chart.destroy();
  156. },
  157. //Test axes data classes
  158. testRemoveKey: function()
  159. {
  160. var assert = Y.Assert,
  161. xAxis = this.chart.getCategoryAxis(),
  162. yAxis = this.chart.getAxisByKey("values"),
  163. keys,
  164. l,
  165. i;
  166. xAxis.on("axisUpdate", Y.bind(function(e) {
  167. l = xAxis.get("keyCollection").length || 0;
  168. assert.areEqual(0, 0, "The value should be zero");
  169. }, this));
  170. yAxis.on("axisUpdate", Y.bind(function(e) {
  171. keys = yAxis.get("keyCollection");
  172. assert.areEqual(1, keys.length, "The length should be 1");
  173. assert.areEqual(Y.Array.indexOf(keys, "revenue"), -1, "The key revenue should be removed");
  174. }));
  175. xAxis.removeKey("category");
  176. yAxis.removeKey("revenue");
  177. },
  178. testAddKey: function()
  179. {
  180. var assert = Y.Assert,
  181. yAxis = this.chart.getAxisByKey("values"),
  182. keys,
  183. pattern = [3700, 9100, 1100, 1900],
  184. testarray,
  185. i = 0;
  186. l = 4;
  187. yAxis.on("axisUpdate", Y.bind(function(e) {
  188. keys = yAxis.get("keyCollection");
  189. testarray = yAxis.getDataByKey("expenses");
  190. assert.areEqual(3, keys.length);
  191. assert.areEqual(Y.Array.indexOf(keys, "expenses"), 2);
  192. for(; i < l; ++i)
  193. {
  194. assert.areEqual(pattern[i], testarray[i]);
  195. }
  196. }, this));
  197. yAxis.addKey("expenses");
  198. }
  199. }));
  200. //create the console
  201. var r = new Y.Console({
  202. newestOnTop : false,
  203. style: 'block' // to anchor in the example content
  204. });
  205. r.render('#testLogger');
  206. //run the tests
  207. Y.Test.Runner.setName("Y.Charts");
  208. Y.Test.Runner.add(suite);
  209. Y.Test.Runner.run();
  210. });
  211. </script>
  212. </body>
  213. </html>