ExpressionDatasource.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { DataSourceInstanceSettings } from '@grafana/data';
  2. import { backendSrv } from 'app/core/services/backend_srv';
  3. import { ExpressionDatasourceApi } from './ExpressionDatasource';
  4. import { ExpressionQueryType } from './types';
  5. jest.mock('@grafana/runtime', () => ({
  6. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  7. getBackendSrv: () => backendSrv,
  8. getTemplateSrv: () => ({
  9. replace: (val: string) => (val ? val.replace('$input', '10').replace('$window', '10s') : val),
  10. }),
  11. }));
  12. describe('ExpressionDatasourceApi', () => {
  13. beforeEach(() => {
  14. jest.clearAllMocks();
  15. });
  16. describe('expression queries with template variables', () => {
  17. it('should interpolate template variables in expression query', () => {
  18. const ds = new ExpressionDatasourceApi({} as DataSourceInstanceSettings);
  19. const query = ds.applyTemplateVariables(
  20. { type: ExpressionQueryType.math, refId: 'B', expression: '$input + 5 + $A' },
  21. {}
  22. );
  23. expect(query.expression).toBe('10 + 5 + $A');
  24. });
  25. it('should interpolate template variables in expression query', () => {
  26. const ds = new ExpressionDatasourceApi({} as DataSourceInstanceSettings);
  27. const query = ds.applyTemplateVariables(
  28. { type: ExpressionQueryType.resample, refId: 'B', window: '$window' },
  29. {}
  30. );
  31. expect(query.window).toBe('10s');
  32. });
  33. });
  34. });