datasource.test.ts 2.2 KB

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