forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] Setting for default env for service inventory (elastic#126151)
- Loading branch information
1 parent
1bc178f
commit 405ef9e
Showing
7 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
x-pack/plugins/apm/public/components/shared/redirect_with_default_environment/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { RouterProvider } from '@kbn/typed-react-router-config'; | ||
import { render } from '@testing-library/react'; | ||
import { createMemoryHistory, Location, MemoryHistory } from 'history'; | ||
import qs from 'query-string'; | ||
import { RedirectWithDefaultEnvironment } from './'; | ||
import { apmRouter } from '../../routing/apm_route_config'; | ||
import * as useApmPluginContextExports from '../../../context/apm_plugin/use_apm_plugin_context'; | ||
import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; | ||
|
||
describe('RedirectWithDefaultEnvironment', () => { | ||
let history: MemoryHistory; | ||
|
||
beforeEach(() => { | ||
history = createMemoryHistory(); | ||
}); | ||
|
||
function renderUrl( | ||
location: Pick<Location, 'pathname' | 'search'>, | ||
defaultSetting: string | ||
) { | ||
history.replace(location); | ||
|
||
jest | ||
.spyOn(useApmPluginContextExports, 'useApmPluginContext') | ||
.mockReturnValue({ | ||
core: { | ||
uiSettings: { | ||
get: () => defaultSetting, | ||
}, | ||
}, | ||
} as any); | ||
|
||
return render( | ||
<RouterProvider history={history} router={apmRouter as any}> | ||
<RedirectWithDefaultEnvironment> | ||
<>Foo</> | ||
</RedirectWithDefaultEnvironment> | ||
</RouterProvider> | ||
); | ||
} | ||
|
||
it('eventually renders the child element', async () => { | ||
const element = renderUrl( | ||
{ | ||
pathname: '/services', | ||
search: location.search, | ||
}, | ||
'' | ||
); | ||
|
||
await expect(element.findByText('Foo')).resolves.not.toBeUndefined(); | ||
|
||
// assertion to make sure our element test actually works | ||
await expect(element.findByText('Bar')).rejects.not.toBeUndefined(); | ||
}); | ||
|
||
it('redirects to ENVIRONMENT_ALL if not set', async () => { | ||
renderUrl( | ||
{ | ||
pathname: '/services', | ||
search: location.search, | ||
}, | ||
'' | ||
); | ||
|
||
expect(qs.parse(history.entries[0].search).environment).toEqual( | ||
ENVIRONMENT_ALL.value | ||
); | ||
}); | ||
|
||
it('redirects to the default environment if set', () => { | ||
renderUrl( | ||
{ | ||
pathname: '/services', | ||
search: location.search, | ||
}, | ||
'production' | ||
); | ||
|
||
expect(qs.parse(history.entries[0].search).environment).toEqual( | ||
'production' | ||
); | ||
}); | ||
|
||
it('does not redirect when an environment has been set', () => { | ||
renderUrl( | ||
{ | ||
pathname: '/services', | ||
search: qs.stringify({ | ||
environment: 'development', | ||
}), | ||
}, | ||
'production' | ||
); | ||
|
||
expect(qs.parse(history.entries[0].search).environment).toEqual( | ||
'development' | ||
); | ||
}); | ||
|
||
it('does not redirect for the service overview', () => { | ||
renderUrl( | ||
{ | ||
pathname: '/services/opbeans-java', | ||
search: location.search, | ||
}, | ||
'' | ||
); | ||
|
||
expect(qs.parse(history.entries[0].search).environment).toBeUndefined(); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/apm/public/components/shared/redirect_with_default_environment/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 { useLocation, Redirect } from 'react-router-dom'; | ||
import qs from 'query-string'; | ||
import React from 'react'; | ||
import { defaultApmServiceEnvironment } from '../../../../../observability/common'; | ||
import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; | ||
import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; | ||
|
||
export function RedirectWithDefaultEnvironment({ | ||
children, | ||
}: { | ||
children: React.ReactElement; | ||
}) { | ||
const { core } = useApmPluginContext(); | ||
const location = useLocation(); | ||
|
||
const query = qs.parse(location.search); | ||
|
||
if ('environment' in query) { | ||
return children; | ||
} | ||
|
||
if (location.pathname === '/services' || location.pathname === '/services/') { | ||
const defaultServiceEnvironment = | ||
core.uiSettings.get<string>(defaultApmServiceEnvironment) || | ||
ENVIRONMENT_ALL.value; | ||
|
||
return ( | ||
<Redirect | ||
to={qs.stringifyUrl({ | ||
url: location.pathname, | ||
query: { | ||
...query, | ||
environment: defaultServiceEnvironment, | ||
}, | ||
})} | ||
/> | ||
); | ||
} | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters