-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Saved Search Embeddable] Add view action (#112396)
* [Saved Search Embeddable] Add view action * Fix typescript and lint errors; add tests * Add a functional test * Fix a unit test * Renaming action Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
ae4e7cc
commit c0d68aa
Showing
7 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import { ApplicationStart, PublicAppInfo } from 'src/core/public'; | ||
import { deepFreeze } from '@kbn/std'; | ||
import { BehaviorSubject, Subject } from 'rxjs'; | ||
|
||
const capabilities = deepFreeze({ | ||
catalogue: {}, | ||
management: {}, | ||
navLinks: {}, | ||
discover: { | ||
show: true, | ||
edit: false, | ||
}, | ||
}); | ||
|
||
export const createStartContractMock = (): jest.Mocked<ApplicationStart> => { | ||
const currentAppId$ = new Subject<string | undefined>(); | ||
|
||
return { | ||
applications$: new BehaviorSubject<Map<string, PublicAppInfo>>(new Map()), | ||
currentAppId$: currentAppId$.asObservable(), | ||
capabilities, | ||
navigateToApp: jest.fn(), | ||
navigateToUrl: jest.fn(), | ||
getUrlForApp: jest.fn(), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/plugins/discover/public/application/embeddable/view_saved_search_action.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ContactCardEmbeddable } from 'src/plugins/embeddable/public/lib/test_samples'; | ||
|
||
import { ViewSavedSearchAction } from './view_saved_search_action'; | ||
import { SavedSearchEmbeddable } from './saved_search_embeddable'; | ||
import { createStartContractMock } from '../../__mocks__/start_contract'; | ||
import { savedSearchMock } from '../../__mocks__/saved_search'; | ||
import { discoverServiceMock } from '../../__mocks__/services'; | ||
import { IndexPattern } from 'src/plugins/data/common'; | ||
import { createFilterManagerMock } from 'src/plugins/data/public/query/filter_manager/filter_manager.mock'; | ||
import { ViewMode } from 'src/plugins/embeddable/public'; | ||
|
||
const applicationMock = createStartContractMock(); | ||
const savedSearch = savedSearchMock; | ||
const indexPatterns = [] as IndexPattern[]; | ||
const services = discoverServiceMock; | ||
const filterManager = createFilterManagerMock(); | ||
const searchInput = { | ||
timeRange: { | ||
from: '2021-09-15', | ||
to: '2021-09-16', | ||
}, | ||
id: '1', | ||
viewMode: ViewMode.VIEW, | ||
}; | ||
const executeTriggerActions = async (triggerId: string, context: object) => { | ||
return Promise.resolve(undefined); | ||
}; | ||
const trigger = { id: 'ACTION_VIEW_SAVED_SEARCH' }; | ||
const embeddableConfig = { | ||
savedSearch, | ||
editUrl: '', | ||
editPath: '', | ||
indexPatterns, | ||
editable: true, | ||
filterManager, | ||
services, | ||
}; | ||
|
||
describe('view saved search action', () => { | ||
it('is compatible when embeddable is of type saved search, in view mode && appropriate permissions are set', async () => { | ||
const action = new ViewSavedSearchAction(applicationMock); | ||
const embeddable = new SavedSearchEmbeddable( | ||
embeddableConfig, | ||
searchInput, | ||
executeTriggerActions | ||
); | ||
expect(await action.isCompatible({ embeddable, trigger })).toBe(true); | ||
}); | ||
|
||
it('is not compatible when embeddable not of type saved search', async () => { | ||
const action = new ViewSavedSearchAction(applicationMock); | ||
const embeddable = new ContactCardEmbeddable( | ||
{ | ||
id: '123', | ||
firstName: 'sue', | ||
viewMode: ViewMode.EDIT, | ||
}, | ||
{ | ||
execAction: () => Promise.resolve(undefined), | ||
} | ||
); | ||
expect( | ||
await action.isCompatible({ | ||
embeddable, | ||
trigger, | ||
}) | ||
).toBe(false); | ||
}); | ||
|
||
it('is not visible when in edit mode', async () => { | ||
const action = new ViewSavedSearchAction(applicationMock); | ||
const input = { ...searchInput, viewMode: ViewMode.EDIT }; | ||
const embeddable = new SavedSearchEmbeddable(embeddableConfig, input, executeTriggerActions); | ||
expect( | ||
await action.isCompatible({ | ||
embeddable, | ||
trigger, | ||
}) | ||
).toBe(false); | ||
}); | ||
|
||
it('execute navigates to a saved search', async () => { | ||
const action = new ViewSavedSearchAction(applicationMock); | ||
const embeddable = new SavedSearchEmbeddable( | ||
embeddableConfig, | ||
searchInput, | ||
executeTriggerActions | ||
); | ||
await action.execute({ embeddable, trigger }); | ||
expect(applicationMock.navigateToApp).toHaveBeenCalledWith('discover', { | ||
path: `#/view/${savedSearch.id}`, | ||
}); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
src/plugins/discover/public/application/embeddable/view_saved_search_action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import { ActionExecutionContext } from 'src/plugins/ui_actions/public'; | ||
import { ApplicationStart } from 'kibana/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { IEmbeddable, ViewMode } from '../../../../embeddable/public'; | ||
import { Action } from '../../../../ui_actions/public'; | ||
import { SavedSearchEmbeddable } from './saved_search_embeddable'; | ||
import { SEARCH_EMBEDDABLE_TYPE } from '../../../common'; | ||
|
||
export const ACTION_VIEW_SAVED_SEARCH = 'ACTION_VIEW_SAVED_SEARCH'; | ||
|
||
export interface ViewSearchContext { | ||
embeddable: IEmbeddable; | ||
} | ||
|
||
export class ViewSavedSearchAction implements Action<ViewSearchContext> { | ||
public id = ACTION_VIEW_SAVED_SEARCH; | ||
public readonly type = ACTION_VIEW_SAVED_SEARCH; | ||
|
||
constructor(private readonly application: ApplicationStart) {} | ||
|
||
async execute(context: ActionExecutionContext<ViewSearchContext>): Promise<void> { | ||
const { embeddable } = context; | ||
const savedSearchId = (embeddable as SavedSearchEmbeddable).getSavedSearch().id; | ||
const path = `#/view/${encodeURIComponent(savedSearchId)}`; | ||
const app = embeddable ? embeddable.getOutput().editApp : undefined; | ||
await this.application.navigateToApp(app ? app : 'discover', { path }); | ||
} | ||
|
||
getDisplayName(context: ActionExecutionContext<ViewSearchContext>): string { | ||
return i18n.translate('discover.savedSearchEmbeddable.action.viewSavedSearch.displayName', { | ||
defaultMessage: 'Open in Discover', | ||
}); | ||
} | ||
|
||
getIconType(context: ActionExecutionContext<ViewSearchContext>): string | undefined { | ||
return 'inspect'; | ||
} | ||
|
||
async isCompatible(context: ActionExecutionContext<ViewSearchContext>) { | ||
const { embeddable } = context; | ||
const { capabilities } = this.application; | ||
const hasDiscoverPermissions = | ||
(capabilities.discover.show as boolean) || (capabilities.discover.save as boolean); | ||
return Boolean( | ||
embeddable.type === SEARCH_EMBEDDABLE_TYPE && | ||
embeddable.getInput().viewMode === ViewMode.VIEW && | ||
hasDiscoverPermissions | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters