Skip to content

Commit

Permalink
[Upgrade Assistant] Use AppContext for services instead of KibanaCont…
Browse files Browse the repository at this point in the history
…ext (elastic#109801)

* Remove kibana context dependency in favour of app context

* Add missing type to ContextValue

* Fix mock type

* Refactor app mount flow and types

* Refactor to use useServices hook

* Fix linter issues

* Keep mount_management_section and initialize breadcrumbs and api there

* Remove useServices and usePlugins in favour of just useAppContext

* Remove unnecessary mocks
  • Loading branch information
sabarasaba committed Oct 26, 2021
1 parent a47436d commit 3726381
Show file tree
Hide file tree
Showing 29 changed files with 247 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ import {
docLinksServiceMock,
notificationServiceMock,
applicationServiceMock,
httpServiceMock,
coreMock,
scopedHistoryMock,
} from 'src/core/public/mocks';
import { sharePluginMock } from 'src/plugins/share/public/mocks';
import { HttpSetup } from 'src/core/public';

import { mockKibanaSemverVersion } from '../../../common/constants';
import { apiService } from '../../../public/application/lib/api';
import { breadcrumbService } from '../../../public/application/lib/breadcrumbs';
import { dataPluginMock } from '../../../../../../src/plugins/data/public/mocks';
import { cloudMock } from '../../../../../../x-pack/plugins/cloud/public/mocks';

const servicesMock = {
api: apiService,
breadcrumbs: breadcrumbService,
data: dataPluginMock.createStartContract(),
};

// We'll mock these values to avoid testing the locators themselves.
const idToUrlMap = {
Expand All @@ -32,19 +42,30 @@ shareMock.url.locators.get = (id) => ({
getUrl: (): string | undefined => idToUrlMap[id],
});

export const getAppContextMock = (mockHttpClient: HttpSetup) => ({
http: mockHttpClient,
docLinks: docLinksServiceMock.createStartContract(),
export const getAppContextMock = () => ({
isReadOnlyMode: false,
kibanaVersionInfo: {
currentMajor: mockKibanaSemverVersion.major,
prevMajor: mockKibanaSemverVersion.major - 1,
nextMajor: mockKibanaSemverVersion.major + 1,
},
notifications: notificationServiceMock.createStartContract(),
isReadOnlyMode: false,
api: apiService,
breadcrumbs: breadcrumbService,
getUrlForApp: applicationServiceMock.createStartContract().getUrlForApp,
deprecations: deprecationsServiceMock.createStartContract(),
share: shareMock,
services: {
...servicesMock,
core: {
...coreMock.createStart(),
http: httpServiceMock.createSetupContract(),
deprecations: deprecationsServiceMock.createStartContract(),
notifications: notificationServiceMock.createStartContract(),
docLinks: docLinksServiceMock.createStartContract(),
history: scopedHistoryMock.create(),
application: applicationServiceMock.createStartContract(),
},
},
plugins: {
share: shareMock,
cloud: {
...cloudMock.createSetup(),
isCloudEnabled: false,
},
},
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,43 @@
import React from 'react';
import axios from 'axios';
import SemVer from 'semver/classes/semver';
import { merge } from 'lodash';
// @ts-ignore
import axiosXhrAdapter from 'axios/lib/adapters/xhr';

import { MAJOR_VERSION } from '../../../common/constants';
import { HttpSetup } from 'src/core/public';

import { KibanaContextProvider } from '../../../public/shared_imports';
import { AppContextProvider } from '../../../public/application/app_context';
import { apiService } from '../../../public/application/lib/api';
import { breadcrumbService } from '../../../public/application/lib/breadcrumbs';
import { GlobalFlyout } from '../../../public/shared_imports';
import { kibanaContextMock } from './kibana_context.mock';
import { getAppContextMock } from './app_context.mock';
import { init as initHttpRequests } from './http_requests';
import { AppDependencies } from '../../../public/types';

const { GlobalFlyoutProvider } = GlobalFlyout;

const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });

export const kibanaVersion = new SemVer(MAJOR_VERSION);

export const WithAppDependencies =
(Comp: any, overrides: Record<string, unknown> = {}) =>
(props: Record<string, unknown>) => {
apiService.setup(mockHttpClient as unknown as HttpSetup);
breadcrumbService.setup(() => '');

const appContextMock = getAppContextMock(mockHttpClient as unknown as HttpSetup);

const { kibanaContextOverrides, ...appContextOverrides } = overrides;

return (
<KibanaContextProvider services={{ ...kibanaContextMock, ...(kibanaContextOverrides as {}) }}>
<AppContextProvider value={{ ...appContextMock, ...appContextOverrides }}>
<GlobalFlyoutProvider>
<Comp {...props} />
</GlobalFlyoutProvider>
</AppContextProvider>
</KibanaContextProvider>
);
};
export const WithAppDependencies = (Comp: any, overrides: Record<string, unknown> = {}) => (
props: Record<string, unknown>
) => {
apiService.setup((mockHttpClient as unknown) as HttpSetup);
breadcrumbService.setup(() => '');

const appContextMock = (getAppContextMock() as unknown) as AppDependencies;

return (
<AppContextProvider value={merge(appContextMock, overrides)}>
<GlobalFlyoutProvider>
<Comp {...props} />
</GlobalFlyoutProvider>
</AppContextProvider>
);
};

export const setupEnvironment = () => {
const { server, httpRequestsMockHelpers } = initHttpRequests();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ describe('Kibana deprecations', () => {
.mockReturnValue(kibanaDeprecationsMockResponse);

testBed = await setupKibanaPage({
deprecations: deprecationService,
services: {
core: {
deprecations: deprecationService,
},
},
});
});

Expand Down Expand Up @@ -177,7 +181,11 @@ describe('Kibana deprecations', () => {
.mockRejectedValue(new Error('Internal Server Error'));

testBed = await setupKibanaPage({
deprecations: deprecationService,
services: {
core: {
deprecations: deprecationService,
},
},
});
});

Expand Down Expand Up @@ -210,7 +218,11 @@ describe('Kibana deprecations', () => {
.mockReturnValue(kibanaDeprecationsMockResponse);

testBed = await setupKibanaPage({
deprecations: deprecationService,
services: {
core: {
deprecations: deprecationService,
},
},
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ describe('Overview - Fix deprecation issues step', () => {
.mockReturnValue(mockedResponses.kibanaDeprecations);

testBed = await setupOverviewPage({
deprecations: deprecationService,
services: {
core: {
deprecations: deprecationService,
},
},
});
});

Expand Down Expand Up @@ -172,7 +176,11 @@ describe('Overview - Fix deprecation issues step', () => {
deprecationService.getAllDeprecations = jest.fn().mockRejectedValue([]);

testBed = await setupOverviewPage({
deprecations: deprecationService,
services: {
core: {
deprecations: deprecationService,
},
},
});
});

Expand Down Expand Up @@ -201,7 +209,11 @@ describe('Overview - Fix deprecation issues step', () => {
.mockRejectedValue(new Error('Internal Server Error'));

testBed = await setupOverviewPage({
deprecations: deprecationService,
services: {
core: {
deprecations: deprecationService,
},
},
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Overview - Upgrade Step', () => {
test('Shows upgrade CTA and link to docs', async () => {
await act(async () => {
testBed = await setupOverviewPage({
kibanaContextOverrides: {
plugins: {
cloud: {
isCloudEnabled: true,
deploymentUrl:
Expand Down
40 changes: 15 additions & 25 deletions x-pack/plugins/upgrade_assistant/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@

import React from 'react';
import { Router, Switch, Route, Redirect } from 'react-router-dom';
import { I18nStart, ScopedHistory } from 'src/core/public';
import { ApplicationStart } from 'kibana/public';
import { ScopedHistory } from 'src/core/public';
import { GlobalFlyout } from '../shared_imports';

import { KibanaContextProvider, APP_WRAPPER_CLASS } from '../shared_imports';
import { AppServicesContext } from '../types';
import { AppContextProvider, ContextValue, useAppContext } from './app_context';
import { APP_WRAPPER_CLASS } from '../shared_imports';
import { AppContextProvider, useAppContext } from './app_context';
import { ComingSoonPrompt } from './components/coming_soon_prompt';
import { EsDeprecations } from './components/es_deprecations';
import { KibanaDeprecationsContent } from './components/kibana_deprecations';
import { Overview } from './components/overview';
import { RedirectAppLinks } from '../../../../../src/plugins/kibana_react/public';
import { AppDependencies } from '../types';

const { GlobalFlyoutProvider } = GlobalFlyout;
export interface AppDependencies extends ContextValue {
i18n: I18nStart;
history: ScopedHistory;
application: ApplicationStart;
services: AppServicesContext;
}

const App: React.FunctionComponent = () => {
const { isReadOnlyMode } = useAppContext();
Expand Down Expand Up @@ -54,23 +47,20 @@ export const AppWithRouter = ({ history }: { history: ScopedHistory }) => {
);
};

export const RootComponent = ({
i18n,
history,
services,
application,
...contextValue
}: AppDependencies) => {
export const RootComponent = (dependencies: AppDependencies) => {
const {
history,
core: { i18n, application },
} = dependencies.services;

return (
<RedirectAppLinks application={application} className={APP_WRAPPER_CLASS}>
<i18n.Context>
<KibanaContextProvider services={services}>
<AppContextProvider value={contextValue}>
<GlobalFlyoutProvider>
<AppWithRouter history={history} />
</GlobalFlyoutProvider>
</AppContextProvider>
</KibanaContextProvider>
<AppContextProvider value={dependencies}>
<GlobalFlyoutProvider>
<AppWithRouter history={history} />
</GlobalFlyoutProvider>
</AppContextProvider>
</i18n.Context>
</RedirectAppLinks>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,16 @@
*/

import React, { createContext, useContext } from 'react';
import {
CoreStart,
DeprecationsServiceStart,
DocLinksStart,
HttpSetup,
NotificationsStart,
} from 'src/core/public';
import { SharePluginSetup } from 'src/plugins/share/public';
import { ApiService } from './lib/api';
import { BreadcrumbService } from './lib/breadcrumbs';
import { AppDependencies } from '../types';

export interface KibanaVersionContext {
currentMajor: number;
prevMajor: number;
nextMajor: number;
}

export interface ContextValue {
http: HttpSetup;
docLinks: DocLinksStart;
kibanaVersionInfo: KibanaVersionContext;
notifications: NotificationsStart;
isReadOnlyMode: boolean;
api: ApiService;
breadcrumbs: BreadcrumbService;
getUrlForApp: CoreStart['application']['getUrlForApp'];
deprecations: DeprecationsServiceStart;
share: SharePluginSetup;
}

export const AppContext = createContext<ContextValue>({} as any);
export const AppContext = createContext<AppDependencies | undefined>(undefined);

export const AppContextProvider = ({
children,
value,
}: {
children: React.ReactNode;
value: ContextValue;
value: AppDependencies;
}) => {
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { FormattedMessage } from '@kbn/i18n/react';
import { useAppContext } from '../app_context';

export const ComingSoonPrompt: React.FunctionComponent = () => {
const { kibanaVersionInfo, docLinks } = useAppContext();
const {
kibanaVersionInfo,
services: {
core: { docLinks },
},
} = useAppContext();

const { nextMajor, currentMajor } = kibanaVersionInfo;
const { ELASTIC_WEBSITE_URL } = docLinks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export const IndexSettingsTableRow: React.FunctionComponent<Props> = ({
details?: ResponseError;
}>({ statusType: 'idle' });

const { api } = useAppContext();
const {
services: { api },
} = useAppContext();

const { addContent: addContentToGlobalFlyout, removeContent: removeContentFromGlobalFlyout } =
useGlobalFlyout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export const MlSnapshotsTableRowCells: React.FunctionComponent<TableRowProps> =
};

export const MlSnapshotsTableRow: React.FunctionComponent<TableRowProps> = (props) => {
const { api } = useAppContext();
const {
services: { api },
} = useAppContext();

return (
<MlSnapshotsStatusProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export const ReindexFlyout: React.FunctionComponent<ReindexFlyoutProps> = ({
}) => {
const { status, reindexWarnings } = reindexState;
const { index, correctiveAction } = deprecation;
const { docLinks } = useAppContext();
const {
services: {
core: { docLinks },
},
} = useAppContext();
// If there are any warnings and we haven't started reindexing, show the warnings step first.
const [currentFlyoutStep, setCurrentFlyoutStep] = useState<ReindexFlyoutStep>(
reindexWarnings && reindexWarnings.length > 0 && status === undefined
Expand Down
Loading

0 comments on commit 3726381

Please sign in to comment.