From 51b1f4febf242ea265c114527de0bdbe90be2f4c Mon Sep 17 00:00:00 2001 From: Jason Rhodes Date: Wed, 7 Nov 2018 11:01:28 -0500 Subject: [PATCH] Fixes bug with rison decode on Kibana links (#25214) (#25284) * Fixes bug with rison decode vs encode * Adds and updates tests for kibana link component --- .../__test__/__snapshots__/url.test.js.snap | 31 ++++++++++++++++--- .../apm/public/utils/__test__/url.test.js | 19 ++++++++++-- x-pack/plugins/apm/public/utils/url.tsx | 20 +++++------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/x-pack/plugins/apm/public/utils/__test__/__snapshots__/url.test.js.snap b/x-pack/plugins/apm/public/utils/__test__/__snapshots__/url.test.js.snap index 609a57249ff0..8fb163cbd10b 100644 --- a/x-pack/plugins/apm/public/utils/__test__/__snapshots__/url.test.js.snap +++ b/x-pack/plugins/apm/public/utils/__test__/__snapshots__/url.test.js.snap @@ -10,13 +10,34 @@ exports[`RelativeLinkComponent should render correct markup 1`] = ` `; -exports[`UnconnectedKibanaLink should render correct markup 1`] = ` - Go to Discover - + +`; + +exports[`UnconnectedKibanaLink should include existing _g values in link href 2`] = ` + + Go to Discover + +`; + +exports[`UnconnectedKibanaLink should render correct markup 1`] = ` + + Go to Discover + `; exports[`ViewMLJob should render component 1`] = ` diff --git a/x-pack/plugins/apm/public/utils/__test__/url.test.js b/x-pack/plugins/apm/public/utils/__test__/url.test.js index be8c87d28b2c..be5c80d98a52 100644 --- a/x-pack/plugins/apm/public/utils/__test__/url.test.js +++ b/x-pack/plugins/apm/public/utils/__test__/url.test.js @@ -197,7 +197,7 @@ describe('UnconnectedKibanaLink', () => { } }; - wrapper = mount( + wrapper = shallow( { }); it('should have correct url', () => { - expect(wrapper.find('a').prop('href')).toBe( + expect(wrapper.find('EuiLink').prop('href')).toBe( 'myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:\'context.service.name:"myServiceName" AND error.grouping_key:"myGroupId"\'),sort:(\'@timestamp\':desc))&_g=(time:(from:now-24h,mode:quick,to:now))' ); }); it('should render correct markup', () => { - expect(toJson(wrapper)).toMatchSnapshot(); + expect(wrapper).toMatchSnapshot(); + }); + + it('should include existing _g values in link href', () => { + wrapper.setProps({ + location: { + search: + '?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-7d,mode:relative,to:now-1d))' + } + }); + expect(wrapper).toMatchSnapshot(); + + wrapper.setProps({ location: { search: '?_g=H@whatever' } }); + expect(wrapper).toMatchSnapshot(); }); }); diff --git a/x-pack/plugins/apm/public/utils/url.tsx b/x-pack/plugins/apm/public/utils/url.tsx index 6982cb1322e8..c2b2b2613e63 100644 --- a/x-pack/plugins/apm/public/utils/url.tsx +++ b/x-pack/plugins/apm/public/utils/url.tsx @@ -6,7 +6,7 @@ import { EuiLink, EuiLinkAnchorProps } from '@elastic/eui'; import createHistory from 'history/createHashHistory'; -import { get, isEmpty, isPlainObject, mapValues } from 'lodash'; +import { get, isPlainObject, mapValues } from 'lodash'; import qs from 'querystring'; import React from 'react'; import { connect } from 'react-redux'; @@ -16,6 +16,10 @@ import chrome from 'ui/chrome'; import url from 'url'; import { StringMap } from '../../typings/common'; +// Kibana default set in: https://github.com/elastic/kibana/blob/e13e47fc4eb6112f2a5401408e9f765eae90f55d/x-pack/plugins/apm/public/utils/timepicker/index.js#L31-L35 +// TODO: store this in config or a shared constant? +const DEFAULT_KIBANA_TIME_RANGE = '(time:(from:now-24h,mode:quick,to:now))'; + interface ViewMlJobArgs { serviceName: string; transactionType: string; @@ -152,17 +156,6 @@ export function RelativeLinkComponent({ // The two components have different APIs: `path` vs `pathname` and one uses EuiLink the other react-router's Link (which behaves differently) // Suggestion: Deprecate RelativeLink, and clean up KibanaLink -// _g is always retrieved from the url - it can not be changed via query prop -function getGArg(g: string) { - // use "g" if it's set in the url - if (g && !isEmpty(rison.decode(g))) { - return g; - } - - // use default 24h (default set in: https://github.com/elastic/kibana/blob/e13e47fc4eb6112f2a5401408e9f765eae90f55d/x-pack/plugins/apm/public/utils/timepicker/index.js#L31-L35) - return '(time:(from:now-24h,mode:quick,to:now))'; -} - export interface KibanaLinkArgs { location: { search?: string; @@ -194,7 +187,8 @@ export const UnconnectedKibanaLink: React.SFC = ({ const currentQuery = toQuery(location.search); const nextQuery = { ...query, - _g: getGArg(currentQuery._g), + // use "_g" if it's set in the url, otherwise use default + _g: currentQuery._g || DEFAULT_KIBANA_TIME_RANGE, _a: query._a ? rison.encode(query._a) : '' };