grafana/public/app/plugins/datasource/loki/components/LokiQueryField.test.tsx
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

82 lines
2.2 KiB
TypeScript

import { render } from '@testing-library/react';
import React, { ComponentProps } from 'react';
import { dateTime } from '@grafana/data';
import { LokiQueryField } from './LokiQueryField';
type Props = ComponentProps<typeof LokiQueryField>;
const defaultProps: Props = {
datasource: {
languageProvider: {
start: () => Promise.resolve(['label1']),
fetchLabels: Promise.resolve(['label1']),
getSyntax: () => {},
getLabelKeys: () => ['label1'],
getLabelValues: () => ['value1'],
} as any,
getInitHints: () => [],
} as any,
range: {
from: dateTime([2021, 1, 11, 12, 0, 0]),
to: dateTime([2021, 1, 11, 18, 0, 0]),
raw: {
from: 'now-1h',
to: 'now',
},
},
query: { expr: '', refId: '' },
onRunQuery: () => {},
onChange: () => {},
history: [],
};
describe('LokiQueryField', () => {
it('refreshes metrics when time range changes over 1 minute', async () => {
const fetchLabelsMock = jest.fn();
const props = defaultProps;
props.datasource.languageProvider.fetchLabels = fetchLabelsMock;
const { rerender } = render(<LokiQueryField {...props} />);
expect(fetchLabelsMock).not.toHaveBeenCalled();
// 2 minutes difference over the initial time
const newRange = {
from: dateTime([2021, 1, 11, 12, 2, 0]),
to: dateTime([2021, 1, 11, 18, 2, 0]),
raw: {
from: 'now-1h',
to: 'now',
},
};
rerender(<LokiQueryField {...props} range={newRange} />);
expect(fetchLabelsMock).toHaveBeenCalledTimes(1);
});
it('does not refreshes metrics when time range change by less than 1 minute', async () => {
const fetchLabelsMock = jest.fn();
const props = defaultProps;
props.datasource.languageProvider.fetchLabels = fetchLabelsMock;
const { rerender } = render(<LokiQueryField {...props} />);
expect(fetchLabelsMock).not.toHaveBeenCalled();
// 20 seconds difference over the initial time
const newRange = {
from: dateTime([2021, 1, 11, 12, 0, 20]),
to: dateTime([2021, 1, 11, 18, 0, 20]),
raw: {
from: 'now-1h',
to: 'now',
},
};
rerender(<LokiQueryField {...props} range={newRange} />);
expect(fetchLabelsMock).not.toHaveBeenCalled();
});
});