From 4406b0e44e876bab92b56d3cdb5087533cb78ec9 Mon Sep 17 00:00:00 2001 From: Julia Rechkunova Date: Wed, 3 Apr 2024 17:30:06 +0200 Subject: [PATCH] [Discover] Mark other lnsXY charts as Customized too --- .../public/chart/suggestion_selector.tsx | 4 +- .../public/services/lens_vis_service.ts | 5 +- .../public/utils/external_vis_context.test.ts | 70 ++++++++++++++++++- .../public/utils/external_vis_context.ts | 25 +++++-- .../apps/discover/group3/_lens_vis.ts | 7 +- 5 files changed, 98 insertions(+), 13 deletions(-) diff --git a/src/plugins/unified_histogram/public/chart/suggestion_selector.tsx b/src/plugins/unified_histogram/public/chart/suggestion_selector.tsx index 8154fd40b1188..0745fd47ecdc9 100644 --- a/src/plugins/unified_histogram/public/chart/suggestion_selector.tsx +++ b/src/plugins/unified_histogram/public/chart/suggestion_selector.tsx @@ -90,7 +90,7 @@ export const SuggestionSelector = ({ > } + prepend={} placeholder={i18n.translate('unifiedHistogram.suggestionSelectorPlaceholder', { defaultMessage: 'Select visualization', })} @@ -110,7 +110,7 @@ export const SuggestionSelector = ({ return ( - + {option.label} diff --git a/src/plugins/unified_histogram/public/services/lens_vis_service.ts b/src/plugins/unified_histogram/public/services/lens_vis_service.ts index f03f95c9c9474..494d99976b2dd 100644 --- a/src/plugins/unified_histogram/public/services/lens_vis_service.ts +++ b/src/plugins/unified_histogram/public/services/lens_vis_service.ts @@ -265,10 +265,7 @@ export class LensVisService { } } - if ( - externalVisContext && - externalVisContext.suggestionType === UnifiedHistogramSuggestionType.lensSuggestion - ) { + if (externalVisContext) { // externalVisContext can be based on an unfamiliar suggestion, but it was saved somehow, so try to restore it too const derivedSuggestion = deriveLensSuggestionFromLensAttributes({ externalVisContext, diff --git a/src/plugins/unified_histogram/public/utils/external_vis_context.test.ts b/src/plugins/unified_histogram/public/utils/external_vis_context.test.ts index 1d44e4054831a..a786bf102065a 100644 --- a/src/plugins/unified_histogram/public/utils/external_vis_context.test.ts +++ b/src/plugins/unified_histogram/public/utils/external_vis_context.test.ts @@ -9,14 +9,14 @@ import type { DataView } from '@kbn/data-views-plugin/common'; import type { Suggestion } from '@kbn/lens-plugin/public'; import { - exportVisContext, canImportVisContext, + exportVisContext, isSuggestionShapeAndVisContextCompatible, } from './external_vis_context'; import { getLensVisMock } from '../__mocks__/lens_vis'; import { dataViewWithTimefieldMock } from '../__mocks__/data_view_with_timefield'; import { tableMock, tableQueryMock } from '../__mocks__/table'; -import type { UnifiedHistogramVisContext } from '../types'; +import { UnifiedHistogramSuggestionType, UnifiedHistogramVisContext } from '../types'; describe('external_vis_context', () => { const dataView: DataView = dataViewWithTimefieldMock; @@ -63,6 +63,7 @@ describe('external_vis_context', () => { isSuggestionShapeAndVisContextCompatible( { visualizationId: 'lnsPie', visualizationState: { shape: 'donut' } } as Suggestion, { + suggestionType: UnifiedHistogramSuggestionType.lensSuggestion, attributes: { visualizationType: 'lnsPie', state: { visualization: { shape: 'donut' } }, @@ -75,6 +76,7 @@ describe('external_vis_context', () => { isSuggestionShapeAndVisContextCompatible( { visualizationId: 'lnsPie', visualizationState: { shape: 'donut' } } as Suggestion, { + suggestionType: UnifiedHistogramSuggestionType.lensSuggestion, attributes: { visualizationType: 'lnsPie', state: { visualization: { shape: 'waffle' } }, @@ -87,12 +89,76 @@ describe('external_vis_context', () => { isSuggestionShapeAndVisContextCompatible( { visualizationId: 'lnsPie', visualizationState: { shape: 'donut' } } as Suggestion, { + suggestionType: UnifiedHistogramSuggestionType.lensSuggestion, + attributes: { + visualizationType: 'lnsXY', + }, + } as UnifiedHistogramVisContext + ) + ).toBe(false); + + expect( + isSuggestionShapeAndVisContextCompatible( + { + visualizationId: 'lnsXY', + visualizationState: { preferredSeriesType: 'bar_stacked' }, + } as Suggestion, + { + attributes: { + visualizationType: 'lnsXY', + state: { visualization: { preferredSeriesType: 'bar_stacked' } }, + }, + } as UnifiedHistogramVisContext + ) + ).toBe(true); + + expect( + isSuggestionShapeAndVisContextCompatible( + { + visualizationId: 'lnsXY', + visualizationState: { preferredSeriesType: 'bar_stacked' }, + } as Suggestion, + { + suggestionType: UnifiedHistogramSuggestionType.histogramForESQL, + attributes: { + visualizationType: 'lnsXY', + state: { visualization: { preferredSeriesType: 'line' } }, + }, + } as UnifiedHistogramVisContext + ) + ).toBe(true); + + expect( + isSuggestionShapeAndVisContextCompatible( + { + visualizationId: 'lnsXY', + visualizationState: { preferredSeriesType: 'bar_stacked' }, + } as Suggestion, + { + suggestionType: UnifiedHistogramSuggestionType.lensSuggestion, attributes: { visualizationType: 'lnsXY', + state: { visualization: { preferredSeriesType: 'line' } }, }, } as UnifiedHistogramVisContext ) ).toBe(false); + + expect( + isSuggestionShapeAndVisContextCompatible( + { + visualizationId: 'lnsXY', + visualizationState: { preferredSeriesType: 'bar_stacked' }, + } as Suggestion, + { + suggestionType: UnifiedHistogramSuggestionType.histogramForDataView, + attributes: { + visualizationType: 'lnsXY', + state: { visualization: { preferredSeriesType: 'bar_stacked' } }, + }, + } as UnifiedHistogramVisContext + ) + ).toBe(true); }); }); }); diff --git a/src/plugins/unified_histogram/public/utils/external_vis_context.ts b/src/plugins/unified_histogram/public/utils/external_vis_context.ts index 7724cbec41ddf..9c5088eaccb4f 100644 --- a/src/plugins/unified_histogram/public/utils/external_vis_context.ts +++ b/src/plugins/unified_histogram/public/utils/external_vis_context.ts @@ -7,7 +7,7 @@ */ import type { Suggestion } from '@kbn/lens-plugin/public'; -import type { UnifiedHistogramVisContext } from '../types'; +import { UnifiedHistogramSuggestionType, UnifiedHistogramVisContext } from '../types'; import { removeTablesFromLensAttributes } from './lens_vis_from_table'; export const exportVisContext = ( @@ -66,11 +66,28 @@ export const isSuggestionShapeAndVisContextCompatible = ( if (!suggestion && !externalVisContext) { return true; } + + if (suggestion?.visualizationId !== externalVisContext?.attributes?.visualizationType) { + return false; + } + + if (externalVisContext?.suggestionType !== UnifiedHistogramSuggestionType.lensSuggestion) { + return true; + } + + if (suggestion?.visualizationId === 'lnsXY') { + return ( + // @ts-expect-error visualization state has different structure between vis types + suggestion?.visualizationState?.preferredSeriesType === + // @ts-expect-error visualization state has different structure between vis types + externalVisContext?.attributes?.state?.visualization?.preferredSeriesType + ); + } + return ( - suggestion?.visualizationId === externalVisContext?.attributes?.visualizationType && // @ts-expect-error visualization state has different structure between vis types suggestion?.visualizationState?.shape === - // @ts-expect-error visualization state has different structure between vis types - externalVisContext?.attributes?.state?.visualization?.shape + // @ts-expect-error visualization state has different structure between vis types + externalVisContext?.attributes?.state?.visualization?.shape ); }; diff --git a/test/functional/apps/discover/group3/_lens_vis.ts b/test/functional/apps/discover/group3/_lens_vis.ts index 8243688167553..dfbe76e86f0ff 100644 --- a/test/functional/apps/discover/group3/_lens_vis.ts +++ b/test/functional/apps/discover/group3/_lens_vis.ts @@ -17,6 +17,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const retry = getService('retry'); const find = getService('find'); const browser = getService('browser'); + const toasts = getService('toasts'); const PageObjects = getPageObjects([ 'settings', 'common', @@ -86,10 +87,13 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { expect(await testSubjects.getVisibleText('lnsChartSwitchPopover')).to.be(seriesType); }); + await toasts.dismissAll(); + await testSubjects.scrollIntoView('applyFlyoutButton'); await testSubjects.click('applyFlyoutButton'); } async function getCurrentVisSeriesTypeLabel() { + await toasts.dismissAll(); await testSubjects.click('unifiedHistogramEditFlyoutVisualization'); const seriesType = await testSubjects.getVisibleText('lnsChartSwitchPopover'); await testSubjects.click('cancelFlyoutButton'); @@ -108,6 +112,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await security.testUser.setRoles(['kibana_admin', 'test_logstash_reader']); await esArchiver.loadIfNeeded('test/functional/fixtures/es_archiver/logstash_functional'); await kibanaServer.importExport.load('test/functional/fixtures/kbn_archiver/discover'); + await browser.setWindowSize(1300, 1000); }); after(async () => { @@ -618,7 +623,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.discover.waitUntilSearchingHasFinished(); await testSubjects.missingOrFail('unsavedChangesBadge'); - expect(await PageObjects.discover.getCurrentLensChart()).to.be('Bar vertical stacked'); + expect(await PageObjects.discover.getCurrentLensChart()).to.be('Customized'); expect(await getCurrentVisSeriesTypeLabel()).to.be('Line'); expect(await getCurrentVisChartTitle()).to.be('Bar vertical stacked'); });