From 9b8104dd468999806512a54a8916c0a03092c617 Mon Sep 17 00:00:00 2001 From: Marco Antonio Ghiani Date: Tue, 24 Oct 2023 12:51:57 +0200 Subject: [PATCH 01/20] feat(discover): enhance flyout customization to update content --- .../discover_grid_flyout.test.tsx | 124 +++++++++++++----- .../discover_grid_flyout.tsx | 83 ++++++++---- .../use_flyout_actions.tsx | 18 +-- .../flyout_customization.ts | 17 +++ 4 files changed, 172 insertions(+), 70 deletions(-) diff --git a/src/plugins/discover/public/components/discover_grid_flyout/discover_grid_flyout.test.tsx b/src/plugins/discover/public/components/discover_grid_flyout/discover_grid_flyout.test.tsx index d1ff933ecad8f5..b2d46022bd344a 100644 --- a/src/plugins/discover/public/components/discover_grid_flyout/discover_grid_flyout.test.tsx +++ b/src/plugins/discover/public/components/discover_grid_flyout/discover_grid_flyout.test.tsx @@ -8,6 +8,7 @@ import React from 'react'; import { findTestSubject } from '@elastic/eui/lib/test'; +import { EuiFlexItem } from '@elastic/eui'; import { mountWithIntl } from '@kbn/test-jest-helpers'; import type { Query, AggregateQuery } from '@kbn/es-query'; import { DiscoverGridFlyout, DiscoverGridFlyoutProps } from './discover_grid_flyout'; @@ -24,7 +25,6 @@ import { ReactWrapper } from 'enzyme'; import { setUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/plugin'; import { mockUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/__mocks__'; import { FlyoutCustomization, useDiscoverCustomization } from '../../customizations'; -import { EuiFlexItem } from '@elastic/eui'; const mockFlyoutCustomization: FlyoutCustomization = { id: 'flyout', @@ -69,6 +69,9 @@ describe('Discover flyout', function () { }, contextLocator: { getRedirectUrl: jest.fn(() => 'mock-context-redirect-url') }, singleDocLocator: { getRedirectUrl: jest.fn(() => 'mock-doc-redirect-url') }, + toastNotifications: { + addSuccess: jest.fn(), + }, } as unknown as DiscoverServices; setUnifiedDocViewerServices(mockUnifiedDocViewerServices); @@ -103,11 +106,12 @@ describe('Discover flyout', function () { const component = mountWithIntl(); await waitNextUpdate(component); - return { component, props }; + return { component, props, services }; }; beforeEach(() => { mockFlyoutCustomization.actions.defaultActions = undefined; + mockFlyoutCustomization.Content = undefined; jest.clearAllMocks(); (useDiscoverCustomization as jest.Mock).mockImplementation(() => mockFlyoutCustomization); @@ -226,44 +230,98 @@ describe('Discover flyout', function () { expect(flyoutTitle.text()).toBe('Expanded row'); }); - describe('when customizations actions exists', () => { - it('should display actions added by getActionItems', async () => { - mockFlyoutCustomization.actions = { - getActionItems: jest.fn(() => [ - { - id: 'action-item-1', - enabled: true, - Content: () => Action 1, - }, - { - id: 'action-item-2', - enabled: true, - Content: () => Action 2, + describe('with applied customizations', () => { + describe('when actions are customized', () => { + it('should display actions added by getActionItems', async () => { + mockFlyoutCustomization.actions = { + getActionItems: jest.fn(() => [ + { + id: 'action-item-1', + enabled: true, + Content: () => Action 1, + }, + { + id: 'action-item-2', + enabled: true, + Content: () => Action 2, + }, + ]), + }; + + const { component } = await mountComponent({}); + + const action1 = findTestSubject(component, 'customActionItem1'); + const action2 = findTestSubject(component, 'customActionItem2'); + + expect(action1.text()).toBe('Action 1'); + expect(action2.text()).toBe('Action 2'); + }); + + it('should allow disabling default actions', async () => { + mockFlyoutCustomization.actions = { + defaultActions: { + viewSingleDocument: { disabled: true }, + viewSurroundingDocument: { disabled: true }, }, - ]), - }; + }; - const { component } = await mountComponent({}); + const { component } = await mountComponent({}); - const action1 = findTestSubject(component, 'customActionItem1'); - const action2 = findTestSubject(component, 'customActionItem2'); - - expect(action1.text()).toBe('Action 1'); - expect(action2.text()).toBe('Action 2'); + const singleDocumentView = findTestSubject(component, 'docTableRowAction'); + expect(singleDocumentView.length).toBeFalsy(); + }); }); - it('should allow disabling default actions', async () => { - mockFlyoutCustomization.actions = { - defaultActions: { - viewSingleDocument: { disabled: true }, - viewSurroundingDocument: { disabled: true }, - }, - }; + describe('when content is customized', () => { + it('should display the component passed to the Content customization', async () => { + mockFlyoutCustomization.Content = () => ( + Custom content + ); + + const { component } = await mountComponent({}); + + const customContent = findTestSubject(component, 'flyoutCustomContent'); + + expect(customContent.text()).toBe('Custom content'); + }); + + it('should provide a doc property to display details about the current document overview', async () => { + mockFlyoutCustomization.Content = ({ doc }) => { + return ( + {doc.flattened.message as string} + ); + }; + + const { component } = await mountComponent({}); + + const customContent = findTestSubject(component, 'flyoutCustomContent'); + + expect(customContent.text()).toBe('test1'); + }); + + it('should provide an actions prop collection to optionally update the grid content', async () => { + mockFlyoutCustomization.Content = ({ actions }) => ( + <> +