datasource.jest.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 => val.replace('$var', 'replaced')
  15. };
  16. const ds = new GrafanaDatasource(backendSrvStub, q, templateSrvStub);
  17. describe('with tags that have template variables', () => {
  18. const options = setupAnnotationQueryOptions(
  19. {tags: ['tag1:$var']}
  20. );
  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 type dashboard', () => {
  29. const options = setupAnnotationQueryOptions(
  30. {
  31. type: 'dashboard',
  32. tags: ['tag1']
  33. },
  34. {id: 1}
  35. );
  36. beforeEach(() => {
  37. return ds.annotationQuery(options);
  38. });
  39. it('should remove tags from query options', () => {
  40. expect(calledBackendSrvParams.tags).toBe(undefined);
  41. });
  42. });
  43. });
  44. });
  45. function setupAnnotationQueryOptions(annotation, dashboard?) {
  46. return {
  47. annotation: annotation,
  48. dashboard: dashboard,
  49. range: {
  50. from: moment(1432288354),
  51. to: moment(1432288401)
  52. },
  53. rangeRaw: {from: "now-24h", to: "now"}
  54. };
  55. }