Skip to content

Commit

Permalink
Added tests for the new behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed May 10, 2022
1 parent 024ffe7 commit 9abcfec
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { expressionsPluginMock } from '@kbn/expressions-plugin/public/mocks';
import { mockDataPlugin, mountWithProvider } from '../../mocks';
import { setState } from '../../state_management';
import { getLensInspectorService } from '../../lens_inspector_service';
import { toExpression } from '@kbn/interpreter';

function generateSuggestion(state = {}): DatasourceSuggestion {
return {
Expand Down Expand Up @@ -208,10 +209,20 @@ describe('editor_frame', () => {
it('should render the resulting expression using the expression renderer', async () => {
mockDatasource.getLayers.mockReturnValue(['first']);

const props = {
const props: EditorFrameProps = {
...getDefaultProps(),
visualizationMap: {
testVis: { ...mockVisualization, toExpression: () => 'testVis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpression({
type: 'expression',
chain: [
...(datasourceExpressionsByLayers.first?.chain ?? []),
{ type: 'function', function: 'testVis', arguments: {} },
],
}),
},
},
datasourceMap: {
testDatasource: {
Expand Down Expand Up @@ -240,9 +251,10 @@ describe('editor_frame', () => {

instance.update();

expect(instance.find(expressionRendererMock).prop('expression')).toMatchInlineSnapshot(
`"testVis"`
);
expect(instance.find(expressionRendererMock).prop('expression')).toMatchInlineSnapshot(`
"datasource
| testVis"
`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import { getLensInspectorService } from '../../../lens_inspector_service';
import { inspectorPluginMock } from '@kbn/inspector-plugin/public/mocks';
import { disableAutoApply, enableAutoApply } from '../../../state_management/lens_slice';
import { Ast, toExpression } from '@kbn/interpreter';

const defaultPermissions: Record<string, Record<string, boolean | Record<string, boolean>>> = {
navLinks: { management: true },
Expand Down Expand Up @@ -72,6 +73,19 @@ const defaultProps = {
toggleFullscreen: jest.fn(),
};

const toExpr = (
datasourceExpressionsByLayers: Record<string, Ast>,
fn: string = 'testVis',
layerId: string = 'first'
) =>
toExpression({
type: 'expression',
chain: [
...(datasourceExpressionsByLayers[layerId]?.chain ?? []),
{ type: 'function', function: fn, arguments: {} },
],
});

const SELECTORS = {
applyChangesButton: 'button[data-test-subj="lnsApplyChanges__toolbar"]',
dragDropPrompt: '[data-test-subj="workspace-drag-drop-prompt"]',
Expand Down Expand Up @@ -147,7 +161,11 @@ describe('workspace_panel', () => {
<WorkspacePanel
{...defaultProps}
visualizationMap={{
testVis: { ...mockVisualization, toExpression: () => 'testVis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpr(datasourceExpressionsByLayers),
},
}}
/>,

Expand Down Expand Up @@ -176,7 +194,11 @@ describe('workspace_panel', () => {
}}
framePublicAPI={framePublicAPI}
visualizationMap={{
testVis: { ...mockVisualization, toExpression: () => 'testVis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpr(datasourceExpressionsByLayers),
},
}}
ExpressionRenderer={expressionRendererMock}
/>
Expand All @@ -186,9 +208,10 @@ describe('workspace_panel', () => {

instance.update();

expect(instance.find(expressionRendererMock).prop('expression')).toMatchInlineSnapshot(
`"testVis"`
);
expect(instance.find(expressionRendererMock).prop('expression')).toMatchInlineSnapshot(`
"datasource
| testVis"
`);
});

it('should give user control when auto-apply disabled', async () => {
Expand All @@ -207,7 +230,11 @@ describe('workspace_panel', () => {
}}
framePublicAPI={framePublicAPI}
visualizationMap={{
testVis: { ...mockVisualization, toExpression: () => 'testVis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpr(datasourceExpressionsByLayers),
},
}}
ExpressionRenderer={expressionRendererMock}
/>,
Expand All @@ -224,41 +251,61 @@ describe('workspace_panel', () => {
const getExpression = () => instance.find(expressionRendererMock).prop('expression');

// allows initial render
expect(getExpression()).toMatchInlineSnapshot(`"testVis"`);
expect(getExpression()).toMatchInlineSnapshot(`
"datasource
| testVis"
`);

mockDatasource.toExpression.mockReturnValue('new-datasource');
act(() => {
instance.setProps({
visualizationMap: {
testVis: { ...mockVisualization, toExpression: () => 'new-vis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpr(datasourceExpressionsByLayers, 'new-vis'),
} as Visualization,
},
});
});
instance.update();

expect(getExpression()).toMatchInlineSnapshot(`"testVis"`);
expect(getExpression()).toMatchInlineSnapshot(`
"datasource
| testVis"
`);

act(() => {
mounted.lensStore.dispatch(applyChanges());
});
instance.update();

// should update
expect(getExpression()).toMatchInlineSnapshot(`"new-vis"`);
expect(getExpression()).toMatchInlineSnapshot(`
"new-datasource
| new-vis"
`);

mockDatasource.toExpression.mockReturnValue('other-new-datasource');
act(() => {
instance.setProps({
visualizationMap: {
testVis: { ...mockVisualization, toExpression: () => 'other-new-vis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpr(datasourceExpressionsByLayers, 'other-new-vis'),
} as Visualization,
},
});
mounted.lensStore.dispatch(enableAutoApply());
});
instance.update();

// reenabling auto-apply triggers an update as well
expect(getExpression()).toMatchInlineSnapshot(`"other-new-vis"`);
expect(getExpression()).toMatchInlineSnapshot(`
"other-new-datasource
| other-new-vis"
`);
});

it('should base saveability on working changes when auto-apply disabled', async () => {
Expand Down Expand Up @@ -286,7 +333,11 @@ describe('workspace_panel', () => {
}}
framePublicAPI={framePublicAPI}
visualizationMap={{
testVis: { ...mockVisualization, toExpression: () => 'testVis' },
testVis: {
...mockVisualization,
toExpression: (state, datasourceLayers, attrs, datasourceExpressionsByLayers = {}) =>
toExpr(datasourceExpressionsByLayers),
},
}}
ExpressionRenderer={expressionRendererMock}
/>
Expand All @@ -298,9 +349,10 @@ describe('workspace_panel', () => {
instance.update();

// allows initial render
expect(instance.find(expressionRendererMock).prop('expression')).toMatchInlineSnapshot(
`"testVis"`
);
expect(instance.find(expressionRendererMock).prop('expression')).toMatchInlineSnapshot(`
"datasource
| testVis"
`);
expect(isSaveable()).toBe(true);

act(() => {
Expand Down

0 comments on commit 9abcfec

Please sign in to comment.