grafana/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner...

122 lines
4.2 KiB
TypeScript
Raw Normal View History

Annotations: Adds DashboardQueryRunner (#32834) * WIP: initial commit * Fix: Fixed $timeout call when testing snapshots * Chore: reverts changes to metrics_panel_ctrl.ts * Chore: reverts changes to annotations_srv * Refactor: adds DashboardQueryRunner.run to initdashboard * Refactor: adds run to dashboard model start refresh * Refactor: move to own folder and split up into smaller files * Tests: adds tests for LegacyAnnotationQueryRunner * Tests: adds tests for AnnotationsQueryRunner * Tests: adds tests for SnapshotWorker * Refactor: renames from canRun|run to canWork|work * Tests: adds tests for AlertStatesWorker * Tests: adds tests for AnnotationsWorker * Refactor: renames operators * Refactor: renames operators * Tests: adds tests for DashboardQueryRunner * Refactor: adds mergePanelAndDashboardData function * Tests: fixes broken tests * Chore: Fixes errors after merge with master * Chore: Removes usage of AnnotationSrv from event_editor and initDashboard * WIP: getting annotations and alerts working in graph (snapshot not working) * Refactor: fixes snapshot data for React panels * Refactor: Fixes so snapshots work for Graph * Refactor: moves alert types to grafana-data * Refactor: changes to some for readability * Tests: skipping tests for now, needs rewrite * Refactor: refactors out common static functions to utils * Refactor: fixes resolving annotations from dataframes * Refactor: removes getRunners/Workers functions * Docs: fixes docs errors * Docs: trying to fix doc error * Refactor: changes after PR comments * Refactor: hides everything behind a factory instead * Refactor: adds cancellation between runs and explicitly
2021-04-26 12:13:03 +08:00
import { getDefaultTimeRange } from '@grafana/data';
import { LegacyAnnotationQueryRunner } from './LegacyAnnotationQueryRunner';
import { AnnotationQueryRunnerOptions } from './types';
import { silenceConsoleOutput } from '../../../../../test/core/utils/silenceConsoleOutput';
import * as store from '../../../../store/store';
function getDefaultOptions(annotationQuery?: jest.Mock): AnnotationQueryRunnerOptions {
const annotation: any = {};
const dashboard: any = {};
const datasource: any = {
annotationQuery: annotationQuery ?? jest.fn().mockResolvedValue([{ id: '1' }]),
};
const range = getDefaultTimeRange();
return { annotation, datasource, dashboard, range };
}
function getTestContext(annotationQuery?: jest.Mock) {
jest.clearAllMocks();
const dispatchMock = jest.spyOn(store, 'dispatch');
const options = getDefaultOptions(annotationQuery);
const annotationQueryMock = options.datasource!.annotationQuery;
Annotations: Adds DashboardQueryRunner (#32834) * WIP: initial commit * Fix: Fixed $timeout call when testing snapshots * Chore: reverts changes to metrics_panel_ctrl.ts * Chore: reverts changes to annotations_srv * Refactor: adds DashboardQueryRunner.run to initdashboard * Refactor: adds run to dashboard model start refresh * Refactor: move to own folder and split up into smaller files * Tests: adds tests for LegacyAnnotationQueryRunner * Tests: adds tests for AnnotationsQueryRunner * Tests: adds tests for SnapshotWorker * Refactor: renames from canRun|run to canWork|work * Tests: adds tests for AlertStatesWorker * Tests: adds tests for AnnotationsWorker * Refactor: renames operators * Refactor: renames operators * Tests: adds tests for DashboardQueryRunner * Refactor: adds mergePanelAndDashboardData function * Tests: fixes broken tests * Chore: Fixes errors after merge with master * Chore: Removes usage of AnnotationSrv from event_editor and initDashboard * WIP: getting annotations and alerts working in graph (snapshot not working) * Refactor: fixes snapshot data for React panels * Refactor: Fixes so snapshots work for Graph * Refactor: moves alert types to grafana-data * Refactor: changes to some for readability * Tests: skipping tests for now, needs rewrite * Refactor: refactors out common static functions to utils * Refactor: fixes resolving annotations from dataframes * Refactor: removes getRunners/Workers functions * Docs: fixes docs errors * Docs: trying to fix doc error * Refactor: changes after PR comments * Refactor: hides everything behind a factory instead * Refactor: adds cancellation between runs and explicitly
2021-04-26 12:13:03 +08:00
return { options, dispatchMock, annotationQueryMock };
}
describe('LegacyAnnotationQueryRunner', () => {
const runner = new LegacyAnnotationQueryRunner();
describe('when canWork is called with correct props', () => {
it('then it should return true', () => {
const datasource: any = {
annotationQuery: jest.fn(),
};
expect(runner.canRun(datasource)).toBe(true);
});
});
describe('when canWork is called without datasource', () => {
it('then it should return false', () => {
const datasource: any = undefined;
expect(runner.canRun(datasource)).toBe(false);
});
});
Annotations: Adds DashboardQueryRunner (#32834) * WIP: initial commit * Fix: Fixed $timeout call when testing snapshots * Chore: reverts changes to metrics_panel_ctrl.ts * Chore: reverts changes to annotations_srv * Refactor: adds DashboardQueryRunner.run to initdashboard * Refactor: adds run to dashboard model start refresh * Refactor: move to own folder and split up into smaller files * Tests: adds tests for LegacyAnnotationQueryRunner * Tests: adds tests for AnnotationsQueryRunner * Tests: adds tests for SnapshotWorker * Refactor: renames from canRun|run to canWork|work * Tests: adds tests for AlertStatesWorker * Tests: adds tests for AnnotationsWorker * Refactor: renames operators * Refactor: renames operators * Tests: adds tests for DashboardQueryRunner * Refactor: adds mergePanelAndDashboardData function * Tests: fixes broken tests * Chore: Fixes errors after merge with master * Chore: Removes usage of AnnotationSrv from event_editor and initDashboard * WIP: getting annotations and alerts working in graph (snapshot not working) * Refactor: fixes snapshot data for React panels * Refactor: Fixes so snapshots work for Graph * Refactor: moves alert types to grafana-data * Refactor: changes to some for readability * Tests: skipping tests for now, needs rewrite * Refactor: refactors out common static functions to utils * Refactor: fixes resolving annotations from dataframes * Refactor: removes getRunners/Workers functions * Docs: fixes docs errors * Docs: trying to fix doc error * Refactor: changes after PR comments * Refactor: hides everything behind a factory instead * Refactor: adds cancellation between runs and explicitly
2021-04-26 12:13:03 +08:00
describe('when canWork is called with incorrect props', () => {
it('then it should return false', () => {
const datasource: any = {
annotationQuery: jest.fn(),
annotations: {},
};
expect(runner.canRun(datasource)).toBe(false);
});
});
describe('when run is called with unsupported props', () => {
it('then it should return the correct results', async () => {
const datasource: any = {
annotationQuery: jest.fn(),
annotations: {},
};
const options = { ...getDefaultOptions(), datasource };
await expect(runner.run(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual([]);
expect(datasource.annotationQuery).not.toHaveBeenCalled();
});
});
});
describe('when run is called and the request is successful', () => {
it('then it should return the correct results', async () => {
const { options, annotationQueryMock } = getTestContext();
await expect(runner.run(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual([{ id: '1' }]);
expect(annotationQueryMock).toHaveBeenCalledTimes(1);
});
});
});
describe('when run is called and the request fails', () => {
silenceConsoleOutput();
it('then it should return the correct results', async () => {
const annotationQuery = jest.fn().mockRejectedValue({ message: 'An error' });
const { options, annotationQueryMock, dispatchMock } = getTestContext(annotationQuery);
await expect(runner.run(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual([]);
expect(annotationQueryMock).toHaveBeenCalledTimes(1);
expect(dispatchMock).toHaveBeenCalledTimes(1);
});
});
});
describe('when run is called and the request is cancelled', () => {
silenceConsoleOutput();
it('then it should return the correct results', async () => {
const annotationQuery = jest.fn().mockRejectedValue({ cancelled: true });
const { options, annotationQueryMock, dispatchMock } = getTestContext(annotationQuery);
await expect(runner.run(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual([]);
expect(annotationQueryMock).toHaveBeenCalledTimes(1);
expect(dispatchMock).not.toHaveBeenCalled();
});
});
});
});