Skip to content

Commit

Permalink
fix(ChartStatus): render complete if same parent size is dispatched (#…
Browse files Browse the repository at this point in the history
…2534)

fix(ChartResizer): dont trigger render if displatched the same parent size
  • Loading branch information
markov00 authored Oct 2, 2024
1 parent 8803baf commit c3aba88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/charts/src/specs/specs_parser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { DEFAULT_SETTINGS_SPEC } from './constants';
import { SpecsParser } from './specs_parser';
import { BarSeries } from '../chart_types/specs';
import { BarSeriesSpec } from '../chart_types/xy_chart/utils/specs';
import { ChartContainer } from '../components/chart_container';
import { updateParentDimensions } from '../state/actions/chart_settings';
import { chartStoreReducer } from '../state/chart_state';

describe('Specs parser', () => {
Expand Down Expand Up @@ -170,4 +172,44 @@ describe('Specs parser', () => {
component.unmount();
expect(chartStore.getState().specsInitialized).toBe(false);
});

test('correctly set the rendered status', () => {
const storeReducer = chartStoreReducer('chart_id');
const chartStore = createStore(storeReducer);

expect(chartStore.getState().specsInitialized).toBe(false);
const chartContainerRef: React.RefObject<HTMLDivElement> = React.createRef();
const getChartContainerRef = () => chartContainerRef;
const chartStageRef: React.RefObject<HTMLCanvasElement> = React.createRef();

const component = (
<Provider store={chartStore}>
<SpecsParser>
<BarSeries
id="bars"
xAccessor={0}
yAccessors={[1]}
data={[
[0, 1],
[1, 2],
]}
/>
</SpecsParser>
<ChartContainer getChartContainerRef={getChartContainerRef} forwardStageRef={chartStageRef} />
</Provider>
);
mount(component);
const state = chartStore.getState();
expect(state.specsInitialized).toBe(true);
expect(state.parentDimensions).toEqual({ width: 0, height: 0, top: 0, left: 0 });
chartStore.dispatch(updateParentDimensions({ width: 100, height: 100, top: 0, left: 0 }));
expect(chartStore.getState().parentDimensions).toEqual({ width: 100, height: 100, top: 0, left: 0 });
// passing the same parent dimmesion again can be triggered by the ResizeObserver
chartStore.dispatch(updateParentDimensions({ width: 100, height: 100, top: 0, left: 0 }));
expect(chartStore.getState().chartRendered).toBe(true);

// trigger also with just differences in top/left
chartStore.dispatch(updateParentDimensions({ width: 100, height: 100, top: 1, left: 1 }));
expect(chartStore.getState().chartRendered).toBe(true);
});
});
4 changes: 4 additions & 0 deletions packages/charts/src/state/chart_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { TooltipInfo } from '../components/tooltip/types';
import { DEFAULT_SETTINGS_SPEC, PointerEvent, Spec, TooltipValue } from '../specs';
import { keepDistinct } from '../utils/common';
import { Dimensions } from '../utils/dimensions';
import { deepEqual } from '../utils/fast_deep_equal';
import { Logger } from '../utils/logger';
import { Point } from '../utils/point';

Expand Down Expand Up @@ -388,6 +389,9 @@ export const chartStoreReducer = (chartId: string, title?: string, description?:
chartRenderedCount,
};
case UPDATE_PARENT_DIMENSION:
if (deepEqual(action.dimensions, state.parentDimensions)) {
return state;
}
return {
...state,
interactions: {
Expand Down

0 comments on commit c3aba88

Please sign in to comment.