Skip to content

Commit

Permalink
fix tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Aug 20, 2020
1 parent a898781 commit a343e44
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 115 deletions.
17 changes: 14 additions & 3 deletions x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ describe('Lens App', () => {
query: 'fake query',
filters: [],
},
references: [],
});
await act(async () => {
instance.setProps({ docId: '1234' });
Expand Down Expand Up @@ -1057,6 +1058,7 @@ describe('Lens App', () => {
query: 'kuery',
filters: [],
},
references: [],
} as jest.ResolvedValue<Document>);
});

Expand Down Expand Up @@ -1088,7 +1090,11 @@ describe('Lens App', () => {
act(() =>
onChange({
filterableIndexPatterns: [],
doc: ({ id: undefined } as unknown) as Document,
doc: ({
id: undefined,

references: [],
} as unknown) as Document,
isSaveable: true,
})
);
Expand Down Expand Up @@ -1135,7 +1141,11 @@ describe('Lens App', () => {
act(() =>
onChange({
filterableIndexPatterns: [],
doc: ({ id: '1234' } as unknown) as Document,
doc: ({
id: '1234',

references: [],
} as unknown) as Document,
isSaveable: true,
})
);
Expand Down Expand Up @@ -1163,6 +1173,7 @@ describe('Lens App', () => {
doc: ({
id: '1234',
title: 'My cool doc',
references: [],
state: {
query: 'kuery',
filters: [],
Expand Down Expand Up @@ -1192,7 +1203,7 @@ describe('Lens App', () => {
act(() =>
onChange({
filterableIndexPatterns: [],
doc: ({ id: '1234' } as unknown) as Document,
doc: ({ id: '1234', references: [] } as unknown) as Document,
isSaveable: true,
})
);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export function App({
indexPatternsForTopNav: indexPatterns,
}));
})
.catch(() => {
.catch((e) => {
setState((s) => ({ ...s, isLoading: false }));

redirectTo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,21 +422,6 @@ describe('editor_frame', () => {
"function": "kibana",
"type": "function",
},
Object {
"arguments": Object {
"filters": Array [
"[]",
],
"query": Array [
"{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"}",
],
"timeRange": Array [
"{\\"from\\":\\"\\",\\"to\\":\\"\\"}",
],
},
"function": "kibana_context",
"type": "function",
},
Object {
"arguments": Object {
"layerIds": Array [
Expand Down Expand Up @@ -529,21 +514,6 @@ describe('editor_frame', () => {
"function": "kibana",
"type": "function",
},
Object {
"arguments": Object {
"filters": Array [
"[]",
],
"query": Array [
"{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"}",
],
"timeRange": Array [
"{\\"from\\":\\"\\",\\"to\\":\\"\\"}",
],
},
"function": "kibana_context",
"type": "function",
},
Object {
"arguments": Object {
"layerIds": Array [
Expand Down Expand Up @@ -1447,7 +1417,7 @@ describe('editor_frame', () => {
mockDatasource.getLayers.mockReturnValue(['first']);
mockDatasource.getPersistableState = jest.fn((x) => ({
state: x,
savedObjectReferences: [{ type: 'index-pattern', id: '1', name: '' }],
savedObjectReferences: [{ type: 'index-pattern', id: '1', name: 'index-pattern-0' }],
}));
mockVisualization.initialize.mockReturnValue({ initialState: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React, { useEffect, useReducer } from 'react';
import { CoreSetup, CoreStart } from 'kibana/public';
import { ReactExpressionRendererType } from '../../../../../../src/plugins/expressions/public';
import { Datasource, DatasourcePublicAPI, FramePublicAPI, Visualization } from '../../types';
import { Datasource, FramePublicAPI, Visualization } from '../../types';
import { reducer, getInitialState } from './state_management';
import { DataPanelWrapper } from './data_panel_wrapper';
import { ConfigPanelWrapper } from './config_panel';
Expand Down Expand Up @@ -63,20 +63,19 @@ export function EditorFrame(props: EditorFrameProps) {
// prevents executing dispatch on unmounted component
let isUnmounted = false;
if (!allLoaded) {
initializeDatasources(
props.datasourceMap,
state.datasourceStates,
(datasourceId, datasourceState) => {
initializeDatasources(props.datasourceMap, state.datasourceStates, props.doc?.references)
.then((result) => {
if (!isUnmounted) {
dispatch({
type: 'UPDATE_DATASOURCE_STATE',
updater: datasourceState,
datasourceId,
Object.entries(result).forEach(([datasourceId, { state: datasourceState }]) => {
dispatch({
type: 'UPDATE_DATASOURCE_STATE',
updater: datasourceState,
datasourceId,
});
});
}
},
onError
);
})
.catch(onError);
}
return () => {
isUnmounted = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export function getSavedObjectFormat({
visualizationState: state.visualization.state,
datasourceMap: activeDatasources,
datasourceStates: state.datasourceStates,
framePublicAPI,
removeDateRange: true,
datasourceLayers: framePublicAPI.datasourceLayers,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import { Ast } from '@kbn/interpreter/common';
import { Datasource, DatasourcePublicAPI, Visualization } from '../../types';
import { buildExpression } from './expression_helpers';

export function initializeDatasources(
export async function initializeDatasources(
datasourceMap: Record<string, Datasource>,
datasourceStates: Record<string, { state: unknown; isLoading: boolean }>,
// TODO turn this into a single return value via promise
onInit: (id: string, state: unknown) => void,
onError: (e: { message: string }) => void,
references?: SavedObjectReference[]
) {
Object.entries(datasourceMap).forEach(([datasourceId, datasource]) => {
if (datasourceStates[datasourceId] && datasourceStates[datasourceId].isLoading) {
datasource
.initialize(datasourceStates[datasourceId].state || undefined, references)
.then((datasourceState) => {
onInit(datasourceId, datasourceState);
})
.catch(onError);
}
});
): Promise<Record<string, { isLoading: boolean; state: unknown }>> {
const states: Record<string, { isLoading: boolean; state: unknown }> = {};
await Promise.all(
Object.entries(datasourceMap).map(([datasourceId, datasource]) => {
if (datasourceStates[datasourceId]) {
return datasource
.initialize(datasourceStates[datasourceId].state || undefined, references)
.then((datasourceState) => {
states[datasourceId] = { isLoading: false, state: datasourceState };
});
}
})
);
return states;
}

export function createDatasourceLayers(
Expand Down Expand Up @@ -58,18 +58,15 @@ export async function persistedStateToExpression(
persistedDatasourceStates: Record<string, unknown>,
references?: SavedObjectReference[]
): Promise<Ast> {
const datasourceStates: Record<string, { isLoading: boolean; state: unknown }> = {};

await Promise.all(
Object.entries(datasources).map(([datasourceId, datasource]) => {
if (persistedDatasourceStates[datasourceId]) {
return datasource
.initialize(persistedDatasourceStates[datasourceId], references)
.then((datasourceState) => {
datasourceStates[datasourceId] = { isLoading: false, state: datasourceState };
});
}
})
const datasourceStates = await initializeDatasources(
datasources,
Object.fromEntries(
Object.entries(persistedDatasourceStates).map(([id, state]) => [
id,
{ isLoading: false, state },
])
),
references
);

const datasourceLayers = createDatasourceLayers(datasources, datasourceStates);
Expand All @@ -80,5 +77,5 @@ export async function persistedStateToExpression(
datasourceMap: datasources,
datasourceStates,
datasourceLayers,
});
})!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ describe('suggestion_panel', () => {

expect(passedExpression).toMatchInlineSnapshot(`
"kibana
| kibana_context timeRange=\\"{\\\\\\"from\\\\\\":\\\\\\"now-7d\\\\\\",\\\\\\"to\\\\\\":\\\\\\"now\\\\\\"}\\" query=\\"{\\\\\\"query\\\\\\":\\\\\\"\\\\\\",\\\\\\"language\\\\\\":\\\\\\"lucene\\\\\\"}\\" filters=\\"[{\\\\\\"meta\\\\\\":{\\\\\\"index\\\\\\":\\\\\\"index1\\\\\\"},\\\\\\"exists\\\\\\":{\\\\\\"field\\\\\\":\\\\\\"myfield\\\\\\"}}]\\"
| lens_merge_tables layerIds=\\"first\\" tables={datasource_expression}
| test
| expression"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
ReactExpressionRendererProps,
ReactExpressionRendererType,
} from '../../../../../../src/plugins/expressions/public';
import { prependDatasourceExpression, prependKibanaContext } from './expression_helpers';
import { prependDatasourceExpression } from './expression_helpers';
import { debouncedComponent } from '../../debounced_component';
import { trackUiEvent, trackSuggestionEvent } from '../../lens_ui_telemetry';
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
Expand Down Expand Up @@ -265,15 +265,6 @@ export function SuggestionPanel({
}
}

const expressionContext = {
query: frame.query,
filters: frame.filters,
timeRange: {
from: frame.dateRange.fromDate,
to: frame.dateRange.toDate,
},
};

return (
<div className="lnsSuggestionPanel">
<EuiFlexGroup alignItems="center">
Expand Down Expand Up @@ -318,9 +309,7 @@ export function SuggestionPanel({
{currentVisualizationId && (
<SuggestionPreview
preview={{
expression: currentStateExpression
? prependKibanaContext(currentStateExpression, expressionContext)
: undefined,
expression: currentStateExpression!,
icon:
visualizationMap[currentVisualizationId].getDescription(currentVisualizationState)
.icon || 'empty',
Expand All @@ -338,9 +327,7 @@ export function SuggestionPanel({
return (
<SuggestionPreview
preview={{
expression: suggestion.previewExpression
? prependKibanaContext(suggestion.previewExpression, expressionContext)
: undefined,
expression: suggestion.previewExpression!,
icon: suggestion.previewIcon,
title: suggestion.title,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,6 @@ describe('workspace_panel', () => {
"function": "kibana",
"type": "function",
},
Object {
"arguments": Object {
"filters": Array [
"[]",
],
"query": Array [
"{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"}",
],
"timeRange": Array [
"{\\"from\\":\\"now-7d\\",\\"to\\":\\"now\\"}",
],
},
"function": "kibana_context",
"type": "function",
},
Object {
"arguments": Object {
"layerIds": Array [
Expand Down Expand Up @@ -305,10 +290,10 @@ describe('workspace_panel', () => {
);

expect(
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[2].arguments.layerIds
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[1].arguments.layerIds
).toEqual(['first', 'second', 'third']);
expect(
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[2].arguments.tables
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[1].arguments.tables
).toMatchInlineSnapshot(`
Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function InnerWorkspacePanel({
visualizationState,
datasourceMap,
datasourceStates,
framePublicAPI,
datasourceLayers: framePublicAPI.datasourceLayers,
});
} catch (e) {
// Most likely an error in the expression provided by a datasource or visualization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('embeddable', () => {

expect(expressionRenderer.mock.calls[0][0].searchContext).toEqual({
timeRange,
query,
query: [query, savedVis.state.query],
filters,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class EditorFrameService {
coreHttp: coreStart.http,
timefilter: deps.data.query.timefilter.timefilter,
expressionRenderer: deps.expressions.ReactExpressionRenderer,
documentToExpression: this.documentToExpression,
documentToExpression: this.documentToExpression.bind(this),
indexPatternService: deps.data.indexPatterns,
uiActions: deps.uiActions,
datasources: resolvedDatasources,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/server/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { cloneDeep, uniqBy } from 'lodash';
import { cloneDeep } from 'lodash';
import { fromExpression, toExpression, Ast, ExpressionFunctionAST } from '@kbn/interpreter/common';
import {
SavedObjectMigrationMap,
Expand Down

0 comments on commit a343e44

Please sign in to comment.