Explore: Avoid reinitializing graph on every query run (#77281)

This commit is contained in:
Giordano Ricci 2023-10-27 14:37:23 +01:00 committed by GitHub
parent f750c3194e
commit bf554d121c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -51,6 +51,15 @@ beforeAll(() => {
describe('useStructureRev', () => {
afterEach(() => resetCounters());
// mirrors the logic in componentDidUpdate in packages/grafana-ui/src/components/GraphNG/GraphNG.tsx,
// which treats all falsy values for structureRev as a signal to reconfig the graph
it('should start from a thruthy value', () => {
let frames: DataFrame[] = [toDataFrame({ fields: [{ name: 'time', type: FieldType.time, values: [1, 2, 3] }] })];
const { result } = renderHook((frames) => useStructureRev(frames), { initialProps: frames });
expect(result.current).not.toBeFalsy();
});
it('should increment only when relevant fields in frame change', () => {
let frames: DataFrame[] = [toDataFrame({ fields: [{ name: 'time', type: FieldType.time, values: [1, 2, 3] }] })];
const { result, rerender } = renderHook((frames) => useStructureRev(frames), { initialProps: frames });

View File

@ -4,7 +4,7 @@ import { useCounter, usePrevious } from 'react-use';
import { DataFrame, compareArrayValues, compareDataFrameStructures } from '@grafana/data';
export function useStructureRev(frames: DataFrame[]) {
const [structureRev, { inc }] = useCounter(0);
const [structureRev, { inc }] = useCounter(1);
const previousFrames = usePrevious(frames);
// We need to increment structureRev when the number of series changes.