Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ChartStatus): render complete if same parent size is dispatched #2534

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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