graphiteTargetCtrl-specs.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. define([
  2. './helpers',
  3. 'services/graphite/gfunc',
  4. 'controllers/graphiteTarget'
  5. ], function(helpers, gfunc) {
  6. 'use strict';
  7. describe('GraphiteTargetCtrl', function() {
  8. var ctx = new helpers.ControllerTestContext();
  9. beforeEach(module('grafana.controllers'));
  10. beforeEach(ctx.providePhase());
  11. beforeEach(ctx.createControllerPhase('GraphiteTargetCtrl'));
  12. beforeEach(function() {
  13. ctx.scope.target = {
  14. target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)'
  15. };
  16. ctx.scope.datasource = ctx.datasource;
  17. ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
  18. });
  19. describe('init', function() {
  20. beforeEach(function() {
  21. ctx.scope.init();
  22. ctx.scope.$digest();
  23. });
  24. it('should validate metric key exists', function() {
  25. expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
  26. });
  27. it('should delete last segment if no metrics are found', function() {
  28. expect(ctx.scope.segments[2].value).to.be('select metric');
  29. });
  30. it('should parse expression and build function model', function() {
  31. expect(ctx.scope.functions.length).to.be(2);
  32. });
  33. });
  34. describe('when adding function', function() {
  35. beforeEach(function() {
  36. ctx.scope.target.target = 'test.prod.*.count';
  37. ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
  38. ctx.scope.init();
  39. ctx.scope.$digest();
  40. ctx.scope.$parent = { get_data: sinon.spy() };
  41. ctx.scope.addFunction(gfunc.getFuncDef('aliasByNode'));
  42. });
  43. it('should add function with correct node number', function() {
  44. expect(ctx.scope.functions[0].params[0]).to.be(2);
  45. });
  46. it('should update target', function() {
  47. expect(ctx.scope.target.target).to.be('aliasByNode(test.prod.*.count,2)');
  48. });
  49. it('should call get_data', function() {
  50. expect(ctx.scope.$parent.get_data.called).to.be(true);
  51. });
  52. });
  53. describe('targetChanged', function() {
  54. beforeEach(function() {
  55. ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
  56. ctx.scope.init();
  57. ctx.scope.$digest();
  58. ctx.scope.$parent = { get_data: sinon.spy() };
  59. ctx.scope.target.target = '';
  60. ctx.scope.targetChanged();
  61. });
  62. it('should rebuld target after expression model', function() {
  63. expect(ctx.scope.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*,1),2)');
  64. });
  65. it('should call get_data', function() {
  66. expect(ctx.scope.$parent.get_data.called).to.be(true);
  67. });
  68. });
  69. });
  70. });