Skip to content

Commit

Permalink
move dropdown options function to own file and update jest test
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Apr 18, 2023
1 parent 1134e2c commit 00c5841
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
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';

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,
Expand Down Expand Up @@ -54,15 +67,15 @@ 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,
setEditCustomUrl: setEditCustomUrlFn,
savedCustomUrls,
dashboards,
dataViewListItems,
queryEntityFieldNames,
job,
};

return shallow(<CustomUrlEditor {...props} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 [];
}

0 comments on commit 00c5841

Please sign in to comment.