datasource.test.ts 2.2 KB

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