Skip to content

Commit

Permalink
Removing type error to avoid breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kc13greiner committed Jul 11, 2023
1 parent bdbe9a8 commit cc45a2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/plugins/kibana_react/public/theme/use_theme.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { CoreTheme } from '@kbn/core/public';
import { KibanaContextProvider } from '../context';
import { themeServiceMock } from '@kbn/core/public/mocks';
import { useKibanaTheme } from './use_theme';
import { of } from 'rxjs';

describe('useKibanaTheme', () => {
let resultTheme: CoreTheme | undefined;
Expand All @@ -29,9 +30,10 @@ describe('useKibanaTheme', () => {
};

it('retrieve CoreTheme when theme service is provided in context', async () => {
const expectedCoreTheme: CoreTheme = { darkMode: false };
const expectedCoreTheme: CoreTheme = { darkMode: true };

const themeServiceStart = themeServiceMock.createStartContract();
themeServiceStart.theme$ = of({ darkMode: true });

mountWithIntl(
<KibanaContextProvider services={{ theme: themeServiceStart }}>
Expand All @@ -42,15 +44,15 @@ describe('useKibanaTheme', () => {
expect(resultTheme).toEqual(expectedCoreTheme);
});

it('throws type error when theme is not provided', async () => {
const mount = () => {
return mountWithIntl(
<KibanaContextProvider>
<InnerComponent />
</KibanaContextProvider>
);
};
it('does not throw error when theme service is not provided, default theme applied', async () => {
const expectedCoreTheme: CoreTheme = { darkMode: false };

mountWithIntl(
<KibanaContextProvider>
<InnerComponent />
</KibanaContextProvider>
);

expect(mount).toThrow(new TypeError('theme service not available in kibana-react context.'));
expect(resultTheme).toEqual(expectedCoreTheme);
});
});
9 changes: 7 additions & 2 deletions src/plugins/kibana_react/public/theme/use_theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { CoreTheme } from '@kbn/core-theme-browser';
import useObservable from 'react-use/lib/useObservable';
import { of } from 'rxjs';
import { useKibana } from '../context/context';

export const useKibanaTheme = (): CoreTheme => {
Expand All @@ -17,9 +18,13 @@ export const useKibanaTheme = (): CoreTheme => {
services: { theme },
} = useKibana();

let themeObservable;

if (!theme) {
throw new TypeError('theme service not available in kibana-react context.');
themeObservable = of(defaultTheme);
} else {
themeObservable = theme.theme$;
}

return useObservable(theme.theme$, defaultTheme);
return useObservable(themeObservable, defaultTheme);
};

0 comments on commit cc45a2b

Please sign in to comment.