From 3a1c7c6f5c1d11adeb7499a91a14408dfe5f068b Mon Sep 17 00:00:00 2001 From: tygao Date: Fri, 20 Sep 2024 10:57:56 +0800 Subject: [PATCH] add tests Signed-off-by: tygao --- .../generate_popover_body.test.tsx | 135 ++++++++++++++++++ .../generate_popover_body.tsx | 5 +- public/utils/alerting.ts | 1 - 3 files changed, 137 insertions(+), 4 deletions(-) diff --git a/public/components/incontext_insight/generate_popover_body.test.tsx b/public/components/incontext_insight/generate_popover_body.test.tsx index 2581d776..4b3c1302 100644 --- a/public/components/incontext_insight/generate_popover_body.test.tsx +++ b/public/components/incontext_insight/generate_popover_body.test.tsx @@ -10,9 +10,16 @@ import { GeneratePopoverBody } from './generate_popover_body'; import { HttpSetup } from '../../../../../src/core/public'; import { SUMMARY_ASSISTANT_API } from '../../../common/constants/llm'; import { usageCollectionPluginMock } from '../../../../../src/plugins/usage_collection/public/mocks'; +import { coreMock } from '../../../../../src/core/public/mocks'; +import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks'; jest.mock('../../services'); +jest.mock('../../utils', () => ({ + createIndexPatterns: jest.fn().mockResolvedValue('index pattern'), + buildUrlQuery: jest.fn().mockResolvedValue('query'), +})); + const mockToasts = { addDanger: jest.fn(), }; @@ -33,6 +40,36 @@ const mockHttpSetup: HttpSetup = ({ post: mockPost, } as unknown) as HttpSetup; // Mocking HttpSetup +const mockDSL = `{ + "query": { + "bool": { + "filter": [ + { + "range": { + "timestamp": { + "from": "2024-09-06T04:02:52||-1h", + "to": "2024-09-06T04:02:52", + "include_lower": true, + "include_upper": true, + "boost": 1 + } + } + }, + { + "term": { + "FlightDelay": { + "value": "true", + "boost": 1 + } + } + } + ], + "adjust_pure_negative": true, + "boost": 1 + } + } +}`; + describe('GeneratePopoverBody', () => { const incontextInsightMock = { contextProvider: jest.fn(), @@ -240,4 +277,102 @@ describe('GeneratePopoverBody', () => { // insight tip icon is not visible for this alert expect(screen.queryAllByLabelText('How was this generated?')).toHaveLength(0); }); + + it('should not display discover link if monitor type is not query_level_monitor or bucket_level_monitor', async () => { + incontextInsightMock.contextProvider = jest.fn().mockResolvedValue({ + additionalInfo: { + dsl: mockDSL, + index: 'mock_index', + dataSourceId: `test-data-source-id`, + monitorType: 'mock_type', + }, + }); + mockPost.mockImplementation((path: string, body) => { + let value; + switch (path) { + case SUMMARY_ASSISTANT_API.SUMMARIZE: + value = { + summary: 'Generated summary content', + insightAgentIdExists: true, + }; + break; + + case SUMMARY_ASSISTANT_API.INSIGHT: + value = 'Generated insight content'; + break; + + default: + return null; + } + return Promise.resolve(value); + }); + + const { queryByText } = render( + + ); + + await waitFor(() => { + expect(queryByText('Discover details')).not.toBeInTheDocument(); + }); + }); + + it('handle navigate to discover after clicking link', async () => { + incontextInsightMock.contextProvider = jest.fn().mockResolvedValue({ + additionalInfo: { + dsl: mockDSL, + index: 'mock_index', + dataSourceId: `test-data-source-id`, + monitorType: 'query_level_monitor', + }, + }); + mockPost.mockImplementation((path: string, body) => { + let value; + switch (path) { + case SUMMARY_ASSISTANT_API.SUMMARIZE: + value = { + summary: 'Generated summary content', + insightAgentIdExists: true, + }; + break; + + case SUMMARY_ASSISTANT_API.INSIGHT: + value = 'Generated insight content'; + break; + + default: + return null; + } + return Promise.resolve(value); + }); + + const coreStart = coreMock.createStart(); + const dataStart = dataPluginMock.createStartContract(); + const getStartServices = jest.fn().mockResolvedValue([ + coreStart, + { + data: dataStart, + }, + ]); + const { getByText } = render( + + ); + + await waitFor(() => { + const button = getByText('Discover details'); + expect(button).toBeInTheDocument(); + fireEvent.click(button); + expect(coreStart.application.navigateToUrl).toHaveBeenCalledWith( + 'data-explorer/discover#?query' + ); + }); + }); }); diff --git a/public/components/incontext_insight/generate_popover_body.tsx b/public/components/incontext_insight/generate_popover_body.tsx index ad75f2fb..76f5950f 100644 --- a/public/components/incontext_insight/generate_popover_body.tsx +++ b/public/components/incontext_insight/generate_popover_body.tsx @@ -135,7 +135,7 @@ export const GeneratePopoverBody: React.FC<{ defaultMessage: 'Generate summary error', }) ); - // closePopover(); + closePopover(); }); }; @@ -181,7 +181,6 @@ export const GeneratePopoverBody: React.FC<{ const handleNavigateToDiscover = async () => { const context = await incontextInsight?.contextProvider?.(); - console.log('handleNavigateToDiscover context', context); const dsl = context?.additionalInfo?.dsl; const indexName = context?.additionalInfo?.index; if (!dsl || !indexName) return; @@ -316,7 +315,7 @@ export const GeneratePopoverBody: React.FC<{ {displayDiscoverButton && ( {i18n.translate('assistantDashboards.incontextInsight.discover', { - defaultMessage: 'Deep dive in Discover', + defaultMessage: 'Discover details', })} )} diff --git a/public/utils/alerting.ts b/public/utils/alerting.ts index 1fa489cf..cf1712d0 100644 --- a/public/utils/alerting.ts +++ b/public/utils/alerting.ts @@ -40,7 +40,6 @@ export const createIndexPatterns = async ( id: dataSourceId, } : undefined; - console.log('dataSourceRef', dataSourceId, dataSourceRef); try { pattern = await dataStart.indexPatterns.createAndSave({ id: '',