grafanaGraph-specs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. define([
  2. './helpers',
  3. 'angular',
  4. 'jquery',
  5. 'components/timeSeries',
  6. 'directives/grafanaGraph'
  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. };
  28. scope.hiddenSeries = {};
  29. scope.dashboard = { timezone: 'browser' };
  30. scope.range = {
  31. from: new Date('2014-08-09 10:00:00'),
  32. to: new Date('2014-09-09 13:00:00')
  33. };
  34. ctx.data = [];
  35. ctx.data.push(new TimeSeries({
  36. datapoints: [[1,1],[2,2]],
  37. info: { alias: 'series1', enable: true }
  38. }));
  39. ctx.data.push(new TimeSeries({
  40. datapoints: [[1,1],[2,2]],
  41. info: { alias: 'series2', enable: true }
  42. }));
  43. setupFunc(scope, ctx.data);
  44. $compile(element)(scope);
  45. scope.$digest();
  46. $.plot = ctx.plotSpy = sinon.spy();
  47. scope.$emit('render', ctx.data);
  48. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  49. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  50. }));
  51. };
  52. func(ctx);
  53. });
  54. }
  55. graphScenario('simple lines options', function(ctx) {
  56. ctx.setup(function(scope) {
  57. scope.panel.lines = true;
  58. scope.panel.fill = 5;
  59. scope.panel.linewidth = 3;
  60. scope.panel.steppedLine = true;
  61. });
  62. it('should configure plot with correct options', function() {
  63. expect(ctx.plotOptions.series.lines.show).to.be(true);
  64. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  65. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  66. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  67. });
  68. });
  69. graphScenario('grid thresholds 100, 200', function(ctx) {
  70. ctx.setup(function(scope) {
  71. scope.panel.grid = {
  72. threshold1: 100,
  73. threshold1Color: "#111",
  74. threshold2: 200,
  75. threshold2Color: "#222",
  76. };
  77. });
  78. it('should add grid markings', function() {
  79. var markings = ctx.plotOptions.grid.markings;
  80. expect(markings[0].yaxis.from).to.be(100);
  81. expect(markings[0].yaxis.to).to.be(200);
  82. expect(markings[0].color).to.be('#111');
  83. expect(markings[1].yaxis.from).to.be(200);
  84. expect(markings[1].yaxis.to).to.be(Infinity);
  85. });
  86. });
  87. graphScenario('inverted grid thresholds 200, 100', function(ctx) {
  88. ctx.setup(function(scope) {
  89. scope.panel.grid = {
  90. threshold1: 200,
  91. threshold1Color: "#111",
  92. threshold2: 100,
  93. threshold2Color: "#222",
  94. };
  95. });
  96. it('should add grid markings', function() {
  97. var markings = ctx.plotOptions.grid.markings;
  98. expect(markings[0].yaxis.from).to.be(200);
  99. expect(markings[0].yaxis.to).to.be(100);
  100. expect(markings[0].color).to.be('#111');
  101. expect(markings[1].yaxis.from).to.be(100);
  102. expect(markings[1].yaxis.to).to.be(-Infinity);
  103. });
  104. });
  105. graphScenario('series option overrides, fill & points', function(ctx) {
  106. ctx.setup(function(scope, data) {
  107. scope.panel.lines = true;
  108. scope.panel.fill = 5;
  109. scope.panel.seriesOverrides = [
  110. { alias: 'test', fill: 0, points: true }
  111. ];
  112. data[1].info.alias = 'test';
  113. });
  114. it('should match second series and fill zero, and enable points', function() {
  115. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  116. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  117. expect(ctx.plotData[1].points.show).to.be(true);
  118. });
  119. });
  120. graphScenario('should order series order according to zindex', function(ctx) {
  121. ctx.setup(function(scope) {
  122. scope.panel.seriesOverrides = [{ alias: 'series1', zindex: 2 }];
  123. });
  124. it('should move zindex 2 last', function() {
  125. expect(ctx.plotData[0].info.alias).to.be('series2');
  126. expect(ctx.plotData[1].info.alias).to.be('series1');
  127. });
  128. });
  129. graphScenario('when series is hidden', function(ctx) {
  130. ctx.setup(function(scope) {
  131. scope.hiddenSeries = {'series2': true};
  132. });
  133. it('should remove datapoints and disable stack', function() {
  134. expect(ctx.plotData[0].info.alias).to.be('series1');
  135. expect(ctx.plotData[1].data.length).to.be(0);
  136. expect(ctx.plotData[1].stack).to.be(false);
  137. });
  138. });
  139. });
  140. });