I have the following component which displays a text on the app if the app is offline.
import React from 'react';
import { useNetInfo } from '@react-native-community/netinfo';
import { Label } from 'components/ui';
const OfflineNotice = () => {
const netInfo = useNetInfo();
if (netInfo.type !== 'unknown' && netInfo.isInternetReachable === false) {
return <Label size={18} text='No Internet Connection' />;
}
return null;
};
export default OfflineNotice;
I want to write a unit test to this to check if this works properly. How can I do this? Im new to unit tests. I don't understand how to mock this.
I use typescript and testing-library/react-native.
UPATED: Why does this first test fail? It should NOT TO BE NULL. But it fails. The error is,
OfflineNotice component › test
expect(received).not.toBeNull()
Received: null
15 |
16 | const { queryByText } = render(<OfflineNotice />);
> 17 | expect(queryByText(/no internet connection/i)).not.toBeNull();
| ^
18 | });
19 |
at Object.<anonymous> (src/components/offline-notice/offline-notice.test.tsx:17:56)