Skip to content

Commit

Permalink
Update tests and add keptLayerIds everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Oct 28, 2019
1 parent a51e1e5 commit 289c48d
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe('chart_switch', () => {
layerId: 'a',
changeType: 'unchanged',
},
keptLayerIds: ['a'],
},
]);
return {
Expand Down Expand Up @@ -219,6 +220,7 @@ describe('chart_switch', () => {
isMultiRow: true,
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);
datasourceMap.testDatasource.publicAPIMock.getTableSpec.mockReturnValue([
Expand Down Expand Up @@ -332,31 +334,7 @@ describe('chart_switch', () => {
expect(getMenuItem('subvisC2', component).prop('betaBadgeIconType')).toBeUndefined();
});

it('should remove unused layers', () => {
const removeLayers = jest.fn();
const frame = {
...mockFrame(['a', 'b', 'c']),
removeLayers,
};
const component = mount(
<ChartSwitch
visualizationId="visA"
visualizationState={{}}
visualizationMap={mockVisualizations()}
dispatch={jest.fn()}
framePublicAPI={frame}
datasourceMap={mockDatasourceMap()}
datasourceStates={mockDatasourceStates()}
/>
);

switchTo('subvisB', component);

expect(removeLayers).toHaveBeenCalledTimes(1);
expect(removeLayers).toHaveBeenCalledWith(['b', 'c']);
});

it('should remove all layers if there is no suggestion', () => {
it('should switch even if there is no suggestion', () => {
const dispatch = jest.fn();
const visualizations = mockVisualizations();
visualizations.visB.getSuggestions.mockReturnValueOnce([]);
Expand All @@ -376,17 +354,23 @@ describe('chart_switch', () => {

switchTo('subvisB', component);

expect(frame.removeLayers).toHaveBeenCalledTimes(1);
expect(frame.removeLayers).toHaveBeenCalledWith(['a', 'b', 'c']);
expect(visualizations.visB.getSuggestions).toHaveBeenCalledWith(
expect.objectContaining({
keptLayerIds: ['a'],
})
);

expect(dispatch).toHaveBeenCalledWith(
expect.objectContaining({
type: 'SWITCH_VISUALIZATION',
initialState: 'visB initial state',
})
);
});

it('should not remove layers if the visualization is not changing', () => {
const dispatch = jest.fn();
const removeLayers = jest.fn();
const frame = {
...mockFrame(['a', 'b', 'c']),
removeLayers,
};
const frame = mockFrame(['a', 'b', 'c']);
const visualizations = mockVisualizations();
const switchVisualizationType = jest.fn(() => 'therebedragons');

Expand All @@ -405,7 +389,6 @@ describe('chart_switch', () => {
);

switchTo('subvisC2', component);
expect(removeLayers).not.toHaveBeenCalled();
expect(switchVisualizationType).toHaveBeenCalledWith('subvisC2', 'therebegriffins');
expect(dispatch).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -447,6 +430,7 @@ describe('chart_switch', () => {
isMultiRow: true,
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function generateSuggestion(state = {}): DatasourceSuggestion {
layerId: 'first',
changeType: 'unchanged',
},
keptLayerIds: ['first'],
};
}

Expand Down Expand Up @@ -917,6 +918,7 @@ describe('editor_frame', () => {
layerId: 'first',
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);

Expand Down Expand Up @@ -1062,6 +1064,7 @@ describe('editor_frame', () => {
isMultiRow: true,
layerId: 'first',
},
keptLayerIds: [],
},
]);
mount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const generateSuggestion = (state = {}, layerId: string = 'first'): DatasourceSu
layerId,
changeType: 'unchanged',
},
keptLayerIds: [layerId],
});

let datasourceMap: Record<string, DatasourceMock>;
Expand Down Expand Up @@ -235,8 +236,8 @@ describe('suggestion helpers', () => {
changeType: 'unchanged',
};
datasourceMap.mock.getDatasourceSuggestionsFromCurrentState.mockReturnValue([
{ state: {}, table: table1 },
{ state: {}, table: table2 },
{ state: {}, table: table1, keptLayerIds: ['first'] },
{ state: {}, table: table2, keptLayerIds: ['first'] },
]);
getSuggestions({
visualizationMap: {
Expand Down Expand Up @@ -343,45 +344,4 @@ describe('suggestion helpers', () => {
})
);
});

it('should drop other layers only on visualization switch', () => {
const mockVisualization1 = createMockVisualization();
const mockVisualization2 = createMockVisualization();
datasourceMap.mock.getDatasourceSuggestionsFromCurrentState.mockReturnValue([
generateSuggestion(),
]);
datasourceMap.mock.getLayers.mockReturnValue(['first', 'second']);
const suggestions = getSuggestions({
visualizationMap: {
vis1: {
...mockVisualization1,
getSuggestions: () => [
{
score: 0.8,
title: 'Test2',
state: {},
previewIcon: 'empty',
},
],
},
vis2: {
...mockVisualization2,
getSuggestions: () => [
{
score: 0.6,
title: 'Test3',
state: {},
previewIcon: 'empty',
},
],
},
},
activeVisualizationId: 'vis1',
visualizationState: {},
datasourceMap,
datasourceStates,
});
expect(suggestions[0].keptLayerIds).toEqual(['first', 'second']);
expect(suggestions[1].keptLayerIds).toEqual(['first']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface Suggestion {
visualizationId: string;
datasourceState?: unknown;
datasourceId?: string;
// keptLayerIds: string[];
columns: number;
score: number;
title: string;
Expand All @@ -30,6 +29,7 @@ export interface Suggestion {
previewIcon: IconType;
hide?: boolean;
changeType: TableChangeType;
keptLayerIds: string[];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,36 +218,6 @@ describe('suggestion_panel', () => {
);
});

it('should remove unused layers if suggestion is clicked', () => {
defaultProps.frame.datasourceLayers.a = mockDatasource.publicAPIMock;
defaultProps.frame.datasourceLayers.b = mockDatasource.publicAPIMock;
const wrapper = mount(
<SuggestionPanel
{...defaultProps}
stagedPreview={{ visualization: { state: {}, activeId: 'vis' }, datasourceStates: {} }}
activeVisualizationId="vis2"
/>
);

act(() => {
wrapper
.find('button[data-test-subj="lnsSuggestion"]')
.at(1)
.simulate('click');
});

wrapper.update();

act(() => {
wrapper
.find('[data-test-subj="lensSubmitSuggestion"]')
.first()
.simulate('click');
});

expect(defaultProps.frame.removeLayers).toHaveBeenCalledWith(['b']);
});

it('should render preview expression if there is one', () => {
mockDatasource.getLayers.mockReturnValue(['first']);
(getSuggestions as jest.Mock).mockReturnValue([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ describe('workspace_panel', () => {
{
state: {},
table: expectedTable,
keptLayerIds: [],
},
]);
mockVisualization.getSuggestions.mockReturnValueOnce([
Expand Down Expand Up @@ -613,6 +614,7 @@ describe('workspace_panel', () => {
columns: [],
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);
mockVisualization.getSuggestions.mockReturnValueOnce([
Expand All @@ -639,6 +641,7 @@ describe('workspace_panel', () => {
columns: [],
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);
mockVisualization2.getSuggestions.mockReturnValueOnce([
Expand All @@ -665,6 +668,7 @@ describe('workspace_panel', () => {
columns: [],
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);
mockVisualization.getSuggestions.mockReturnValueOnce([
Expand Down Expand Up @@ -694,6 +698,7 @@ describe('workspace_panel', () => {
layerId: '1',
changeType: 'unchanged',
},
keptLayerIds: [],
},
{
state: {},
Expand All @@ -703,6 +708,7 @@ describe('workspace_panel', () => {
layerId: '1',
changeType: 'unchanged',
},
keptLayerIds: [],
},
]);
mockVisualization.getSuggestions.mockReturnValueOnce([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,38 @@ describe('IndexPattern Data Source suggestions', () => {
})
);
});

it('creates a new layer if no match is found', () => {
const suggestions = getDatasourceSuggestionsForField(stateWithEmptyLayer(), '2', {
name: 'source',
type: 'string',
aggregatable: true,
searchable: true,
});

expect(suggestions).toContainEqual(
expect.objectContaining({
state: expect.objectContaining({
layers: {
previousLayer: expect.objectContaining({
indexPatternId: '1',
}),
id1: expect.objectContaining({
indexPatternId: '2',
}),
},
}),
table: {
changeType: 'initial',
label: undefined,
isMultiRow: true,
columns: expect.arrayContaining([]),
layerId: 'id1',
},
keptLayerIds: ['previousLayer'],
})
);
});
});

describe('suggesting extensions to non-empty tables', () => {
Expand Down Expand Up @@ -979,12 +1011,25 @@ describe('IndexPattern Data Source suggestions', () => {
};

const result = getDatasourceSuggestionsFromCurrentState(state);

expect(result).toContainEqual(
expect.objectContaining({
table: {
table: expect.objectContaining({
isMultiRow: true,
changeType: 'unchanged',
label: undefined,
layerId: 'first',
}),
keptLayerIds: ['first', 'second'],
})
);

expect(result).toContainEqual(
expect.objectContaining({
table: {
isMultiRow: true,
changeType: 'layers',
label: 'Show only layer 1',
columns: [
{
columnId: 'col1',
Expand All @@ -1005,8 +1050,8 @@ describe('IndexPattern Data Source suggestions', () => {
expect.objectContaining({
table: {
isMultiRow: true,
changeType: 'unchanged',
label: undefined,
changeType: 'layers',
label: 'Show only layer 2',
columns: [
{
columnId: 'cola',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ export function getDatasourceSuggestionsForField(
const layerIds = layers.filter(id => state.layers[id].indexPatternId === indexPatternId);

if (layerIds.length === 0) {
// The field we're suggesting on does not match any existing layer. This will always add
// a new layer if possible, but that might not be desirable if the layers are too complicated
// already
// The field we're suggesting on does not match any existing layer. This will add
// a new layer.
return getEmptyLayerSuggestionsForField(state, generateId(), indexPatternId, field);
} else {
// The field we're suggesting on matches an existing layer. In this case we find the layer with
Expand Down Expand Up @@ -368,7 +367,7 @@ export function getDatasourceSuggestionsFromCurrentState(
): Array<DatasourceSuggestion<IndexPatternPrivateState>> {
const layers = Object.entries(state.layers || {});
if (layers.length > 1) {
// Return only two suggestions: each layer individually
// Return suggestions that reduce the data to each layer individually
return layers
.map(([layerId, layer], index) => {
const hasMatchingLayer = layers.some(
Expand Down
Loading

0 comments on commit 289c48d

Please sign in to comment.