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: Add dark mode to storybook #10286

Merged
merged 14 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ scripts
.detoxrc.js
.prettierignore
locales
.storybook
.storybook/storybook.requires.js
/ppom
app/lib/ppom/ppom.html.js
/app/lib/ppom/blockaid-version.js
/app/lib/ppom/blockaid-version.js
2 changes: 2 additions & 0 deletions .storybook/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { default as withMockStore } from './withMockStore';
export { default as withNavigation } from './withNavigation';
export { default as withSafeArea } from './withSafeArea';
export { default as withTheme } from './withTheme';
16 changes: 16 additions & 0 deletions .storybook/decorators/withMockStore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Third party dependencies
import React from 'react';
import configureMockStore from 'redux-mock-store';
import { Provider } from 'react-redux';

// External dependencies
import { storybookStore } from '../storybook-store';

const mockStore = configureMockStore();
const store = mockStore(storybookStore);

const withMockStore = (story: any) => {
return <Provider store={store}>{story()}</Provider>;
};

export default withMockStore;
8 changes: 8 additions & 0 deletions .storybook/decorators/withTheme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import ThemeProvider from '../../app/component-library/providers/ThemeProvider/ThemeProvider';

const withTheme = (storyFn: () => React.ReactNode) => (
<ThemeProvider>{storyFn()}</ThemeProvider>
);

export default withTheme;
16 changes: 8 additions & 8 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ThemeContext, mockTheme } from '../app/util/theme';
import { withNavigation, withSafeArea } from './decorators';
import {
withMockStore,
withTheme,
withNavigation,
withSafeArea,
} from './decorators';

export const decorators = [
// Using a decorator to apply padding for every story
(StoryFn) => (
<ThemeContext.Provider value={mockTheme}>
{<StoryFn />}
</ThemeContext.Provider>
),
withTheme,
withSafeArea,
withNavigation,
withMockStore,
];

export const parameters = {
Expand Down
5 changes: 5 additions & 0 deletions .storybook/storybook-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AppThemeKey } from '../app/util/theme/models';

export const storybookStore = {
user: { appTheme: AppThemeKey.os },
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable import/prefer-default-export */
// Test IDs
export const THEMEPROVIDER_TESTID = 'theme-provider';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Third party dependencies
import React from 'react';
import { render } from '@testing-library/react-native';
import { View } from 'react-native';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';

// External dependencies
import { mockTheme } from '../../../util/theme';

// Internal dependencies
import ThemeProvider from './ThemeProvider';
import { THEMEPROVIDER_TESTID } from './ThemeProvider.constants';

// Create a mock store
const mockStore = configureStore([]);
const store = mockStore({
user: {
appTheme: mockTheme.themeAppearance, // or any other relevant initial state
},
});

describe('ThemeProvider', () => {
it('should provide the correct theme to its children', () => {
const { getByTestId } = render(
<Provider store={store}>
<ThemeProvider>
<View />
</ThemeProvider>
</Provider>,
);

expect(getByTestId(THEMEPROVIDER_TESTID).props.style.backgroundColor).toBe(
mockTheme.colors.background.alternative,
);
});
});
30 changes: 30 additions & 0 deletions app/component-library/providers/ThemeProvider/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Third party dependencies
import React from 'react';
import { Dimensions, View } from 'react-native';

// External dependencies
import { useAppTheme, ThemeContext } from '../../../util/theme';

// Internal dependencies
import { THEMEPROVIDER_TESTID } from './ThemeProvider.constants';

const ThemeProvider = ({ children }: { children: React.ReactNode }) => {
const { width: windowWidth, height: windowHeight } = Dimensions.get('window');
const theme = useAppTheme();
return (
<ThemeContext.Provider value={theme}>
<View
style={{
width: windowWidth,
height: windowHeight,
backgroundColor: theme.colors.background.alternative,
}}
testID={THEMEPROVIDER_TESTID}
>
{children}
</View>
</ThemeContext.Provider>
);
};

export default ThemeProvider;
Loading