2021-04-25 23:13:03 -05:00
|
|
|
import { AlertState, getDefaultTimeRange, LoadingState, PanelData, toDataFrame } from '@grafana/data';
|
|
|
|
import { mergePanelAndDashData } from './mergePanelAndDashData';
|
2021-06-15 06:33:24 -05:00
|
|
|
import { TestScheduler } from 'rxjs/testing';
|
2021-04-25 23:13:03 -05:00
|
|
|
|
|
|
|
function getTestContext() {
|
|
|
|
const timeRange = getDefaultTimeRange();
|
|
|
|
const panelData: PanelData = {
|
|
|
|
state: LoadingState.Done,
|
|
|
|
series: [],
|
|
|
|
annotations: [toDataFrame([{ id: 'panelData' }])],
|
|
|
|
timeRange,
|
|
|
|
};
|
2021-06-15 06:33:24 -05:00
|
|
|
const scheduler: TestScheduler = new TestScheduler((actual, expected) => {
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
2021-04-25 23:13:03 -05:00
|
|
|
|
2021-07-02 03:52:13 -05:00
|
|
|
return { timeRange, scheduler, panelData };
|
2021-04-25 23:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('mergePanelAndDashboardData', () => {
|
2021-07-02 03:52:13 -05:00
|
|
|
describe('when called and dashboard data contains annotations', () => {
|
|
|
|
it('then the annotations should be combined', () => {
|
|
|
|
const { panelData, timeRange, scheduler } = getTestContext();
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
scheduler.run(({ cold, expectObservable }) => {
|
2021-07-02 03:52:13 -05:00
|
|
|
const panelObservable = cold('a', { a: panelData });
|
|
|
|
const dashObservable = cold('a', { a: { annotations: [{ id: 'dashData' }] } });
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
const result = mergePanelAndDashData(panelObservable, dashObservable);
|
|
|
|
|
2021-07-02 03:52:13 -05:00
|
|
|
expectObservable(result).toBe('a', {
|
2021-06-15 06:33:24 -05:00
|
|
|
a: {
|
|
|
|
state: LoadingState.Done,
|
|
|
|
series: [],
|
|
|
|
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
|
|
|
|
timeRange,
|
|
|
|
},
|
2021-04-25 23:13:03 -05:00
|
|
|
});
|
|
|
|
});
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
scheduler.flush();
|
2021-04-25 23:13:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-02 03:52:13 -05:00
|
|
|
describe('when called and dashboard data contains alert states', () => {
|
|
|
|
it('then the alert states should be added', () => {
|
|
|
|
const { panelData, timeRange, scheduler } = getTestContext();
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
scheduler.run(({ cold, expectObservable }) => {
|
2021-07-02 03:52:13 -05:00
|
|
|
const panelObservable = cold('a', { a: panelData });
|
|
|
|
const dashObservable = cold('a', {
|
2021-06-15 06:33:24 -05:00
|
|
|
a: {
|
2021-07-02 03:52:13 -05:00
|
|
|
annotations: [],
|
2021-04-25 23:13:03 -05:00
|
|
|
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
|
2021-06-15 06:33:24 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = mergePanelAndDashData(panelObservable, dashObservable);
|
|
|
|
|
2021-07-02 03:52:13 -05:00
|
|
|
expectObservable(result).toBe('a', {
|
2021-06-15 06:33:24 -05:00
|
|
|
a: {
|
2021-04-25 23:13:03 -05:00
|
|
|
state: LoadingState.Done,
|
|
|
|
series: [],
|
2021-07-02 03:52:13 -05:00
|
|
|
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([])],
|
2021-04-25 23:13:03 -05:00
|
|
|
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
|
|
|
|
timeRange,
|
2021-06-15 06:33:24 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
scheduler.flush();
|
2021-04-25 23:13:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-02 03:52:13 -05:00
|
|
|
describe('when called and dashboard data does not contain annotations or alertState', () => {
|
|
|
|
it('then the panelData is unchanged', () => {
|
|
|
|
const { panelData, timeRange, scheduler } = getTestContext();
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
scheduler.run(({ cold, expectObservable }) => {
|
2021-07-02 03:52:13 -05:00
|
|
|
const panelObservable = cold('a', { a: panelData });
|
|
|
|
const dashObservable = cold('a', {
|
|
|
|
a: {
|
|
|
|
annotations: [],
|
|
|
|
},
|
|
|
|
});
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
const result = mergePanelAndDashData(panelObservable, dashObservable);
|
|
|
|
|
2021-07-02 03:52:13 -05:00
|
|
|
expectObservable(result).toBe('a', {
|
2021-06-15 06:33:24 -05:00
|
|
|
a: {
|
|
|
|
state: LoadingState.Done,
|
|
|
|
series: [],
|
|
|
|
annotations: [toDataFrame([{ id: 'panelData' }])],
|
|
|
|
timeRange,
|
|
|
|
},
|
2021-04-25 23:13:03 -05:00
|
|
|
});
|
|
|
|
});
|
2021-06-15 06:33:24 -05:00
|
|
|
|
|
|
|
scheduler.flush();
|
2021-04-25 23:13:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|