From 00c58413e8d0108083ca7b29aa59a1a0e58752b4 Mon Sep 17 00:00:00 2001 From: Melissa Date: Tue, 18 Apr 2023 12:54:46 -0600 Subject: [PATCH] move dropdown options function to own file and update jest test --- .../custom_url_editor/editor.test.tsx | 17 ++++++++++-- .../custom_urls/custom_url_editor/editor.tsx | 20 ++------------ .../custom_url_editor/get_dropdown_options.ts | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/get_dropdown_options.ts diff --git a/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.test.tsx b/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.test.tsx index ce7f31df4e86c..264d141573768 100644 --- a/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.test.tsx +++ b/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.test.tsx @@ -9,6 +9,18 @@ import { shallow } from 'enzyme'; jest.mock('../../../services/job_service', () => 'mlJobService'); +jest.mock('../../../contexts/kibana', () => ({ + useMlKibana: () => ({ + services: { + application: { + navigateToApp: jest.fn(), + }, + data: { + dataViews: [], + }, + }, + }), +})); import React from 'react'; @@ -16,6 +28,7 @@ import { CustomUrlEditor } from './editor'; import { TIME_RANGE_TYPE, URL_TYPE } from './constants'; import { CustomUrlSettings } from './utils'; import { DataViewListItem } from '@kbn/data-views-plugin/common'; +import { Job } from '../../../../../common/types/anomaly_detection_jobs'; function prepareTest( customUrl: CustomUrlSettings, @@ -54,7 +67,7 @@ function prepareTest( { id: 'pattern2', title: 'Data view 2' }, ] as DataViewListItem[]; - const queryEntityFieldNames = ['airline']; + const job = { job_id: 'id', analysis_config: { influencers: ['airline'] } } as Job; const props = { customUrl, @@ -62,7 +75,7 @@ function prepareTest( savedCustomUrls, dashboards, dataViewListItems, - queryEntityFieldNames, + job, }; return shallow(); diff --git a/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.tsx b/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.tsx index 14e09b52c606e..315c60fab6a6f 100644 --- a/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.tsx +++ b/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/editor.tsx @@ -28,16 +28,13 @@ import { DataViewListItem } from '@kbn/data-views-plugin/common'; import { DataView } from '@kbn/data-views-plugin/public'; import { CustomUrlSettings, isValidCustomUrlSettingsTimeRange } from './utils'; import { isValidLabel } from '../../../util/custom_url_utils'; -import { - isDataFrameAnalyticsConfigs, - type DataFrameAnalyticsConfig, -} from '../../../../../common/types/data_frame_analytics'; +import { type DataFrameAnalyticsConfig } from '../../../../../common/types/data_frame_analytics'; import { Job, isAnomalyDetectionJob } from '../../../../../common/types/anomaly_detection_jobs'; import { TIME_RANGE_TYPE, TimeRangeType, URL_TYPE } from './constants'; import { UrlConfig } from '../../../../../common/types/custom_urls'; -import { getQueryEntityFieldNames, getSupportedFieldNames } from './utils'; import { useMlKibana } from '../../../contexts/kibana'; +import { getDropDownOptions } from './get_dropdown_options'; function getLinkToOptions() { return [ @@ -62,19 +59,6 @@ function getLinkToOptions() { ]; } -function getDropDownOptions( - isFirstRender: boolean, - job: Job | DataFrameAnalyticsConfig, - dataView?: DataView -) { - if (isAnomalyDetectionJob(job) && isFirstRender) { - return getQueryEntityFieldNames(job); - } else if ((isDataFrameAnalyticsConfigs(job) || !isFirstRender) && dataView !== undefined) { - return getSupportedFieldNames(job, dataView); - } - return []; -} - interface CustomUrlEditorProps { customUrl: CustomUrlSettings | undefined; setEditCustomUrl: (url: CustomUrlSettings) => void; diff --git a/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/get_dropdown_options.ts b/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/get_dropdown_options.ts new file mode 100644 index 0000000000000..6f94514252dba --- /dev/null +++ b/x-pack/plugins/ml/public/application/components/custom_urls/custom_url_editor/get_dropdown_options.ts @@ -0,0 +1,27 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { DataView } from '@kbn/data-views-plugin/public'; +import { + isDataFrameAnalyticsConfigs, + type DataFrameAnalyticsConfig, +} from '../../../../../common/types/data_frame_analytics'; +import { Job, isAnomalyDetectionJob } from '../../../../../common/types/anomaly_detection_jobs'; +import { getQueryEntityFieldNames, getSupportedFieldNames } from './utils'; + +export function getDropDownOptions( + isFirstRender: boolean, + job: Job | DataFrameAnalyticsConfig, + dataView?: DataView +) { + if (isAnomalyDetectionJob(job) && isFirstRender) { + return getQueryEntityFieldNames(job); + } else if ((isDataFrameAnalyticsConfigs(job) || !isFirstRender) && dataView !== undefined) { + return getSupportedFieldNames(job, dataView); + } + return []; +}