mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Loki: Allow setting of timeRange when using languageProvider functions * Loki: Use timerange where available for start * Loki: Use timerange where available for fetchLabels * Loki: Use timerange where available for fetchSeriesLabels * Loki: Use timerange where available for fetchLabelValues * Loki: Use timerange where available for getParserAndLabelKeys * Loki: Update and add tests for fetchLabels * Loki: Update and add tests for fetchSeriesLabels * Loki: Update and add tests for fetchSeries * Loki: Update and add tests for fetchLabelValues * Loki: Update and add tests for fetchLabelValues * Loki: Update and add tests for getParserAndLabelKeys * Update public/app/plugins/datasource/loki/LanguageProvider.test.ts Co-authored-by: Matias Chomicki <matyax@gmail.com> * Update public/app/plugins/datasource/loki/LanguageProvider.test.ts Co-authored-by: Matias Chomicki <matyax@gmail.com> * Not needing to use languageProvider.getDefaultTime in Monaco * Update comment * Update getDefaultTimeRange to be ptivate --------- Co-authored-by: Matias Chomicki <matyax@gmail.com>
76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import React, { ComponentProps } from 'react';
|
|
|
|
import { dateTime } from '@grafana/data';
|
|
|
|
import { createLokiDatasource } from '../mocks';
|
|
|
|
import { LokiQueryField } from './LokiQueryField';
|
|
|
|
type Props = ComponentProps<typeof LokiQueryField>;
|
|
describe('LokiQueryField', () => {
|
|
let props: Props;
|
|
beforeEach(() => {
|
|
props = {
|
|
datasource: createLokiDatasource(),
|
|
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: [],
|
|
};
|
|
jest.spyOn(props.datasource.languageProvider, 'start').mockResolvedValue([]);
|
|
jest.spyOn(props.datasource.languageProvider, 'fetchLabels').mockResolvedValue(['label1']);
|
|
});
|
|
|
|
it('refreshes metrics when time range changes over 1 minute', async () => {
|
|
const { rerender } = render(<LokiQueryField {...props} />);
|
|
|
|
expect(await screen.findByText('Loading...')).toBeInTheDocument();
|
|
|
|
expect(props.datasource.languageProvider.fetchLabels).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(props.datasource.languageProvider.fetchLabels).toHaveBeenCalledTimes(1);
|
|
expect(props.datasource.languageProvider.fetchLabels).toHaveBeenCalledWith({ timeRange: newRange });
|
|
});
|
|
|
|
it('does not refreshes metrics when time range change by less than 1 minute', async () => {
|
|
const { rerender } = render(<LokiQueryField {...props} />);
|
|
|
|
expect(await screen.findByText('Loading...')).toBeInTheDocument();
|
|
|
|
expect(props.datasource.languageProvider.fetchLabels).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(props.datasource.languageProvider.fetchLabels).not.toHaveBeenCalled();
|
|
});
|
|
});
|