From 21dc82ff56f771c51c30bd43ad6024284c5fe301 Mon Sep 17 00:00:00 2001 From: Ievgen Sorokopud Date: Tue, 5 Nov 2024 19:53:15 +0100 Subject: [PATCH 1/2] [Security Solution][Bug] Incorrect message as Duplicate entry? shown on creating New Knowledge Base index. (#198892) --- .../index.tsx | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx b/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx index 904ceba7a1f6f..86b3594daa3cd 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.tsx @@ -166,26 +166,31 @@ export const KnowledgeBaseSettingsManagement: React.FC = React.memo(({ d isRefetching: kbStatus?.is_setup_in_progress, }); + const resetStateAndCloseFlyout = useCallback(() => { + setOriginalEntry(undefined); + setSelectedEntry(undefined); + setDuplicateKBItem(null); + closeFlyout(); + }, [closeFlyout]); + // Flyout Save/Cancel Actions const onSaveConfirmed = useCallback(async () => { if (isKnowledgeBaseEntryResponse(selectedEntry)) { await updateEntries([selectedEntry]); - closeFlyout(); + resetStateAndCloseFlyout(); } else if (isKnowledgeBaseEntryCreateProps(selectedEntry)) { if (originalEntry) { setDuplicateKBItem(selectedEntry); return; } await createEntry(selectedEntry); - closeFlyout(); + resetStateAndCloseFlyout(); } - }, [selectedEntry, originalEntry, updateEntries, closeFlyout, createEntry]); + }, [selectedEntry, updateEntries, resetStateAndCloseFlyout, originalEntry, createEntry]); const onSaveCancelled = useCallback(() => { - setOriginalEntry(undefined); - setSelectedEntry(undefined); - closeFlyout(); - }, [closeFlyout]); + resetStateAndCloseFlyout(); + }, [resetStateAndCloseFlyout]); const { value: existingIndices } = useAsync(() => { const indices: string[] = []; @@ -323,10 +328,9 @@ export const KnowledgeBaseSettingsManagement: React.FC = React.memo(({ d const handleDuplicateEntry = useCallback(async () => { if (duplicateKBItem) { await createEntry(duplicateKBItem); - closeFlyout(); - setDuplicateKBItem(null); + resetStateAndCloseFlyout(); } - }, [closeFlyout, createEntry, duplicateKBItem]); + }, [createEntry, duplicateKBItem, resetStateAndCloseFlyout]); if (!enableKnowledgeBaseByDefault) { return ( From 0fc3889420f354e5f9d0b311a14c45e21e8c4691 Mon Sep 17 00:00:00 2001 From: Ievgen Sorokopud Date: Tue, 5 Nov 2024 20:04:17 +0100 Subject: [PATCH 2/2] Unit test --- .../index.test.tsx | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.test.tsx index cfc8d2d3d52f9..180b88fc3cdc8 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/index.test.tsx @@ -425,6 +425,63 @@ describe('KnowledgeBaseSettingsManagement', () => { expect(mockCreateEntry).toHaveBeenCalledWith({ ...mockData[3], users: undefined }); }); + it('does not show duplicate entry modal on new document entry creation', async () => { + // Covers the BUG: https://github.com/elastic/kibana/issues/198892 + const closeFlyoutMock = jest.fn(); + (useFlyoutModalVisibility as jest.Mock).mockReturnValue({ + isFlyoutOpen: true, + openFlyout: jest.fn(), + closeFlyout: closeFlyoutMock, + }); + render(, { + wrapper, + }); + + await waitFor(() => { + fireEvent.click(screen.getAllByTestId('edit-button')[3]); + }); + expect(screen.getByTestId('flyout')).toBeVisible(); + + await waitFor(() => { + expect(screen.getByText('Edit document entry')).toBeInTheDocument(); + }); + + await waitFor(() => { + fireEvent.click(screen.getByTestId('sharing-select')); + fireEvent.click(screen.getByTestId('sharing-private-option')); + fireEvent.click(screen.getByTestId('save-button')); + }); + + expect(screen.getByTestId('create-duplicate-entry-modal')).toBeInTheDocument(); + await waitFor(() => { + fireEvent.click(screen.getByTestId('confirmModalConfirmButton')); + }); + expect(screen.queryByTestId('create-duplicate-entry-modal')).not.toBeInTheDocument(); + await waitFor(() => { + expect(mockCreateEntry).toHaveBeenCalledTimes(1); + }); + + // Create a new document entry + await waitFor(() => { + fireEvent.click(screen.getByTestId('addEntry')); + }); + await waitFor(() => { + fireEvent.click(screen.getByTestId('addDocument')); + }); + + expect(screen.getByTestId('flyout')).toBeVisible(); + + await userEvent.type(screen.getByTestId('entryNameInput'), 'hi'); + await userEvent.type(screen.getByTestId('entryMarkdownInput'), 'hi'); + + await waitFor(() => { + fireEvent.click(screen.getByTestId('save-button')); + }); + + expect(screen.queryByTestId('create-duplicate-entry-modal')).not.toBeInTheDocument(); + expect(closeFlyoutMock).toHaveBeenCalled(); + }); + it('shows warning icon for index entries with missing indices', async () => { render(, { wrapper,