Skip to content

Commit

Permalink
fix: set uninitialized state when removeSpec action is called (#739)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markov00 authored Jul 6, 2020
1 parent f23e7e2 commit 35b8caf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/chart_types/xy_chart/state/chart_state.specs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -32,8 +34,7 @@ const data = [
describe('XYChart - specs ordering', () => {
let store: Store<GlobalChartState>;
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', () => {
Expand Down Expand Up @@ -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);
});
});
3 changes: 3 additions & 0 deletions src/state/chart_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit 35b8caf

Please sign in to comment.