graph_specs.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common';
  3. import '../module';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import helpers from 'test/specs/helpers';
  7. import TimeSeries from 'app/core/time_series2';
  8. import moment from 'moment';
  9. import {Emitter} from 'app/core/core';
  10. describe('grafanaGraph', function() {
  11. beforeEach(angularMocks.module('grafana.directives'));
  12. function graphScenario(desc, func, elementWidth = 500) {
  13. describe(desc, function() {
  14. var ctx: any = {};
  15. ctx.setup = function(setupFunc) {
  16. beforeEach(angularMocks.module(function($provide) {
  17. $provide.value("timeSrv", new helpers.TimeSrvStub());
  18. }));
  19. beforeEach(angularMocks.inject(function($rootScope, $compile) {
  20. var ctrl: any = {
  21. events: new Emitter(),
  22. height: 200,
  23. panel: {
  24. legend: {},
  25. grid: { },
  26. yaxes: [
  27. {
  28. min: null,
  29. max: null,
  30. format: 'short',
  31. logBase: 1
  32. },
  33. {
  34. min: null,
  35. max: null,
  36. format: 'short',
  37. logBase: 1
  38. }
  39. ],
  40. thresholds: [],
  41. xaxis: {},
  42. seriesOverrides: [],
  43. tooltip: {
  44. shared: true
  45. }
  46. },
  47. renderingCompleted: sinon.spy(),
  48. hiddenSeries: {},
  49. dashboard: {
  50. getTimezone: sinon.stub().returns('browser')
  51. },
  52. range: {
  53. from: moment([2015, 1, 1, 10]),
  54. to: moment([2015, 1, 1, 22]),
  55. },
  56. };
  57. var scope = $rootScope.$new();
  58. scope.ctrl = ctrl;
  59. $rootScope.onAppEvent = sinon.spy();
  60. ctx.data = [];
  61. ctx.data.push(new TimeSeries({
  62. datapoints: [[1,1],[2,2]],
  63. alias: 'series1'
  64. }));
  65. ctx.data.push(new TimeSeries({
  66. datapoints: [[1,1],[2,2]],
  67. alias: 'series2'
  68. }));
  69. setupFunc(ctrl, ctx.data);
  70. var element = angular.element("<div style='width:" + elementWidth + "px' grafana-graph><div>");
  71. $compile(element)(scope);
  72. scope.$digest();
  73. $.plot = ctx.plotSpy = sinon.spy();
  74. ctrl.events.emit('render', ctx.data);
  75. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  76. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  77. }));
  78. };
  79. func(ctx);
  80. });
  81. }
  82. graphScenario('simple lines options', function(ctx) {
  83. ctx.setup(function(ctrl) {
  84. ctrl.panel.lines = true;
  85. ctrl.panel.fill = 5;
  86. ctrl.panel.linewidth = 3;
  87. ctrl.panel.steppedLine = true;
  88. });
  89. it('should configure plot with correct options', function() {
  90. expect(ctx.plotOptions.series.lines.show).to.be(true);
  91. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  92. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  93. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  94. });
  95. });
  96. graphScenario('when logBase is log 10', function(ctx) {
  97. ctx.setup(function(ctrl) {
  98. ctrl.panel.yaxes[0].logBase = 10;
  99. });
  100. it('should apply axis transform and ticks', function() {
  101. var axis = ctx.plotOptions.yaxes[0];
  102. expect(axis.transform(100)).to.be(Math.log(100+0.1));
  103. expect(axis.ticks[0]).to.be(0);
  104. expect(axis.ticks[1]).to.be(1);
  105. });
  106. });
  107. graphScenario('should use timeStep for barWidth', function(ctx) {
  108. ctx.setup(function(ctrl, data) {
  109. ctrl.panel.bars = true;
  110. data[0] = new TimeSeries({
  111. datapoints: [[1,10],[2,20]],
  112. alias: 'series1',
  113. });
  114. });
  115. it('should set barWidth', function() {
  116. expect(ctx.plotOptions.series.bars.barWidth).to.be(10/1.5);
  117. });
  118. });
  119. graphScenario('series option overrides, fill & points', function(ctx) {
  120. ctx.setup(function(ctrl, data) {
  121. ctrl.panel.lines = true;
  122. ctrl.panel.fill = 5;
  123. data[0].zindex = 10;
  124. data[1].alias = 'test';
  125. data[1].lines = {fill: 0.001};
  126. data[1].points = {show: true};
  127. });
  128. it('should match second series and fill zero, and enable points', function() {
  129. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  130. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  131. expect(ctx.plotData[1].points.show).to.be(true);
  132. });
  133. });
  134. graphScenario('should order series order according to zindex', function(ctx) {
  135. ctx.setup(function(ctrl, data) {
  136. data[1].zindex = 1;
  137. data[0].zindex = 10;
  138. });
  139. it('should move zindex 2 last', function() {
  140. expect(ctx.plotData[0].alias).to.be('series2');
  141. expect(ctx.plotData[1].alias).to.be('series1');
  142. });
  143. });
  144. graphScenario('when series is hidden', function(ctx) {
  145. ctx.setup(function(ctrl) {
  146. ctrl.hiddenSeries = {'series2': true};
  147. });
  148. it('should remove datapoints and disable stack', function() {
  149. expect(ctx.plotData[0].alias).to.be('series1');
  150. expect(ctx.plotData[1].data.length).to.be(0);
  151. expect(ctx.plotData[1].stack).to.be(false);
  152. });
  153. });
  154. graphScenario('when stack and percent', function(ctx) {
  155. ctx.setup(function(ctrl) {
  156. ctrl.panel.percentage = true;
  157. ctrl.panel.stack = true;
  158. });
  159. it('should show percentage', function() {
  160. var axis = ctx.plotOptions.yaxes[0];
  161. expect(axis.tickFormatter(100, axis)).to.be("100%");
  162. });
  163. });
  164. graphScenario('when panel too narrow to show x-axis dates in same granularity as wide panels', function(ctx) {
  165. describe('and the range is less than 24 hours', function() {
  166. ctx.setup(function(ctrl) {
  167. ctrl.range.from = moment([2015, 1, 1, 10]);
  168. ctrl.range.to = moment([2015, 1, 1, 22]);
  169. });
  170. it('should format dates as hours minutes', function() {
  171. var axis = ctx.plotOptions.xaxis;
  172. expect(axis.timeformat).to.be('%H:%M');
  173. });
  174. });
  175. describe('and the range is less than one year', function() {
  176. ctx.setup(function(scope) {
  177. scope.range.from = moment([2015, 1, 1]);
  178. scope.range.to = moment([2015, 11, 20]);
  179. });
  180. it('should format dates as month days', function() {
  181. var axis = ctx.plotOptions.xaxis;
  182. expect(axis.timeformat).to.be('%m/%d');
  183. });
  184. });
  185. }, 10);
  186. });