interpolation.test.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React from 'react';
  2. import { DataQueryRequest, serializeStateToUrlParam } from '@grafana/data';
  3. import { getTemplateSrv } from '@grafana/runtime';
  4. import { LokiQuery } from '../../../plugins/datasource/loki/types';
  5. import { makeLogsQueryResponse } from './helper/query';
  6. import { setupExplore, waitForExplore } from './helper/setup';
  7. const fetch = jest.fn();
  8. jest.mock('@grafana/runtime', () => ({
  9. ...jest.requireActual('@grafana/runtime'),
  10. getBackendSrv: () => ({ fetch }),
  11. }));
  12. jest.mock('react-virtualized-auto-sizer', () => {
  13. return {
  14. __esModule: true,
  15. default(props: any) {
  16. return <div>{props.children({ width: 1000 })}</div>;
  17. },
  18. };
  19. });
  20. describe('Explore: interpolation', () => {
  21. // support-escalations/issues/1459
  22. it('Time is interpolated when explore is opened with a URL', async () => {
  23. const urlParams = {
  24. left: serializeStateToUrlParam({
  25. datasource: 'loki',
  26. queries: [{ refId: 'A', expr: '{ job="a", from="${__from}", to="${__to}" }' }],
  27. range: { from: '1600000000000', to: '1700000000000' },
  28. }),
  29. right: serializeStateToUrlParam({
  30. datasource: 'loki',
  31. queries: [{ refId: 'b', expr: '{ job="b", from="${__from}", to="${__to}" }' }],
  32. range: { from: '1800000000000', to: '1900000000000' },
  33. }),
  34. };
  35. const { datasources } = setupExplore({ urlParams });
  36. const fakeFetch = jest.fn();
  37. (datasources.loki.query as jest.Mock).mockImplementation((request: DataQueryRequest<LokiQuery>) => {
  38. fakeFetch(getTemplateSrv().replace(request.targets[0]!.expr));
  39. return makeLogsQueryResponse();
  40. });
  41. await waitForExplore();
  42. expect(fakeFetch).toBeCalledTimes(2);
  43. expect(fakeFetch).toHaveBeenCalledWith('{ job="a", from="1600000000000", to="1700000000000" }');
  44. expect(fakeFetch).toHaveBeenCalledWith('{ job="b", from="1800000000000", to="1900000000000" }');
  45. });
  46. });