diff --git a/src/platform/plugins/shared/dashboard/public/dashboard_actions/library_add_action.test.tsx b/src/platform/plugins/shared/dashboard/public/dashboard_actions/library_add_action.test.tsx new file mode 100644 index 0000000000000..03d169c0a27ba --- /dev/null +++ b/src/platform/plugins/shared/dashboard/public/dashboard_actions/library_add_action.test.tsx @@ -0,0 +1,75 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import React from 'react'; +import type { OnSaveProps } from '@kbn/saved-objects-plugin/public'; +import { AddPanelToLibraryActionApi, AddToLibraryAction } from './library_add_action'; +import { BehaviorSubject } from 'rxjs'; + +jest.mock('@kbn/saved-objects-plugin/public', () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { render } = require('@testing-library/react'); + const MockSavedObjectSaveModal = ({ onSave }: { onSave: (props: OnSaveProps) => void }) => { + onSave({ + newTitle: 'Library panel one', + newCopyOnSave: true, + isTitleDuplicateConfirmed: false, + onTitleDuplicate: () => {}, + newDescription: '', + }); + return null; + }; + return { + SavedObjectSaveModal: MockSavedObjectSaveModal, + showSaveModal: (saveModal: React.ReactElement) => { + render(saveModal); + }, + }; +}); + +describe('AddToLibraryAction', () => { + const action = new AddToLibraryAction(); + const saveToLibraryMock = jest.fn(async () => 'libraryId1'); + const replacePanelMock = jest.fn(); + const embeddableApi = { + checkForDuplicateTitle: async () => {}, + canLinkToLibrary: async () => true, + canUnlinkFromLibrary: async () => false, + getSerializedStateByReference: () => ({ rawState: { savedObjectId: 'libraryId1' } }), + getSerializedStateByValue: () => ({ rawState: {} }), + parentApi: { + replacePanel: replacePanelMock, + viewMode$: new BehaviorSubject('edit'), + }, + saveToLibrary: saveToLibraryMock, + type: 'testEmbeddable', + uuid: '1', + } as AddPanelToLibraryActionApi; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + describe('execute', () => { + test('should save panel to library and replace panel with library panel', async () => { + await action.execute({ embeddable: embeddableApi }); + expect(saveToLibraryMock).toHaveBeenCalled(); + expect(replacePanelMock).toHaveBeenCalledWith('1', { + panelType: 'testEmbeddable', + serializedState: { + rawState: { + savedObjectId: 'libraryId1', + title: 'Library panel one', + }, + references: undefined, + }, + }); + }); + }); +}); diff --git a/src/platform/plugins/shared/dashboard/public/dashboard_actions/library_unlink_action.test.ts b/src/platform/plugins/shared/dashboard/public/dashboard_actions/library_unlink_action.test.ts new file mode 100644 index 0000000000000..41ee0c1e0066c --- /dev/null +++ b/src/platform/plugins/shared/dashboard/public/dashboard_actions/library_unlink_action.test.ts @@ -0,0 +1,52 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { BehaviorSubject } from 'rxjs'; +import { UnlinkFromLibraryAction, UnlinkPanelFromLibraryActionApi } from './library_unlink_action'; + +describe('AddToLibraryAction', () => { + const action = new UnlinkFromLibraryAction(); + const replacePanelMock = jest.fn(); + const embeddableApi = { + defaultTitle$: new BehaviorSubject('Panel one'), + checkForDuplicateTitle: async () => {}, + canLinkToLibrary: async () => false, + canUnlinkFromLibrary: async () => true, + getSerializedStateByReference: () => ({ rawState: {} }), + getSerializedStateByValue: () => ({ rawState: { key1: 'value1' } }), + parentApi: { + replacePanel: replacePanelMock, + viewMode$: new BehaviorSubject('edit'), + }, + saveToLibrary: async () => 'libraryId1', + type: 'testEmbeddable', + uuid: '1', + } as UnlinkPanelFromLibraryActionApi; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + describe('execute', () => { + test('should replace panel with by value panel', async () => { + await action.execute({ embeddable: embeddableApi }); + expect(replacePanelMock).toHaveBeenCalledWith('1', { + panelType: 'testEmbeddable', + serializedState: { + rawState: { + key1: 'value1', + // should get default title from by reference embeddable + title: 'Panel one', + }, + references: undefined, + }, + }); + }); + }); +}); diff --git a/src/platform/test/functional/apps/dashboard/group6/embeddable_library.ts b/src/platform/test/functional/apps/dashboard/group6/embeddable_library.ts deleted file mode 100644 index 9f885bde7e04e..0000000000000 --- a/src/platform/test/functional/apps/dashboard/group6/embeddable_library.ts +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -import { FtrProviderContext } from '../../../ftr_provider_context'; - -export default function ({ getService, getPageObjects }: FtrProviderContext) { - const { dashboard } = getPageObjects(['dashboard']); - const find = getService('find'); - const kibanaServer = getService('kibanaServer'); - const dashboardAddPanel = getService('dashboardAddPanel'); - const panelActions = getService('dashboardPanelActions'); - const savedObjectsFinder = getService('savedObjectsFinder'); - const title = 'Rendering Test: heatmap'; - - describe('embeddable library', () => { - before(async () => { - await kibanaServer.savedObjects.cleanStandardList(); - await kibanaServer.importExport.load( - 'src/platform/test/functional/fixtures/kbn_archiver/dashboard/current/kibana' - ); - await kibanaServer.uiSettings.replace({ - defaultIndex: '0bf35f60-3dc9-11e8-8660-4d65aa086b3c', - }); - await dashboard.navigateToApp(); - await dashboard.preserveCrossAppState(); - await dashboard.clickNewDashboard(); - }); - - it('unlink visualize panel from embeddable library', async () => { - // add heatmap panel from library - await dashboardAddPanel.clickOpenAddPanel(); - await savedObjectsFinder.filterEmbeddableNames(title); - await find.clickByButtonText(title); - await dashboardAddPanel.closeAddPanel(); - - await panelActions.unlinkFromLibrary(title); - await panelActions.expectNotLinkedToLibrary(title); - - await dashboardAddPanel.clickOpenAddPanel(); - await savedObjectsFinder.filterEmbeddableNames(title); - await find.existsByLinkText(title); - await dashboardAddPanel.closeAddPanel(); - }); - - it('save visualize panel to embeddable library', async () => { - const newTitle = 'Rendering Test: heatmap - copy'; - await panelActions.saveToLibrary(newTitle, title); - await panelActions.expectLinkedToLibrary(newTitle); - }); - }); -} diff --git a/src/platform/test/functional/apps/dashboard/group6/index.ts b/src/platform/test/functional/apps/dashboard/group6/index.ts index 9b72b632b9dab..22f0b9323f6e5 100644 --- a/src/platform/test/functional/apps/dashboard/group6/index.ts +++ b/src/platform/test/functional/apps/dashboard/group6/index.ts @@ -41,7 +41,6 @@ export default function ({ getService, loadTestFile }: FtrProviderContext) { // The dashboard_snapshot test below requires the timestamped URL which breaks the view_edit test. // If we don't use the timestamp in the URL, the colors in the charts will be different. loadTestFile(require.resolve('./dashboard_snapshots')); - loadTestFile(require.resolve('./embeddable_library')); loadTestFile(require.resolve('./dashboard_esql_chart')); loadTestFile(require.resolve('./dashboard_esql_no_data')); });