Skip to content

Commit

Permalink
Use KibanaThemeProvider for security/spaces apps (#119124)
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner authored Nov 23, 2021
1 parent 3c17852 commit e5b279f
Show file tree
Hide file tree
Showing 39 changed files with 532 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import React from 'react';

import { coreMock } from 'src/core/public/mocks';
import { coreMock, themeServiceMock } from 'src/core/public/mocks';

import { ClusterAddressForm } from './cluster_address_form';
import { Providers } from './plugin';
Expand All @@ -21,14 +21,16 @@ jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => ({
describe('ClusterAddressForm', () => {
jest.setTimeout(20_000);

const theme$ = themeServiceMock.createTheme$();

it('calls enrollment API when submitting form', async () => {
const coreStart = coreMock.createStart();
coreStart.http.post.mockResolvedValue({});

const onSuccess = jest.fn();

const { findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<ClusterAddressForm onSuccess={onSuccess} />
</Providers>
);
Expand All @@ -52,7 +54,7 @@ describe('ClusterAddressForm', () => {
const onSuccess = jest.fn();

const { findAllByText, findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<ClusterAddressForm onSuccess={onSuccess} />
</Providers>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import React from 'react';

import { coreMock } from 'src/core/public/mocks';
import { coreMock, themeServiceMock } from 'src/core/public/mocks';

import { ClusterConfigurationForm } from './cluster_configuration_form';
import { Providers } from './plugin';
Expand All @@ -21,14 +21,16 @@ jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => ({
describe('ClusterConfigurationForm', () => {
jest.setTimeout(20_000);

const theme$ = themeServiceMock.createTheme$();

it('calls enrollment API for https addresses when submitting form', async () => {
const coreStart = coreMock.createStart();
coreStart.http.post.mockResolvedValue({});

const onSuccess = jest.fn();

const { findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<ClusterConfigurationForm
host="https://localhost:9200"
authRequired
Expand Down Expand Up @@ -78,7 +80,7 @@ describe('ClusterConfigurationForm', () => {
const onSuccess = jest.fn();

const { findByRole } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<ClusterConfigurationForm
host="http://localhost:9200"
authRequired={false}
Expand Down Expand Up @@ -107,7 +109,7 @@ describe('ClusterConfigurationForm', () => {
const onSuccess = jest.fn();

const { findAllByText, findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<ClusterConfigurationForm
host="https://localhost:9200"
authRequired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import React from 'react';

import { coreMock } from 'src/core/public/mocks';
import { coreMock, themeServiceMock } from 'src/core/public/mocks';

import type { EnrollmentToken } from '../common';
import { decodeEnrollmentToken, EnrollmentTokenForm } from './enrollment_token_form';
Expand All @@ -29,14 +29,16 @@ const token: EnrollmentToken = {
describe('EnrollmentTokenForm', () => {
jest.setTimeout(20_000);

const theme$ = themeServiceMock.createTheme$();

it('calls enrollment API when submitting form', async () => {
const coreStart = coreMock.createStart();
coreStart.http.post.mockResolvedValue({});

const onSuccess = jest.fn();

const { findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<EnrollmentTokenForm onSuccess={onSuccess} />
</Providers>
);
Expand All @@ -62,7 +64,7 @@ describe('EnrollmentTokenForm', () => {
const onSuccess = jest.fn();

const { findAllByText, findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<EnrollmentTokenForm onSuccess={onSuccess} />
</Providers>
);
Expand Down
22 changes: 14 additions & 8 deletions src/plugins/interactive_setup/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import type { FunctionComponent } from 'react';
import React from 'react';
import ReactDOM from 'react-dom';
import type { Observable } from 'rxjs';

import { I18nProvider } from '@kbn/i18n/react';
import type { CoreSetup, CoreStart, Plugin } from 'src/core/public';
import type { CoreSetup, CoreStart, CoreTheme, Plugin } from 'src/core/public';

import { App } from './app';
import { KibanaThemeProvider } from './theme'; // TODO: replace this with the one exported from `kibana_react` after https://github.com/elastic/kibana/issues/119204 is implemented.
import { KibanaProvider } from './use_kibana';
import { VerificationProvider } from './use_verification';

Expand All @@ -24,7 +26,7 @@ export class InteractiveSetupPlugin implements Plugin<void, void, {}, {}> {
title: 'Configure Elastic to get started',
appRoute: '/',
chromeless: true,
mount: async (params) => {
mount: async ({ element, theme$ }) => {
const url = new URL(window.location.href);
const defaultCode = url.searchParams.get('code') || undefined;
const onSuccess = () => {
Expand All @@ -34,12 +36,12 @@ export class InteractiveSetupPlugin implements Plugin<void, void, {}, {}> {
const [services] = await core.getStartServices();

ReactDOM.render(
<Providers defaultCode={defaultCode} services={services}>
<Providers defaultCode={defaultCode} services={services} theme$={theme$}>
<App onSuccess={onSuccess} />
</Providers>,
params.element
element
);
return () => ReactDOM.unmountComponentAtNode(params.element);
return () => ReactDOM.unmountComponentAtNode(element);
},
});
}
Expand All @@ -49,17 +51,21 @@ export class InteractiveSetupPlugin implements Plugin<void, void, {}, {}> {

export interface ProvidersProps {
services: CoreStart;
theme$: Observable<CoreTheme>;
defaultCode?: string;
}

export const Providers: FunctionComponent<ProvidersProps> = ({
defaultCode,
services,
theme$,
children,
}) => (
<I18nProvider>
<KibanaProvider services={services}>
<VerificationProvider defaultCode={defaultCode}>{children}</VerificationProvider>
</KibanaProvider>
<KibanaThemeProvider theme$={theme$}>
<KibanaProvider services={services}>
<VerificationProvider defaultCode={defaultCode}>{children}</VerificationProvider>
</KibanaProvider>
</KibanaThemeProvider>
</I18nProvider>
);
9 changes: 9 additions & 0 deletions src/plugins/interactive_setup/public/theme/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { KibanaThemeProvider } from './kibana_theme_provider';
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { useEuiTheme } from '@elastic/eui';
import type { ReactWrapper } from 'enzyme';
import type { FC } from 'react';
import React, { useEffect } from 'react';
import { act } from 'react-dom/test-utils';
import { BehaviorSubject, of } from 'rxjs';

import { mountWithIntl } from '@kbn/test/jest';
import type { CoreTheme } from 'src/core/public';

import { KibanaThemeProvider } from './kibana_theme_provider';

describe('KibanaThemeProvider', () => {
let euiTheme: ReturnType<typeof useEuiTheme> | undefined;

beforeEach(() => {
euiTheme = undefined;
});

const flushPromises = async () => {
await new Promise<void>(async (resolve, reject) => {
try {
setImmediate(() => resolve());
} catch (error) {
reject(error);
}
});
};

const InnerComponent: FC = () => {
const theme = useEuiTheme();
useEffect(() => {
euiTheme = theme;
}, [theme]);
return <div>foo</div>;
};

const refresh = async (wrapper: ReactWrapper<unknown>) => {
await act(async () => {
await flushPromises();
wrapper.update();
});
};

it('exposes the EUI theme provider', async () => {
const coreTheme: CoreTheme = { darkMode: true };

const wrapper = mountWithIntl(
<KibanaThemeProvider theme$={of(coreTheme)}>
<InnerComponent />
</KibanaThemeProvider>
);

await refresh(wrapper);

expect(euiTheme!.colorMode).toEqual('DARK');
});

it('propagates changes of the coreTheme observable', async () => {
const coreTheme$ = new BehaviorSubject<CoreTheme>({ darkMode: true });

const wrapper = mountWithIntl(
<KibanaThemeProvider theme$={coreTheme$}>
<InnerComponent />
</KibanaThemeProvider>
);

await refresh(wrapper);

expect(euiTheme!.colorMode).toEqual('DARK');

await act(async () => {
coreTheme$.next({ darkMode: false });
});

await refresh(wrapper);

expect(euiTheme!.colorMode).toEqual('LIGHT');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { EuiThemeProvider } from '@elastic/eui';
import type { FC } from 'react';
import React, { useMemo } from 'react';
import useObservable from 'react-use/lib/useObservable';
import type { Observable } from 'rxjs';

import type { CoreTheme } from '../../../../core/public';
import { getColorMode } from './utils';

interface KibanaThemeProviderProps {
theme$: Observable<CoreTheme>;
}

const defaultTheme: CoreTheme = {
darkMode: false,
};

/**
* Copied from the `kibana_react` plugin, remove once https://github.com/elastic/kibana/issues/119204 is implemented.
*/
export const KibanaThemeProvider: FC<KibanaThemeProviderProps> = ({ theme$, children }) => {
const theme = useObservable(theme$, defaultTheme);
const colorMode = useMemo(() => getColorMode(theme), [theme]);
return <EuiThemeProvider colorMode={colorMode}>{children}</EuiThemeProvider>;
};
19 changes: 19 additions & 0 deletions src/plugins/interactive_setup/public/theme/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { getColorMode } from './utils';

describe('getColorMode', () => {
it('returns the correct `colorMode` when `darkMode` is enabled', () => {
expect(getColorMode({ darkMode: true })).toEqual('DARK');
});

it('returns the correct `colorMode` when `darkMode` is disabled', () => {
expect(getColorMode({ darkMode: false })).toEqual('LIGHT');
});
});
19 changes: 19 additions & 0 deletions src/plugins/interactive_setup/public/theme/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { EuiThemeColorMode } from '@elastic/eui/src/services/theme/types';

import type { CoreTheme } from '../../../../core/public';

/**
* Copied from the `kibana_react` plugin, remove once https://github.com/elastic/kibana/issues/119204 is implemented.
*/
export const getColorMode = (theme: CoreTheme): EuiThemeColorMode => {
// COLOR_MODES_STANDARD is not exported from eui
return theme.darkMode ? 'DARK' : 'LIGHT';
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import React from 'react';

import { coreMock } from 'src/core/public/mocks';
import { coreMock, themeServiceMock } from 'src/core/public/mocks';

import { Providers } from './plugin';
import { VerificationCodeForm } from './verification_code_form';
Expand All @@ -21,14 +21,16 @@ jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => ({
describe('VerificationCodeForm', () => {
jest.setTimeout(20_000);

const theme$ = themeServiceMock.createTheme$();

it('calls enrollment API when submitting form', async () => {
const coreStart = coreMock.createStart();
coreStart.http.post.mockResolvedValue({});

const onSuccess = jest.fn();

const { findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<VerificationCodeForm onSuccess={onSuccess} />
</Providers>
);
Expand Down Expand Up @@ -65,7 +67,7 @@ describe('VerificationCodeForm', () => {
const onSuccess = jest.fn();

const { findAllByText, findByRole, findByLabelText } = render(
<Providers services={coreStart}>
<Providers services={coreStart} theme$={theme$}>
<VerificationCodeForm onSuccess={onSuccess} />
</Providers>
);
Expand Down
Loading

0 comments on commit e5b279f

Please sign in to comment.