generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
252c10d
commit cc4f406
Showing
5 changed files
with
55 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 37 additions & 25 deletions
62
packages/design-system-react-native/src/hocs/withThemeProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,49 @@ | ||
/* eslint-disable jsdoc/require-returns */ | ||
// src/hocs/withThemeProvider.tsx | ||
|
||
import { | ||
ThemeProvider, | ||
ThemeContext, | ||
ColorSet, | ||
Theme, | ||
} from '@metamask/design-system-twrnc-preset'; | ||
import React, { forwardRef, useContext } from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
|
||
import { withThemeProvider } from './withThemeProvider'; | ||
|
||
/** | ||
* HOC to wrap components with ThemeProvider if none is present. | ||
* @param Component - The component to wrap with ThemeProvider. | ||
*/ | ||
export function withThemeProvider<Props extends object>( | ||
Component: React.ComponentType<Props>, | ||
) { | ||
const WrappedComponent = forwardRef<unknown, Props>((props, ref) => { | ||
// Check if a ThemeProvider is already present | ||
const themeContext = useContext(ThemeContext); | ||
const TestThemeComponent = React.forwardRef((props, ref) => { | ||
const themeContext = React.useContext(ThemeContext); | ||
return ( | ||
<Text ref={ref}> | ||
{themeContext.theme ? themeContext.theme : 'No Theme'} | ||
</Text> | ||
); | ||
}); | ||
const WrappedComponent = withThemeProvider(TestThemeComponent); | ||
|
||
// If ThemeProvider exists, use the component as is | ||
if (themeContext) { | ||
return <Component {...(props as Props)} ref={ref} />; | ||
} | ||
describe('withThemeProvider HOC', () => { | ||
it('provides default theme when no ThemeProvider is present', () => { | ||
const { getByText } = render(<WrappedComponent />); | ||
expect(getByText(Theme.Default)).toBeDefined(); | ||
}); | ||
|
||
// Otherwise, wrap with ThemeProvider | ||
return ( | ||
<ThemeProvider colorSet={ColorSet.Brand} theme={Theme.Default}> | ||
<Component {...(props as Props)} ref={ref} /> | ||
</ThemeProvider> | ||
it('does not override existing theme context', () => { | ||
const { getByText } = render( | ||
<ThemeProvider colorSet={ColorSet.Brand} theme={Theme.Dark}> | ||
<WrappedComponent /> | ||
</ThemeProvider>, | ||
); | ||
expect(getByText(Theme.Dark)).toBeDefined(); | ||
}); | ||
|
||
return WrappedComponent; | ||
} | ||
it('forwards ref to the wrapped component', () => { | ||
const ref = React.createRef(); | ||
render(<WrappedComponent ref={ref} />); | ||
expect(ref.current).toBeDefined(); | ||
}); | ||
|
||
it('passes additional props to the wrapped component', () => { | ||
const { getByText } = render(<WrappedComponent testProp="testValue" />); | ||
// Update TestThemeComponent to display testProp for this test | ||
expect(getByText('testValue')).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { useTailwind, useThemeContext } from './Theme.hooks'; | ||
export { useTailwind } from './Theme.hooks'; | ||
export { ThemeContext, ThemeProvider } from './Theme.providers'; | ||
export { ColorSet, Theme } from './Theme.types'; | ||
export type { ThemeContextProps, ThemeProviderProps } from './Theme.types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters