Chore: Refactors flaky Frontend test (#35645)

* Chore: Refactors flaky Frontend test

* Refactor: update after PR comments
This commit is contained in:
Hugo Häggmark 2021-06-15 13:33:24 +02:00 committed by GitHub
parent 303352a89b
commit 3395142ece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,8 @@
import { asyncScheduler, Observable, of, scheduled } from 'rxjs';
import { AlertState, getDefaultTimeRange, LoadingState, PanelData, toDataFrame } from '@grafana/data';
import { DashboardQueryRunnerResult } from './DashboardQueryRunner/types';
import { mergePanelAndDashData } from './mergePanelAndDashData';
import { delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
function getTestContext() {
const timeRange = getDefaultTimeRange();
@ -17,104 +16,123 @@ function getTestContext() {
annotations: [{ id: 'dashData' }],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
};
const panelObservable: Observable<PanelData> = scheduled(of(panelData), asyncScheduler);
const dashObservable: Observable<DashboardQueryRunnerResult> = scheduled(of(dashData), asyncScheduler);
const scheduler: TestScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
return { timeRange, panelObservable, dashObservable };
return { timeRange, scheduler, panelData, dashData };
}
describe('mergePanelAndDashboardData', () => {
describe('when both results are fast', () => {
it('then just combine the results', async () => {
const { dashObservable, panelObservable, timeRange } = getTestContext();
it('then just combine the results', () => {
const { panelData, dashData, timeRange, scheduler } = getTestContext();
await expect(mergePanelAndDashData(panelObservable, dashObservable)).toEmitValuesWith((received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual({
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
timeRange,
scheduler.run(({ cold, expectObservable }) => {
const panelObservable = cold('10ms a', { a: panelData });
const dashObservable = cold('10ms a', { a: dashData });
const result = mergePanelAndDashData(panelObservable, dashObservable);
expectObservable(result).toBe('10ms a', {
a: {
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
timeRange,
},
});
});
scheduler.flush();
});
});
describe('when dashboard results are slow', () => {
it('then flush panel data first', async () => {
const { dashObservable, panelObservable, timeRange } = getTestContext();
it('then flush panel data first', () => {
const { panelData, dashData, timeRange, scheduler } = getTestContext();
await expect(mergePanelAndDashData(panelObservable, dashObservable.pipe(delay(250)))).toEmitValuesWith(
(received) => {
expect(received).toHaveLength(2);
const fastResults = received[0];
const slowResults = received[1];
expect(fastResults).toEqual({
scheduler.run(({ cold, expectObservable }) => {
const panelObservable = cold('10ms a', { a: panelData });
const dashObservable = cold('210ms a', { a: dashData });
const result = mergePanelAndDashData(panelObservable, dashObservable);
expectObservable(result).toBe('200ms a 9ms b', {
a: {
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }])],
alertState: undefined,
timeRange,
});
expect(slowResults).toEqual({
},
b: {
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
timeRange,
});
}
);
},
});
});
scheduler.flush();
});
});
describe('when panel results are slow', () => {
it('then just combine the results', async () => {
const { dashObservable, panelObservable, timeRange } = getTestContext();
it('then just combine the results', () => {
const { panelData, dashData, timeRange, scheduler } = getTestContext();
await expect(mergePanelAndDashData(panelObservable.pipe(delay(250)), dashObservable)).toEmitValuesWith(
(received) => {
expect(received).toHaveLength(1);
const results = received[0];
expect(results).toEqual({
scheduler.run(({ cold, expectObservable }) => {
const panelObservable = cold('210ms a', { a: panelData });
const dashObservable = cold('10ms a', { a: dashData });
const result = mergePanelAndDashData(panelObservable, dashObservable);
expectObservable(result).toBe('210ms a', {
a: {
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
timeRange,
});
}
);
},
});
});
scheduler.flush();
});
});
describe('when both results are slow', () => {
it('then flush panel data first', async () => {
const { dashObservable, panelObservable, timeRange } = getTestContext();
it('then flush panel data first', () => {
const { panelData, dashData, timeRange, scheduler } = getTestContext();
await expect(
mergePanelAndDashData(panelObservable.pipe(delay(250)), dashObservable.pipe(delay(250)))
).toEmitValuesWith((received) => {
expect(received).toHaveLength(2);
const fastResults = received[0];
const slowResults = received[1];
expect(fastResults).toEqual({
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }])],
alertState: undefined,
timeRange,
});
expect(slowResults).toEqual({
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
timeRange,
scheduler.run(({ cold, expectObservable }) => {
const panelObservable = cold('210ms a', { a: panelData });
const dashObservable = cold('210ms a', { a: dashData });
const result = mergePanelAndDashData(panelObservable, dashObservable);
expectObservable(result).toBe('210ms (ab)', {
a: {
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }])],
timeRange,
},
b: {
state: LoadingState.Done,
series: [],
annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
timeRange,
},
});
});
scheduler.flush();
});
});
});