-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
455 additions
and
59 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
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
70 changes: 70 additions & 0 deletions
70
x-pack/plugins/endpoint/public/applications/endpoint/mocks/app_context_render.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,70 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { createMemoryHistory } from 'history'; | ||
import { render as reactRender, RenderOptions, RenderResult } from '@testing-library/react'; | ||
import { appStoreFactory } from '../store'; | ||
import { coreMock } from '../../../../../../../src/core/public/mocks'; | ||
import { EndpointPluginStartDependencies } from '../../../plugin'; | ||
import { depsStartMock } from './dependencies_start_mock'; | ||
import { AppRootProvider } from '../view/app_root_provider'; | ||
|
||
type UiRender = (ui: React.ReactElement, options?: RenderOptions) => RenderResult; | ||
|
||
/** | ||
* Mocked app root context renderer | ||
*/ | ||
interface AppContextTestRender { | ||
store: ReturnType<typeof appStoreFactory>; | ||
history: ReturnType<typeof createMemoryHistory>; | ||
coreStart: ReturnType<typeof coreMock.createStart>; | ||
depsStart: EndpointPluginStartDependencies; | ||
/** | ||
* A wrapper around `AppRootContext` component. Uses the mocked modules as input to the | ||
* `AppRootContext` | ||
*/ | ||
AppWrapper: React.FC<any>; | ||
/** | ||
* Renders the given UI within the created `AppWrapper` providing the given UI a mocked | ||
* endpoint runtime context environment | ||
*/ | ||
render: UiRender; | ||
} | ||
|
||
/** | ||
* Creates a mocked endpoint app context custom renderer that can be used to render | ||
* component that depend upon the application's surrounding context providers. | ||
* Factory also returns the content that was used to create the custom renderer, allowing | ||
* for further customization. | ||
*/ | ||
export const createAppRootMockRenderer = (): AppContextTestRender => { | ||
const history = createMemoryHistory<never>(); | ||
const coreStart = coreMock.createStart({ basePath: '/mock' }); | ||
const depsStart = depsStartMock(); | ||
const store = appStoreFactory({ coreStart, depsStart }); | ||
const AppWrapper: React.FunctionComponent<{ children: React.ReactElement }> = ({ children }) => ( | ||
<AppRootProvider store={store} history={history} coreStart={coreStart} depsStart={depsStart}> | ||
{children} | ||
</AppRootProvider> | ||
); | ||
const render: UiRender = (ui, options) => { | ||
// @ts-ignore | ||
return reactRender(ui, { | ||
wrapper: AppWrapper, | ||
...options, | ||
}); | ||
}; | ||
|
||
return { | ||
store, | ||
history, | ||
coreStart, | ||
depsStart, | ||
AppWrapper, | ||
render, | ||
}; | ||
}; |
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
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/endpoint/public/applications/endpoint/mocks/index.ts
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,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export * from './dependencies_start_mock'; | ||
export * from './app_context_render'; |
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
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/endpoint/public/applications/endpoint/view/app_root_provider.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,58 @@ | ||
/* | ||
* 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 React, { memo, ReactNode, useMemo } from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { I18nProvider } from '@kbn/i18n/react'; | ||
import { Router } from 'react-router-dom'; | ||
import { History } from 'history'; | ||
import { CoreStart } from 'kibana/public'; | ||
import { useObservable } from 'react-use'; | ||
import { EuiThemeProvider } from '../../../../../../legacy/common/eui_styled_components'; | ||
import { KibanaContextProvider } from '../../../../../../../src/plugins/kibana_react/public'; | ||
import { appStoreFactory } from '../store'; | ||
import { RouteCapture } from './route_capture'; | ||
import { EndpointPluginStartDependencies } from '../../../plugin'; | ||
|
||
/** | ||
* Provides the context for rendering the endpoint app | ||
*/ | ||
export const AppRootProvider = memo<{ | ||
store: ReturnType<typeof appStoreFactory>; | ||
history: History; | ||
coreStart: CoreStart; | ||
depsStart: EndpointPluginStartDependencies; | ||
children: ReactNode | ReactNode[]; | ||
}>( | ||
({ | ||
store, | ||
history, | ||
coreStart: { http, notifications, uiSettings, application }, | ||
depsStart: { data }, | ||
children, | ||
}) => { | ||
const isDarkMode = useObservable<boolean>(uiSettings.get$('theme:darkMode')); | ||
const services = useMemo(() => ({ http, notifications, application, data }), [ | ||
application, | ||
data, | ||
http, | ||
notifications, | ||
]); | ||
return ( | ||
<Provider store={store}> | ||
<I18nProvider> | ||
<KibanaContextProvider services={services}> | ||
<EuiThemeProvider darkMode={isDarkMode}> | ||
<Router history={history}> | ||
<RouteCapture>{children}</RouteCapture> | ||
</Router> | ||
</EuiThemeProvider> | ||
</KibanaContextProvider> | ||
</I18nProvider> | ||
</Provider> | ||
); | ||
} | ||
); |
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
Oops, something went wrong.