LokiQueryEditor.test.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { shallow } from 'enzyme';
  2. import React from 'react';
  3. import { toUtc } from '@grafana/data';
  4. import { LokiDatasource } from '../datasource';
  5. import { LokiQuery } from '../types';
  6. import { LokiQueryEditor } from './LokiQueryEditor';
  7. const createMockRequestRange = (from: string, to: string) => {
  8. return {
  9. from: toUtc(from, 'YYYY-MM-DD'),
  10. to: toUtc(to, 'YYYY-MM-DD'),
  11. };
  12. };
  13. const setup = (propOverrides?: object) => {
  14. const datasourceMock: unknown = {};
  15. const datasource: LokiDatasource = datasourceMock as LokiDatasource;
  16. const onRunQuery = jest.fn();
  17. const onChange = jest.fn();
  18. const query: LokiQuery = {
  19. expr: '',
  20. refId: 'A',
  21. legendFormat: 'My Legend',
  22. };
  23. const range = createMockRequestRange('2020-01-01', '2020-01-02');
  24. const props: any = {
  25. datasource,
  26. onChange,
  27. onRunQuery,
  28. query,
  29. range,
  30. };
  31. Object.assign(props, propOverrides);
  32. const wrapper = shallow(<LokiQueryEditor {...props} />);
  33. const instance = wrapper.instance();
  34. return {
  35. instance,
  36. wrapper,
  37. };
  38. };
  39. describe('Render LokiQueryEditor with legend', () => {
  40. it('should render', () => {
  41. const { wrapper } = setup();
  42. expect(wrapper).toMatchSnapshot();
  43. });
  44. it('should update timerange', () => {
  45. const { wrapper } = setup();
  46. wrapper.setProps({
  47. range: createMockRequestRange('2019-01-01', '2020-01-02'),
  48. });
  49. expect(wrapper).toMatchSnapshot();
  50. });
  51. });