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

Bug: Fixing null/undefined theme argument for the WordPressBlocksProvider #2013

Open
wants to merge 12 commits into
base: canary
Choose a base branch
from
Open
11 changes: 1 addition & 10 deletions packages/blocks/src/components/WordPressBlocksProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,5 @@ export function WordPressBlocksProvider(props: {
* ```
*/
export function useBlocksTheme(): BlocksTheme {
const themeContext = React.useContext(WordPressThemeContext);

// If it's an empty object, the provider hasn't been initialized.
if (themeContext === undefined) {
throw new Error(
'useBlocksTheme hook was called outside of context, make sure your app is wrapped with WordPressBlocksProvider',
);
}

return themeContext;
return React.useContext(WordPressThemeContext) as BlocksTheme;
moonmeister marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colinmurphy I'm guessing we need to leave these tests in now that we've changed the fix, yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moonmeister We probably don't. I was leaving this for reference if someone wanted to use the theme argument but I removed the failing test for undefined.

Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import type { BlocksTheme } from '../../src/types/theme';
import { renderHook } from '@testing-library/react'; // Import from @testing-library/react

describe('useBlocksTheme', () => {
it('Throws an error if not used within WordPressBlocksProvider', () => {
// Assert that renderHook throws an error when used outside of WordPressBlocksProvider
expect(() => {
renderHook(() => useBlocksTheme());
}).toThrow('useBlocksTheme hook was called outside of context, make sure your app is wrapped with WordPressBlocksProvider');
});

it('returns the passed in theme from WordPressBlocksProvider', () => {
// Wrapping component to provide context
const wrapper = ({ children }: PropsWithChildren<{}>) => {
Expand Down Expand Up @@ -46,4 +39,4 @@ describe('useBlocksTheme', () => {
// Check the correct theme is returned
expect(theme?.colors?.palette).toStrictEqual({ primary: 'black' });
});
});
});
Loading