Skip to content

Commit a343e44

Browse files
committed
fix tests and types
1 parent a898781 commit a343e44

File tree

13 files changed

+62
-115
lines changed

13 files changed

+62
-115
lines changed

x-pack/plugins/lens/public/app_plugin/app.test.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ describe('Lens App', () => {
286286
query: 'fake query',
287287
filters: [],
288288
},
289+
references: [],
289290
});
290291
await act(async () => {
291292
instance.setProps({ docId: '1234' });
@@ -1057,6 +1058,7 @@ describe('Lens App', () => {
10571058
query: 'kuery',
10581059
filters: [],
10591060
},
1061+
references: [],
10601062
} as jest.ResolvedValue<Document>);
10611063
});
10621064

@@ -1088,7 +1090,11 @@ describe('Lens App', () => {
10881090
act(() =>
10891091
onChange({
10901092
filterableIndexPatterns: [],
1091-
doc: ({ id: undefined } as unknown) as Document,
1093+
doc: ({
1094+
id: undefined,
1095+
1096+
references: [],
1097+
} as unknown) as Document,
10921098
isSaveable: true,
10931099
})
10941100
);
@@ -1135,7 +1141,11 @@ describe('Lens App', () => {
11351141
act(() =>
11361142
onChange({
11371143
filterableIndexPatterns: [],
1138-
doc: ({ id: '1234' } as unknown) as Document,
1144+
doc: ({
1145+
id: '1234',
1146+
1147+
references: [],
1148+
} as unknown) as Document,
11391149
isSaveable: true,
11401150
})
11411151
);
@@ -1163,6 +1173,7 @@ describe('Lens App', () => {
11631173
doc: ({
11641174
id: '1234',
11651175
title: 'My cool doc',
1176+
references: [],
11661177
state: {
11671178
query: 'kuery',
11681179
filters: [],
@@ -1192,7 +1203,7 @@ describe('Lens App', () => {
11921203
act(() =>
11931204
onChange({
11941205
filterableIndexPatterns: [],
1195-
doc: ({ id: '1234' } as unknown) as Document,
1206+
doc: ({ id: '1234', references: [] } as unknown) as Document,
11961207
isSaveable: true,
11971208
})
11981209
);

x-pack/plugins/lens/public/app_plugin/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export function App({
271271
indexPatternsForTopNav: indexPatterns,
272272
}));
273273
})
274-
.catch(() => {
274+
.catch((e) => {
275275
setState((s) => ({ ...s, isLoading: false }));
276276

277277
redirectTo();

x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.test.tsx

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -422,21 +422,6 @@ describe('editor_frame', () => {
422422
"function": "kibana",
423423
"type": "function",
424424
},
425-
Object {
426-
"arguments": Object {
427-
"filters": Array [
428-
"[]",
429-
],
430-
"query": Array [
431-
"{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"}",
432-
],
433-
"timeRange": Array [
434-
"{\\"from\\":\\"\\",\\"to\\":\\"\\"}",
435-
],
436-
},
437-
"function": "kibana_context",
438-
"type": "function",
439-
},
440425
Object {
441426
"arguments": Object {
442427
"layerIds": Array [
@@ -529,21 +514,6 @@ describe('editor_frame', () => {
529514
"function": "kibana",
530515
"type": "function",
531516
},
532-
Object {
533-
"arguments": Object {
534-
"filters": Array [
535-
"[]",
536-
],
537-
"query": Array [
538-
"{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"}",
539-
],
540-
"timeRange": Array [
541-
"{\\"from\\":\\"\\",\\"to\\":\\"\\"}",
542-
],
543-
},
544-
"function": "kibana_context",
545-
"type": "function",
546-
},
547517
Object {
548518
"arguments": Object {
549519
"layerIds": Array [
@@ -1447,7 +1417,7 @@ describe('editor_frame', () => {
14471417
mockDatasource.getLayers.mockReturnValue(['first']);
14481418
mockDatasource.getPersistableState = jest.fn((x) => ({
14491419
state: x,
1450-
savedObjectReferences: [{ type: 'index-pattern', id: '1', name: '' }],
1420+
savedObjectReferences: [{ type: 'index-pattern', id: '1', name: 'index-pattern-0' }],
14511421
}));
14521422
mockVisualization.initialize.mockReturnValue({ initialState: true });
14531423

x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import React, { useEffect, useReducer } from 'react';
88
import { CoreSetup, CoreStart } from 'kibana/public';
99
import { ReactExpressionRendererType } from '../../../../../../src/plugins/expressions/public';
10-
import { Datasource, DatasourcePublicAPI, FramePublicAPI, Visualization } from '../../types';
10+
import { Datasource, FramePublicAPI, Visualization } from '../../types';
1111
import { reducer, getInitialState } from './state_management';
1212
import { DataPanelWrapper } from './data_panel_wrapper';
1313
import { ConfigPanelWrapper } from './config_panel';
@@ -63,20 +63,19 @@ export function EditorFrame(props: EditorFrameProps) {
6363
// prevents executing dispatch on unmounted component
6464
let isUnmounted = false;
6565
if (!allLoaded) {
66-
initializeDatasources(
67-
props.datasourceMap,
68-
state.datasourceStates,
69-
(datasourceId, datasourceState) => {
66+
initializeDatasources(props.datasourceMap, state.datasourceStates, props.doc?.references)
67+
.then((result) => {
7068
if (!isUnmounted) {
71-
dispatch({
72-
type: 'UPDATE_DATASOURCE_STATE',
73-
updater: datasourceState,
74-
datasourceId,
69+
Object.entries(result).forEach(([datasourceId, { state: datasourceState }]) => {
70+
dispatch({
71+
type: 'UPDATE_DATASOURCE_STATE',
72+
updater: datasourceState,
73+
datasourceId,
74+
});
7575
});
7676
}
77-
},
78-
onError
79-
);
77+
})
78+
.catch(onError);
8079
}
8180
return () => {
8281
isUnmounted = true;

x-pack/plugins/lens/public/editor_frame_service/editor_frame/save.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ export function getSavedObjectFormat({
5454
visualizationState: state.visualization.state,
5555
datasourceMap: activeDatasources,
5656
datasourceStates: state.datasourceStates,
57-
framePublicAPI,
58-
removeDateRange: true,
57+
datasourceLayers: framePublicAPI.datasourceLayers,
5958
});
6059

6160
return {

x-pack/plugins/lens/public/editor_frame_service/editor_frame/state_helpers.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ import { Ast } from '@kbn/interpreter/common';
99
import { Datasource, DatasourcePublicAPI, Visualization } from '../../types';
1010
import { buildExpression } from './expression_helpers';
1111

12-
export function initializeDatasources(
12+
export async function initializeDatasources(
1313
datasourceMap: Record<string, Datasource>,
1414
datasourceStates: Record<string, { state: unknown; isLoading: boolean }>,
15-
// TODO turn this into a single return value via promise
16-
onInit: (id: string, state: unknown) => void,
17-
onError: (e: { message: string }) => void,
1815
references?: SavedObjectReference[]
19-
) {
20-
Object.entries(datasourceMap).forEach(([datasourceId, datasource]) => {
21-
if (datasourceStates[datasourceId] && datasourceStates[datasourceId].isLoading) {
22-
datasource
23-
.initialize(datasourceStates[datasourceId].state || undefined, references)
24-
.then((datasourceState) => {
25-
onInit(datasourceId, datasourceState);
26-
})
27-
.catch(onError);
28-
}
29-
});
16+
): Promise<Record<string, { isLoading: boolean; state: unknown }>> {
17+
const states: Record<string, { isLoading: boolean; state: unknown }> = {};
18+
await Promise.all(
19+
Object.entries(datasourceMap).map(([datasourceId, datasource]) => {
20+
if (datasourceStates[datasourceId]) {
21+
return datasource
22+
.initialize(datasourceStates[datasourceId].state || undefined, references)
23+
.then((datasourceState) => {
24+
states[datasourceId] = { isLoading: false, state: datasourceState };
25+
});
26+
}
27+
})
28+
);
29+
return states;
3030
}
3131

3232
export function createDatasourceLayers(
@@ -58,18 +58,15 @@ export async function persistedStateToExpression(
5858
persistedDatasourceStates: Record<string, unknown>,
5959
references?: SavedObjectReference[]
6060
): Promise<Ast> {
61-
const datasourceStates: Record<string, { isLoading: boolean; state: unknown }> = {};
62-
63-
await Promise.all(
64-
Object.entries(datasources).map(([datasourceId, datasource]) => {
65-
if (persistedDatasourceStates[datasourceId]) {
66-
return datasource
67-
.initialize(persistedDatasourceStates[datasourceId], references)
68-
.then((datasourceState) => {
69-
datasourceStates[datasourceId] = { isLoading: false, state: datasourceState };
70-
});
71-
}
72-
})
61+
const datasourceStates = await initializeDatasources(
62+
datasources,
63+
Object.fromEntries(
64+
Object.entries(persistedDatasourceStates).map(([id, state]) => [
65+
id,
66+
{ isLoading: false, state },
67+
])
68+
),
69+
references
7370
);
7471

7572
const datasourceLayers = createDatasourceLayers(datasources, datasourceStates);
@@ -80,5 +77,5 @@ export async function persistedStateToExpression(
8077
datasourceMap: datasources,
8178
datasourceStates,
8279
datasourceLayers,
83-
});
80+
})!;
8481
}

x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ describe('suggestion_panel', () => {
249249

250250
expect(passedExpression).toMatchInlineSnapshot(`
251251
"kibana
252-
| kibana_context timeRange=\\"{\\\\\\"from\\\\\\":\\\\\\"now-7d\\\\\\",\\\\\\"to\\\\\\":\\\\\\"now\\\\\\"}\\" query=\\"{\\\\\\"query\\\\\\":\\\\\\"\\\\\\",\\\\\\"language\\\\\\":\\\\\\"lucene\\\\\\"}\\" filters=\\"[{\\\\\\"meta\\\\\\":{\\\\\\"index\\\\\\":\\\\\\"index1\\\\\\"},\\\\\\"exists\\\\\\":{\\\\\\"field\\\\\\":\\\\\\"myfield\\\\\\"}}]\\"
253252
| lens_merge_tables layerIds=\\"first\\" tables={datasource_expression}
254253
| test
255254
| expression"

x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
ReactExpressionRendererProps,
3030
ReactExpressionRendererType,
3131
} from '../../../../../../src/plugins/expressions/public';
32-
import { prependDatasourceExpression, prependKibanaContext } from './expression_helpers';
32+
import { prependDatasourceExpression } from './expression_helpers';
3333
import { debouncedComponent } from '../../debounced_component';
3434
import { trackUiEvent, trackSuggestionEvent } from '../../lens_ui_telemetry';
3535
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
@@ -265,15 +265,6 @@ export function SuggestionPanel({
265265
}
266266
}
267267

268-
const expressionContext = {
269-
query: frame.query,
270-
filters: frame.filters,
271-
timeRange: {
272-
from: frame.dateRange.fromDate,
273-
to: frame.dateRange.toDate,
274-
},
275-
};
276-
277268
return (
278269
<div className="lnsSuggestionPanel">
279270
<EuiFlexGroup alignItems="center">
@@ -318,9 +309,7 @@ export function SuggestionPanel({
318309
{currentVisualizationId && (
319310
<SuggestionPreview
320311
preview={{
321-
expression: currentStateExpression
322-
? prependKibanaContext(currentStateExpression, expressionContext)
323-
: undefined,
312+
expression: currentStateExpression!,
324313
icon:
325314
visualizationMap[currentVisualizationId].getDescription(currentVisualizationState)
326315
.icon || 'empty',
@@ -338,9 +327,7 @@ export function SuggestionPanel({
338327
return (
339328
<SuggestionPreview
340329
preview={{
341-
expression: suggestion.previewExpression
342-
? prependKibanaContext(suggestion.previewExpression, expressionContext)
343-
: undefined,
330+
expression: suggestion.previewExpression!,
344331
icon: suggestion.previewIcon,
345332
title: suggestion.title,
346333
}}

x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.test.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,6 @@ describe('workspace_panel', () => {
172172
"function": "kibana",
173173
"type": "function",
174174
},
175-
Object {
176-
"arguments": Object {
177-
"filters": Array [
178-
"[]",
179-
],
180-
"query": Array [
181-
"{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"}",
182-
],
183-
"timeRange": Array [
184-
"{\\"from\\":\\"now-7d\\",\\"to\\":\\"now\\"}",
185-
],
186-
},
187-
"function": "kibana_context",
188-
"type": "function",
189-
},
190175
Object {
191176
"arguments": Object {
192177
"layerIds": Array [
@@ -305,10 +290,10 @@ describe('workspace_panel', () => {
305290
);
306291

307292
expect(
308-
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[2].arguments.layerIds
293+
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[1].arguments.layerIds
309294
).toEqual(['first', 'second', 'third']);
310295
expect(
311-
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[2].arguments.tables
296+
(instance.find(expressionRendererMock).prop('expression') as Ast).chain[1].arguments.tables
312297
).toMatchInlineSnapshot(`
313298
Array [
314299
Object {

x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function InnerWorkspacePanel({
130130
visualizationState,
131131
datasourceMap,
132132
datasourceStates,
133-
framePublicAPI,
133+
datasourceLayers: framePublicAPI.datasourceLayers,
134134
});
135135
} catch (e) {
136136
// Most likely an error in the expression provided by a datasource or visualization

0 commit comments

Comments
 (0)