From 2255d368666fb6d58b44abc8fc5b3cdf152298a8 Mon Sep 17 00:00:00 2001 From: Suchit Sahoo Date: Thu, 5 Sep 2024 21:21:33 +0000 Subject: [PATCH] Fix Test failures Signed-off-by: Suchit Sahoo --- .../open_search_panel.test.tsx.snap | 3 ++ .../finder/saved_object_finder.test.tsx | 53 +++++++++++++++++++ .../public/finder/saved_object_finder.tsx | 50 +++++++++-------- 3 files changed, 83 insertions(+), 23 deletions(-) diff --git a/src/plugins/discover/public/application/components/top_nav/__snapshots__/open_search_panel.test.tsx.snap b/src/plugins/discover/public/application/components/top_nav/__snapshots__/open_search_panel.test.tsx.snap index 1d35e4e977ba..d4e8ff6ea026 100644 --- a/src/plugins/discover/public/application/components/top_nav/__snapshots__/open_search_panel.test.tsx.snap +++ b/src/plugins/discover/public/application/components/top_nav/__snapshots__/open_search_panel.test.tsx.snap @@ -35,6 +35,9 @@ exports[`render 1`] = ` Array [ Object { "getIconForSavedObject": [Function], + "includeFields": Array [ + "kibanaSavedObjectMeta", + ], "name": "Saved search", "type": "search", }, diff --git a/src/plugins/saved_objects/public/finder/saved_object_finder.test.tsx b/src/plugins/saved_objects/public/finder/saved_object_finder.test.tsx index c4e885c33e77..7c89e41c0a32 100644 --- a/src/plugins/saved_objects/public/finder/saved_object_finder.test.tsx +++ b/src/plugins/saved_objects/public/finder/saved_object_finder.test.tsx @@ -48,6 +48,7 @@ import React from 'react'; import * as sinon from 'sinon'; import { SavedObjectFinderUi as SavedObjectFinder } from './saved_object_finder'; import { coreMock } from '../../../../core/public/mocks'; +import { of } from 'rxjs'; describe('SavedObjectsFinder', () => { const doc = { @@ -79,16 +80,20 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); wrapper.instance().componentDidMount!(); + await Promise.resolve(); + expect(core.savedObjects.client.find).toHaveBeenCalledWith({ type: ['search'], fields: ['title'], @@ -106,17 +111,20 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); wrapper.instance().componentDidMount!(); await nextTick(); + await Promise.resolve(); expect( wrapper.containsMatchingElement() ).toEqual(true); @@ -129,6 +137,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} onChoose={chooseStub} savedObjectMetaData={searchMetaData} + application={core.application} /> ); wrapper.instance().componentDidMount!(); await nextTick(); + await Promise.resolve(); wrapper.find(EuiListGroupItem).first().simulate('click'); expect(chooseStub.calledWith('1', 'search', `${doc.attributes.title} (Search)`, doc)).toEqual( true @@ -154,16 +165,20 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); wrapper.instance().componentDidMount!(); await nextTick(); + await Promise.resolve(); + const list = wrapper.find(EuiListGroup); expect(list.childAt(0).key()).toBe('2'); expect(list.childAt(1).key()).toBe('1'); @@ -175,12 +190,14 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); @@ -199,6 +216,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { showSavedObject: ({ id }) => id !== '1', }, ]} + application={core.application} /> ); @@ -229,12 +248,14 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); wrapper.instance().componentDidMount!(); @@ -258,6 +279,7 @@ describe('SavedObjectsFinder', () => { it('should include additional fields in search if listed in meta data', async () => { const core = coreMock.createStart(); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); (core.savedObjects.client.find as jest.Mock).mockResolvedValue({ savedObjects: [] }); const wrapper = shallow( @@ -278,6 +300,7 @@ describe('SavedObjectsFinder', () => { includeFields: ['field2', 'field3'], }, ]} + application={core.application} /> ); wrapper.instance().componentDidMount!(); @@ -304,12 +327,14 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); @@ -332,6 +357,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { getIconForSavedObject: () => 'visLine', }, ]} + application={core.application} /> ); wrapper.instance().componentDidMount!(); @@ -386,6 +413,7 @@ describe('SavedObjectsFinder', () => { }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} showFilter={false} savedObjectMetaData={metaDataConfig} + application={core.application} /> ); @@ -411,6 +440,7 @@ describe('SavedObjectsFinder', () => { }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} showFilter={true} savedObjectMetaData={metaDataConfig} + application={core.application} /> ); @@ -436,6 +467,7 @@ describe('SavedObjectsFinder', () => { }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} showFilter={true} savedObjectMetaData={metaDataConfig} + application={core.application} /> ); @@ -464,6 +497,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const noItemsMessage = ; const wrapper = shallow( @@ -472,6 +506,7 @@ describe('SavedObjectsFinder', () => { uiSettings={core.uiSettings} noItemsMessage={noItemsMessage} savedObjectMetaData={searchMetaData} + application={core.application} /> ); wrapper.instance().componentDidMount!(); @@ -495,6 +530,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: longItemList }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} initialPageSize={15} savedObjectMetaData={searchMetaData} + application={core.application} /> ); @@ -517,6 +554,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: longItemList }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} initialPageSize={15} savedObjectMetaData={searchMetaData} + application={core.application} /> ); @@ -539,6 +578,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: longItemList }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} initialPageSize={15} savedObjectMetaData={searchMetaData} + application={core.application} /> ); @@ -561,6 +602,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: longItemList }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} fixedPageSize={33} savedObjectMetaData={searchMetaData} + application={core.application} /> ); @@ -583,6 +626,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: longItemList }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { uiSettings={core.uiSettings} fixedPageSize={33} savedObjectMetaData={searchMetaData} + application={core.application} /> ); @@ -604,12 +649,14 @@ describe('SavedObjectsFinder', () => { it('should display a spinner during initial loading', () => { const core = coreMock.createStart(); (core.savedObjects.client.find as jest.Mock).mockResolvedValue({ savedObjects: [] }); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); @@ -621,6 +668,7 @@ describe('SavedObjectsFinder', () => { ((core.savedObjects.client.find as any) as jest.SpyInstance).mockImplementation(() => Promise.resolve({ savedObjects: [doc] }) ); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { getIconForSavedObject: () => 'search', }, ]} + application={core.application} /> ); @@ -646,12 +695,14 @@ describe('SavedObjectsFinder', () => { ((core.savedObjects.client.find as any) as jest.SpyInstance).mockImplementation(() => Promise.resolve({ savedObjects: [doc] }) ); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( ); @@ -674,6 +725,7 @@ describe('SavedObjectsFinder', () => { Promise.resolve({ savedObjects: [doc, doc2] }) ); core.uiSettings.get.mockImplementation(() => 10); + core.application.currentAppId$ = of('data-explorer'); const wrapper = shallow( { getIconForSavedObject: () => 'visLine', }, ]} + application={core.application} > diff --git a/src/plugins/saved_objects/public/finder/saved_object_finder.tsx b/src/plugins/saved_objects/public/finder/saved_object_finder.tsx index 3857d8e2bb38..8f54d402c2c5 100644 --- a/src/plugins/saved_objects/public/finder/saved_object_finder.tsx +++ b/src/plugins/saved_objects/public/finder/saved_object_finder.tsx @@ -62,7 +62,7 @@ import { } from 'src/core/public'; import { DataSourceAttributes } from 'src/plugins/data_source/common/data_sources'; -import { DataPublicPluginStart } from 'src/plugins/data/public'; +import { DataPublicPluginStart, LanguageServiceContract } from 'src/plugins/data/public'; import { first } from 'rxjs/operators'; import { getIndexPatternTitle } from '../../../data/common/index_patterns/utils'; import { LISTING_LIMIT_SETTING } from '../../common'; @@ -148,12 +148,22 @@ class SavedObjectFinderUi extends React.Component< return appId; } - readonly languageService = this.props.data.query.queryString.getLanguageService(); + readonly languageService = this.props.data?.query?.queryString?.getLanguageService(); private isComponentMounted: boolean = false; + private isSavedSearchLanguageSupported( + languageId?: string, + currentAppId?: string, + languageService?: LanguageServiceContract + ) { + if (!languageId || !currentAppId || !languageService) { + return true; + } + return languageService.getLanguage(languageId).supportedAppNames.includes(currentAppId) ?? true; + } + private debouncedFetch = _.debounce(async (query: string) => { const metaDataMap = this.getSavedObjectMetaDataMap(); - const currentAppId = await this.getCurrentAppId(); const fields = Object.values(metaDataMap) .map((metaData) => metaData.includeFields || []) @@ -175,7 +185,9 @@ class SavedObjectFinderUi extends React.Component< return await client.get('data-source', id); }; - let savedObjects = await Promise.all( + const currentAppId = await this.getCurrentAppId(); + + const savedObjects = await Promise.all( resp.savedObjects.map(async (obj) => { if (obj.type === 'index-pattern') { const result = { ...obj }; @@ -185,32 +197,24 @@ class SavedObjectFinderUi extends React.Component< getDataSource ); return result; + } else if (obj.type === 'search') { + const sourceObject = JSON.parse( + obj.attributes?.kibanaSavedObjectMeta?.searchSourceJSON ?? null + ); + const languageId = sourceObject?.query?.language; + if (this.isSavedSearchLanguageSupported(languageId, currentAppId, this.languageService)) { + return obj; + } } else { return obj; } }) ); - // Filter search objects whose language are not supported by the current Application - savedObjects = savedObjects.filter((obj) => { - if (obj.type === 'search') { - if (obj.attributes?.kibanaSavedObjectMeta) { - const sourceObject = JSON.parse(obj.attributes?.kibanaSavedObjectMeta?.searchSourceJSON); - const languageId = sourceObject.query.language; - - const languageProperties = this.languageService.getLanguage(languageId); - - if (languageProperties?.supportedAppNames.includes(currentAppId)) { - return true; - } else { - return false; - } - } - } - return true; - }); - resp.savedObjects = savedObjects.filter((savedObject) => { + if (!savedObject) { + return false; + } const metaData = metaDataMap[savedObject.type]; if (metaData.showSavedObject) { return metaData.showSavedObject(savedObject);