datasource.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {GrafanaDatasource} from "../datasource";
  2. import q from 'q';
  3. import moment from 'moment';
  4. describe('grafana data source', () => {
  5. describe('when executing an annotations query', () => {
  6. let calledBackendSrvParams;
  7. const backendSrvStub = {
  8. get: (url, options) => {
  9. calledBackendSrvParams = options;
  10. return q.resolve([]);
  11. }
  12. };
  13. const templateSrvStub = {
  14. replace: val => {
  15. return val
  16. .replace('$var2', 'replaced|replaced2')
  17. .replace('$var', 'replaced');
  18. }
  19. };
  20. const ds = new GrafanaDatasource(backendSrvStub, q, templateSrvStub);
  21. describe('with tags that have template variables', () => {
  22. const options = setupAnnotationQueryOptions(
  23. {tags: ['tag1:$var']}
  24. );
  25. beforeEach(() => {
  26. return ds.annotationQuery(options);
  27. });
  28. it('should interpolate template variables in tags in query options', () => {
  29. expect(calledBackendSrvParams.tags[0]).toBe('tag1:replaced');
  30. });
  31. });
  32. describe('with tags that have multi value template variables', () => {
  33. const options = setupAnnotationQueryOptions(
  34. {tags: ['$var2']}
  35. );
  36. beforeEach(() => {
  37. return ds.annotationQuery(options);
  38. });
  39. it('should interpolate template variables in tags in query options', () => {
  40. expect(calledBackendSrvParams.tags[0]).toBe('replaced');
  41. expect(calledBackendSrvParams.tags[1]).toBe('replaced2');
  42. });
  43. });
  44. describe('with type dashboard', () => {
  45. const options = setupAnnotationQueryOptions(
  46. {
  47. type: 'dashboard',
  48. tags: ['tag1']
  49. },
  50. {id: 1}
  51. );
  52. beforeEach(() => {
  53. return ds.annotationQuery(options);
  54. });
  55. it('should remove tags from query options', () => {
  56. expect(calledBackendSrvParams.tags).toBe(undefined);
  57. });
  58. });
  59. });
  60. });
  61. function setupAnnotationQueryOptions(annotation, dashboard?) {
  62. return {
  63. annotation: annotation,
  64. dashboard: dashboard,
  65. range: {
  66. from: moment(1432288354),
  67. to: moment(1432288401)
  68. },
  69. rangeRaw: {from: "now-24h", to: "now"}
  70. };
  71. }