graph-specs.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. define([
  2. 'helpers',
  3. 'angular',
  4. 'jquery',
  5. 'components/timeSeries',
  6. 'panels/graph/graph'
  7. ], function(helpers, angular, $, TimeSeries) {
  8. 'use strict';
  9. describe('grafanaGraph', function() {
  10. beforeEach(module('grafana.directives'));
  11. function graphScenario(desc, func) {
  12. describe(desc, function() {
  13. var ctx = {};
  14. ctx.setup = function (setupFunc) {
  15. beforeEach(module(function($provide) {
  16. $provide.value("timeSrv", new helpers.TimeSrvStub());
  17. }));
  18. beforeEach(inject(function($rootScope, $compile) {
  19. var scope = $rootScope.$new();
  20. var element = angular.element("<div style='width:500px' grafana-graph><div>");
  21. scope.height = '200px';
  22. scope.panel = {
  23. legend: {},
  24. grid: {},
  25. y_formats: [],
  26. seriesOverrides: [],
  27. tooltip: {
  28. shared: true
  29. }
  30. };
  31. scope.appEvent = sinon.spy();
  32. scope.onAppEvent = sinon.spy();
  33. scope.hiddenSeries = {};
  34. scope.dashboard = { timezone: 'browser' };
  35. scope.range = {
  36. from: new Date('2014-08-09 10:00:00'),
  37. to: new Date('2014-09-09 13:00:00')
  38. };
  39. ctx.data = [];
  40. ctx.data.push(new TimeSeries({
  41. datapoints: [[1,1],[2,2]],
  42. alias: 'series1'
  43. }));
  44. ctx.data.push(new TimeSeries({
  45. datapoints: [[1,1],[2,2]],
  46. alias: 'series2'
  47. }));
  48. setupFunc(scope, ctx.data);
  49. $compile(element)(scope);
  50. scope.$digest();
  51. $.plot = ctx.plotSpy = sinon.spy();
  52. scope.$emit('render', ctx.data);
  53. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  54. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  55. }));
  56. };
  57. func(ctx);
  58. });
  59. }
  60. graphScenario('simple lines options', function(ctx) {
  61. ctx.setup(function(scope) {
  62. scope.panel.lines = true;
  63. scope.panel.fill = 5;
  64. scope.panel.linewidth = 3;
  65. scope.panel.steppedLine = true;
  66. });
  67. it('should configure plot with correct options', function() {
  68. expect(ctx.plotOptions.series.lines.show).to.be(true);
  69. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  70. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  71. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  72. });
  73. });
  74. graphScenario('grid thresholds 100, 200', function(ctx) {
  75. ctx.setup(function(scope) {
  76. scope.panel.grid = {
  77. threshold1: 100,
  78. threshold1Color: "#111",
  79. threshold2: 200,
  80. threshold2Color: "#222",
  81. };
  82. });
  83. it('should add grid markings', function() {
  84. var markings = ctx.plotOptions.grid.markings;
  85. expect(markings[0].yaxis.from).to.be(100);
  86. expect(markings[0].yaxis.to).to.be(200);
  87. expect(markings[0].color).to.be('#111');
  88. expect(markings[1].yaxis.from).to.be(200);
  89. expect(markings[1].yaxis.to).to.be(Infinity);
  90. });
  91. });
  92. graphScenario('inverted grid thresholds 200, 100', function(ctx) {
  93. ctx.setup(function(scope) {
  94. scope.panel.grid = {
  95. threshold1: 200,
  96. threshold1Color: "#111",
  97. threshold2: 100,
  98. threshold2Color: "#222",
  99. };
  100. });
  101. it('should add grid markings', function() {
  102. var markings = ctx.plotOptions.grid.markings;
  103. expect(markings[0].yaxis.from).to.be(200);
  104. expect(markings[0].yaxis.to).to.be(100);
  105. expect(markings[0].color).to.be('#111');
  106. expect(markings[1].yaxis.from).to.be(100);
  107. expect(markings[1].yaxis.to).to.be(-Infinity);
  108. });
  109. });
  110. graphScenario('grid thresholds from zero', function(ctx) {
  111. ctx.setup(function(scope) {
  112. scope.panel.grid = {
  113. threshold1: 0,
  114. threshold1Color: "#111",
  115. };
  116. });
  117. it('should add grid markings', function() {
  118. var markings = ctx.plotOptions.grid.markings;
  119. expect(markings[0].yaxis.from).to.be(0);
  120. });
  121. });
  122. graphScenario('should use timeStep for barWidth', function(ctx) {
  123. ctx.setup(function(scope, data) {
  124. scope.panel.bars = true;
  125. data[0] = new TimeSeries({
  126. datapoints: [[1,10],[2,20]],
  127. alias: 'series1',
  128. });
  129. });
  130. it('should set barWidth', function() {
  131. expect(ctx.plotOptions.series.bars.barWidth).to.be(10/1.5);
  132. });
  133. });
  134. graphScenario('series option overrides, fill & points', function(ctx) {
  135. ctx.setup(function(scope, data) {
  136. scope.panel.lines = true;
  137. scope.panel.fill = 5;
  138. scope.panel.seriesOverrides = [
  139. { alias: 'test', fill: 0, points: true }
  140. ];
  141. data[1].alias = 'test';
  142. });
  143. it('should match second series and fill zero, and enable points', function() {
  144. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  145. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  146. expect(ctx.plotData[1].points.show).to.be(true);
  147. });
  148. });
  149. graphScenario('should order series order according to zindex', function(ctx) {
  150. ctx.setup(function(scope) {
  151. scope.panel.seriesOverrides = [{ alias: 'series1', zindex: 2 }];
  152. });
  153. it('should move zindex 2 last', function() {
  154. expect(ctx.plotData[0].alias).to.be('series2');
  155. expect(ctx.plotData[1].alias).to.be('series1');
  156. });
  157. });
  158. graphScenario('when series is hidden', function(ctx) {
  159. ctx.setup(function(scope) {
  160. scope.hiddenSeries = {'series2': true};
  161. });
  162. it('should remove datapoints and disable stack', function() {
  163. expect(ctx.plotData[0].alias).to.be('series1');
  164. expect(ctx.plotData[1].data.length).to.be(0);
  165. expect(ctx.plotData[1].stack).to.be(false);
  166. });
  167. });
  168. });
  169. });