Skip to content

Commit

Permalink
fix bug (#72809)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Jul 23, 2020
1 parent 119cb10 commit 5cdd080
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,8 @@ describe('editor_frame', () => {
expect(mockVisualization2.initialize).toHaveBeenCalledWith(
expect.objectContaining({
datasourceLayers: expect.objectContaining({ first: mockDatasource.publicAPIMock }),
})
}),
undefined
);
expect(mockVisualization2.getConfiguration).toHaveBeenCalledWith(
expect.objectContaining({ state: { initial: true } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,34 @@ describe('chart_switch', () => {
expect(frame.removeLayers).not.toHaveBeenCalled();
});

it('should not remove layers and initialize with existing state when switching between subtypes without data', () => {
const dispatch = jest.fn();
const frame = mockFrame(['a']);
frame.datasourceLayers.a.getTableSpec = jest.fn().mockReturnValue([]);
const visualizations = mockVisualizations();
visualizations.visC.getSuggestions = jest.fn().mockReturnValue([]);
visualizations.visC.switchVisualizationType = jest.fn(() => 'switched');

const component = mount(
<ChartSwitch
visualizationId="visC"
visualizationState={{ type: 'subvisC1' }}
visualizationMap={visualizations}
dispatch={dispatch}
framePublicAPI={frame}
datasourceMap={mockDatasourceMap()}
datasourceStates={mockDatasourceStates()}
/>
);

switchTo('subvisC3', component);

expect(visualizations.visC.switchVisualizationType).toHaveBeenCalledWith('subvisC3', {
type: 'subvisC1',
});
expect(frame.removeLayers).not.toHaveBeenCalled();
});

it('should switch to the updated datasource state', () => {
const dispatch = jest.fn();
const visualizations = mockVisualizations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,16 @@ export function ChartSwitch(props: Props) {
: () => {
return switchVisType(
subVisualizationId,
newVisualization.initialize(props.framePublicAPI)
newVisualization.initialize(
props.framePublicAPI,
props.visualizationId === newVisualization.id ? props.visualizationState : undefined
)
);
},
keptLayerIds: topSuggestion ? topSuggestion.keptLayerIds : [],
datasourceState: topSuggestion ? topSuggestion.datasourceState : undefined,
datasourceId: topSuggestion ? topSuggestion.datasourceId : undefined,
sameDatasources: dataLoss === 'nothing' && props.visualizationId === newVisualization.id,
};
}

Expand Down
33 changes: 33 additions & 0 deletions x-pack/plugins/lens/public/xy_visualization/xy_suggestions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,39 @@ describe('xy_suggestions', () => {
expect(suggestion.hide).toBeTruthy();
});

test('keeps existing seriesType for initial tables', () => {
const currentState: XYState = {
legend: { isVisible: true, position: 'bottom' },
fittingFunction: 'None',
preferredSeriesType: 'line',
layers: [
{
accessors: [],
layerId: 'first',
seriesType: 'line',
splitAccessor: undefined,
xAccessor: '',
},
],
};
const suggestions = getSuggestions({
table: {
isMultiRow: true,
columns: [numCol('price'), dateCol('date')],
layerId: 'first',
changeType: 'initial',
},
state: currentState,
keptLayerIds: ['first'],
});

expect(suggestions).toHaveLength(1);

expect(suggestions[0].hide).toEqual(false);
expect(suggestions[0].state.preferredSeriesType).toEqual('line');
expect(suggestions[0].state.layers[0].seriesType).toEqual('line');
});

test('makes a visible seriesType suggestion for unchanged table without split', () => {
const currentState: XYState = {
legend: { isVisible: true, position: 'bottom' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,7 @@ function getSeriesType(
return closestSeriesType.startsWith('bar') ? closestSeriesType : defaultType;
}

if (changeType === 'initial') {
return defaultType;
}

return closestSeriesType !== defaultType ? closestSeriesType : defaultType;
return closestSeriesType;
}

function getSuggestionTitle(
Expand Down

0 comments on commit 5cdd080

Please sign in to comment.