From 3f78f192cabe8c7f6d286dd8793a2bf24fae7026 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 4 Sep 2018 20:02:48 +0200 Subject: [PATCH 01/14] [ML] jest tests for exploreSeries. --- .../explorer_charts/explore_series.js | 2 +- .../explorer_charts/explore_series.test.js | 63 +++++++++++++++++++ .../explorer_charts/explorer_chart.test.js | 40 +----------- .../explorer_charts_container.js | 5 +- .../explorer_charts_container_directive.js | 2 - .../explorer_charts/mock_series_config.js | 43 +++++++++++++ 6 files changed, 111 insertions(+), 44 deletions(-) create mode 100644 x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.test.js create mode 100644 x-pack/plugins/ml/public/explorer/explorer_charts/mock_series_config.js diff --git a/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.js b/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.js index 48b11391f5eb8..fcd70b67c3273 100644 --- a/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.js +++ b/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.js @@ -69,6 +69,6 @@ export function exploreSeries(series) { path += '/app/ml#/timeseriesexplorer'; path += '?_g=' + _g; path += '&_a=' + encodeURIComponent(_a); - window.open(path, '_blank'); + return path; } diff --git a/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.test.js b/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.test.js new file mode 100644 index 0000000000000..0eefff73faa58 --- /dev/null +++ b/x-pack/plugins/ml/public/explorer/explorer_charts/explore_series.test.js @@ -0,0 +1,63 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { seriesConfig } from './mock_series_config'; + +jest.mock('ui/chrome', + () => ({ + getBasePath: () => { + return ''; + }, + getUiSettingsClient: () => { + return { + get: (key) => { + switch (key) { + case 'timepicker:timeDefaults': + return { from: 'now-15m', to: 'now', mode: 'quick' }; + case 'timepicker:refreshIntervalDefaults': + return { pause: false, value: 0 }; + default: + throw new Error(`Unexpected config key: ${key}`); + } + } + }; + }, + }), { virtual: true }); + +jest.mock('ui/timefilter/lib/parse_querystring', + () => ({ + parseQueryString: () => { + return { + // Can not access local variable from within a mock + forceNow: global.nowTime + }; + }, + }), { virtual: true }); + +import moment from 'moment'; +import { timefilter } from 'ui/timefilter'; + +import { exploreSeries } from './explore_series'; + +timefilter.enableTimeRangeSelector(); +timefilter.enableAutoRefreshSelector(); +timefilter.setTime({ + from: moment(seriesConfig.selectedEarliest).toISOString(), + to: moment(seriesConfig.selectedLatest).toISOString() +}); + +describe('exploreSeries', () => { + test('get timeseriesexplorer link', () => { + const link = exploreSeries(seriesConfig); + const expectedLink = `/app/ml#/timeseriesexplorer?_g=(ml:(jobIds:!(population-03)),` + + `refreshInterval:(display:Off,pause:!f,value:0),time:(from:'2017-02-23T00:00:00.000Z',mode:absolute,` + + `to:'2017-02-23T23:59:59.999Z'))&_a=(filters%3A!()%2CmlTimeSeriesExplorer%3A(detectorIndex%3A0%2Centities%3A` + + `(nginx.access.remote_ip%3A'72.57.0.53')%2Czoom%3A(from%3A'2017-02-19T20%3A00%3A00.000Z'%2Cto%3A'2017-02-27T04%3A00%3A00.000Z'))` + + `%2Cquery%3A(query_string%3A(analyze_wildcard%3A!t%2Cquery%3A'*')))`; + + expect(link).toBe(expectedLink); + }); +}); diff --git a/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_chart.test.js b/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_chart.test.js index ce40308003f5f..7a9b5792d2787 100644 --- a/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_chart.test.js +++ b/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_chart.test.js @@ -4,6 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ +import { seriesConfig } from './mock_series_config'; + // Mock TimeBuckets and mlFieldFormatService, they don't play well // with the jest based test setup yet. jest.mock('ui/time_buckets', () => ({ @@ -26,44 +28,6 @@ import { ExplorerChart } from './explorer_chart'; import { chartLimits } from '../../util/chart_utils'; describe('ExplorerChart', () => { - const seriesConfig = { - jobId: 'population-03', - detectorIndex: 0, - metricFunction: 'sum', - timeField: '@timestamp', - interval: '1h', - datafeedConfig: { - datafeed_id: 'datafeed-population-03', - job_id: 'population-03', - query_delay: '60s', - frequency: '600s', - indices: ['filebeat-7.0.0*'], - types: ['doc'], - query: { match_all: { boost: 1 } }, - scroll_size: 1000, - chunking_config: { mode: 'auto' }, - state: 'stopped' - }, - metricFieldName: 'nginx.access.body_sent.bytes', - functionDescription: 'sum', - bucketSpanSeconds: 3600, - detectorLabel: 'high_sum(nginx.access.body_sent.bytes) over nginx.access.remote_ip (population-03)', - fieldName: 'nginx.access.body_sent.bytes', - entityFields: [{ - fieldName: 'nginx.access.remote_ip', - fieldValue: '72.57.0.53', - $$hashKey: 'object:813' - }], - infoTooltip: `
job ID: population-03
- aggregation interval: 1h
chart function: sum nginx.access.body_sent.bytes
- nginx.access.remote_ip: 72.57.0.53
`, - loading: false, - plotEarliest: 1487534400000, - plotLatest: 1488168000000, - selectedEarliest: 1487808000000, - selectedLatest: 1487894399999 - }; - const mlSelectSeverityServiceMock = { state: { get: () => ({ diff --git a/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.js b/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.js index 2fb75120fdb44..44115109cb467 100644 --- a/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.js +++ b/x-pack/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.js @@ -9,11 +9,11 @@ import React from 'react'; import { EuiIconTip } from '@elastic/eui'; +import { exploreSeries } from './explore_series'; import { ExplorerChart } from './explorer_chart'; import { ExplorerChartTooltip } from './explorer_chart_tooltip'; export function ExplorerChartsContainer({ - exploreSeries, seriesToPlot, layoutCellsPerChart, tooManyBuckets, @@ -60,7 +60,7 @@ export function ExplorerChartsContainer({ color="warning" /> )} - exploreSeries(series)}> + window.open(exploreSeries(series), '_blank')}> View