Problem when testing a component with react-native-safe-area-context
Solution 1:
Steps to mock react-native-safe-area-context
correctly
- Create a file
mockSafeAreaContext.tsx
import React from 'react';
const MOCK_INITIAL_METRICS = {
frame: {
width: 320,
height: 640,
x: 0,
y: 0,
},
insets: {
left: 0,
right: 0,
bottom: 0,
top: 0,
},
};
const RNSafeAreaContext = jest.requireActual('react-native-safe-area-context');
export default {
...RNSafeAreaContext,
initialWindowMetrics: MOCK_INITIAL_METRICS,
SafeAreaProvider: ({ children, initialMetrics }) => {
return (
<RNSafeAreaContext.SafeAreaFrameContext.Provider
value={initialMetrics?.frame ?? MOCK_INITIAL_METRICS.frame}>
<RNSafeAreaContext.SafeAreaInsetsContext.Provider
value={initialMetrics?.insets ?? MOCK_INITIAL_METRICS.insets}>
{children}
</RNSafeAreaContext.SafeAreaInsetsContext.Provider>
</RNSafeAreaContext.SafeAreaFrameContext.Provider>
);
},
useSafeAreaInsets: jest.fn, // add more hooks if needed
};
- Now use jest to mock in your mock file
import mockSafeAreaContext from 'created-file-path'
jest.mock('react-native-safe-area-context', () => mockSafeAreaContext);
react-native-safe-area-context