From 005cf6b0d6a9943992e5f6a32797758d00ce85cc Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Mon, 6 Jul 2020 11:47:53 +0200 Subject: [PATCH] fix: set uninitialized state when removeSpec action is called This commit reset the chart state to not initialized when removing a spec. After #723 PR that reduced the number of steps on the state machine when parsing, the removeSpec action wasn't accounted on that refactoring causing side effects when removing/switching a spec on the chart configuration due to the state being in an wrong status. fix #738 --- .../xy_chart/state/chart_state.specs.test.ts | 40 +++++++++++++++++-- src/state/chart_state.ts | 3 ++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/chart_types/xy_chart/state/chart_state.specs.test.ts b/src/chart_types/xy_chart/state/chart_state.specs.test.ts index 24699fd54e..111d5a81e5 100644 --- a/src/chart_types/xy_chart/state/chart_state.specs.test.ts +++ b/src/chart_types/xy_chart/state/chart_state.specs.test.ts @@ -17,11 +17,13 @@ * under the License. */ -import { createStore, Store } from 'redux'; +import { Store } from 'redux'; import { MockSeriesSpec } from '../../../mocks/specs'; import { MockStore } from '../../../mocks/store'; -import { GlobalChartState, chartStoreReducer } from '../../../state/chart_state'; +import { removeSpec, specParsed, upsertSpec } from '../../../state/actions/specs'; +import { GlobalChartState } from '../../../state/chart_state'; +import { getInternalIsInitializedSelector, InitStatus } from '../../../state/selectors/get_internal_is_intialized'; import { getLegendItemsSelector } from '../../../state/selectors/get_legend_items'; const data = [ @@ -32,8 +34,7 @@ const data = [ describe('XYChart - specs ordering', () => { let store: Store; beforeEach(() => { - const storeReducer = chartStoreReducer('chartId'); - store = createStore(storeReducer); + store = MockStore.default({ width: 100, height: 100, left: 0, top: 0 }); }); it('the legend respect the insert [A, B, C] order', () => { @@ -98,4 +99,35 @@ describe('XYChart - specs ordering', () => { names = [...legendItems.values()].map((item) => item.label); expect(names).toEqual(['B', 'A', 'C']); }); + it('The status should switch to not initialized removing a spec', () => { + MockStore.addSpecs([ + MockSeriesSpec.bar({ id: 'A', data }), + MockSeriesSpec.bar({ id: 'B', data }), + MockSeriesSpec.bar({ id: 'C', data }), + ], store); + expect(getInternalIsInitializedSelector(store.getState())).toBe(InitStatus.Initialized); + // check on remove + store.dispatch(removeSpec('A')); + expect(getInternalIsInitializedSelector(store.getState())).not.toBe(InitStatus.Initialized); + + // initialized again after specParsed action + store.dispatch(specParsed()); + expect(getInternalIsInitializedSelector(store.getState())).toBe(InitStatus.Initialized); + }); + it('The status should switch to not initialized when upserting a spec', () => { + MockStore.addSpecs([ + MockSeriesSpec.bar({ id: 'A', data }), + MockSeriesSpec.bar({ id: 'B', data }), + MockSeriesSpec.bar({ id: 'C', data }), + ], store); + expect(getInternalIsInitializedSelector(store.getState())).toBe(InitStatus.Initialized); + + // check on upsert + store.dispatch(upsertSpec(MockSeriesSpec.bar({ id: 'D', data }))); + expect(getInternalIsInitializedSelector(store.getState())).not.toBe(InitStatus.Initialized); + + // initialized again after specParsed action + store.dispatch(specParsed()); + expect(getInternalIsInitializedSelector(store.getState())).toBe(InitStatus.Initialized); + }); }); diff --git a/src/state/chart_state.ts b/src/state/chart_state.ts index 1212400a12..b09f5068af 100644 --- a/src/state/chart_state.ts +++ b/src/state/chart_state.ts @@ -320,6 +320,9 @@ export const chartStoreReducer = (chartId: string) => { const { [action.id]: specToRemove, ...rest } = state.specs; return { ...state, + specsInitialized: false, + chartRendered: false, + specParsing: true, specs: { ...rest, },