The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/features/query/state/DashboardQueryRunner/AlertStatesWorker.test.ts

124 lines
4.5 KiB

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
5 years ago
import { AlertState, AlertStateInfo, getDefaultTimeRange, TimeRange } from '@grafana/data';
import { backendSrv } from 'app/core/services/backend_srv';
import { DashboardQueryRunnerOptions } from './types';
import { AlertStatesWorker } from './AlertStatesWorker';
import { silenceConsoleOutput } from '../../../../../test/core/utils/silenceConsoleOutput';
import * as store from '../../../../store/store';
jest.mock('@grafana/runtime', () => ({
...((jest.requireActual('@grafana/runtime') as unknown) as object),
getBackendSrv: () => backendSrv,
}));
function getDefaultOptions(): DashboardQueryRunnerOptions {
const dashboard: any = { id: 'an id' };
const range = getDefaultTimeRange();
return { dashboard, range };
}
function getTestContext() {
jest.clearAllMocks();
const dispatchMock = jest.spyOn(store, 'dispatch');
const options = getDefaultOptions();
const getMock = jest.spyOn(backendSrv, 'get');
return { getMock, options, dispatchMock };
}
describe('AlertStatesWorker', () => {
const worker = new AlertStatesWorker();
describe('when canWork is called with correct props', () => {
it('then it should return true', () => {
const options = getDefaultOptions();
expect(worker.canWork(options)).toBe(true);
});
});
describe('when canWork is called with no dashboard id', () => {
it('then it should return false', () => {
const dashboard: any = {};
const options = { ...getDefaultOptions(), dashboard };
expect(worker.canWork(options)).toBe(false);
});
});
describe('when canWork is called with wrong range', () => {
it('then it should return false', () => {
const defaultRange = getDefaultTimeRange();
const range: TimeRange = { ...defaultRange, raw: { ...defaultRange.raw, to: 'now-6h' } };
const options = { ...getDefaultOptions(), range };
expect(worker.canWork(options)).toBe(false);
});
});
describe('when run is called with incorrect props', () => {
it('then it should return the correct results', async () => {
const { getMock, options } = getTestContext();
const dashboard: any = {};
await expect(worker.work({ ...options, dashboard })).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual({ alertStates: [], annotations: [] });
expect(getMock).not.toHaveBeenCalled();
});
});
});
describe('when run is called with correct props and request is successful', () => {
it('then it should return the correct results', async () => {
const getResults: AlertStateInfo[] = [
{ id: 1, state: AlertState.Alerting, newStateDate: '2021-01-01', dashboardId: 1, panelId: 1 },
{ id: 2, state: AlertState.Alerting, newStateDate: '2021-02-01', dashboardId: 1, panelId: 2 },
];
const { getMock, options } = getTestContext();
getMock.mockResolvedValue(getResults);
await expect(worker.work(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual({ alertStates: getResults, annotations: [] });
expect(getMock).toHaveBeenCalledTimes(1);
});
});
});
describe('when run is called with correct props and request fails', () => {
silenceConsoleOutput();
it('then it should return the correct results', async () => {
const { getMock, options, dispatchMock } = getTestContext();
getMock.mockRejectedValue({ message: 'An error' });
await expect(worker.work(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual({ alertStates: [], annotations: [] });
expect(getMock).toHaveBeenCalledTimes(1);
expect(dispatchMock).toHaveBeenCalledTimes(1);
});
});
});
describe('when run is called with correct props and request is cancelled', () => {
silenceConsoleOutput();
it('then it should return the correct results', async () => {
const { getMock, options, dispatchMock } = getTestContext();
getMock.mockRejectedValue({ cancelled: true });
await expect(worker.work(options)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual({ alertStates: [], annotations: [] });
expect(getMock).toHaveBeenCalledTimes(1);
expect(dispatchMock).not.toHaveBeenCalled();
});
});
});
});