import { fireEvent, render, screen } from '@testing-library/react-native';
import { Button, Text, View } from 'react-native';
import { createMMKV, MMKV } from 'react-native-mmkv';
import { ThemeProvider, useTheme } from '@/theme';
function TestChildComponent() {
const { changeTheme, variant } = useTheme();
return (
{variant}
);
}
describe('ThemeProvider', () => {
let storage: MMKV;
beforeEach(() => {
storage = createMMKV();
});
it('initializes with the default theme when no theme is defined in storage', () => {
render(
,
);
// Assert that the theme context is initialized with 'default'
expect(screen.getByText('default')).toBeTruthy();
});
it('loads the theme from storage if defined', () => {
storage.set('theme', 'dark');
render(
,
);
// Assert that the theme context is initialized with 'dark'
expect(screen.getByText('dark')).toBeTruthy();
});
it('changes the theme when calling changeTheme', () => {
render(
,
);
// Assert that the theme context is initialized with 'default'
expect(screen.getByText('default')).toBeTruthy();
fireEvent.press(screen.getByTestId('change-btn'));
// Assert that the theme has changed to 'light'
expect(screen.getByText('dark')).toBeTruthy();
});
});