Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useTheme returns undefined as value #7960

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/ra-ui-materialui/src/layout/Theme/useTheme.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as React from 'react';
import expect from 'expect';
import { render, screen } from '@testing-library/react';
import { useTheme } from './useTheme';
import { CoreAdminContext } from 'ra-core';

const authProvider = {
login: jest.fn().mockResolvedValueOnce(''),
logout: jest.fn().mockResolvedValueOnce(''),
checkAuth: jest.fn().mockResolvedValueOnce(''),
checkError: jest.fn().mockResolvedValueOnce(''),
getPermissions: jest.fn().mockResolvedValueOnce(''),
};

const Foo = () => {
const [theme] = useTheme();
return theme !== undefined ? <div aria-label="has-theme" /> : <></>;
};

describe('useTheme', () => {
it('should return current theme', () => {
render(
<CoreAdminContext authProvider={authProvider}>
<Foo />
</CoreAdminContext>
);
expect(screen.getByLabelText('has-theme')).not.toBeNull();
});
});
8 changes: 6 additions & 2 deletions packages/ra-ui-materialui/src/layout/Theme/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useStore } from 'ra-core';
import { ThemeOptions } from '@mui/material';
import { ThemeOptions, useTheme as useThemeMUI } from '@mui/material';

export type ThemeSetter = (theme: ThemeOptions) => void;

export const useTheme = (
themeOverride?: ThemeOptions
): [ThemeOptions, ThemeSetter] => useStore('theme', themeOverride);
): [ThemeOptions, ThemeSetter] => {
const themeMUI = useThemeMUI();
const [theme, setter] = useStore('theme', themeOverride);
return [theme || themeMUI, setter];
};