Skip to content

Commit

Permalink
Merge pull request #35884 from kubabutkiewicz/ts-migration/react-nati…
Browse files Browse the repository at this point in the history
…ve-safe-area-context-mock

[No QA][TS migration] Migrate 'react-native-safe-area-context.js' mock to TypeScript
  • Loading branch information
arosiclair committed Mar 4, 2024
2 parents 3aeeda5 + 59bc087 commit f50ac2a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 52 deletions.
52 changes: 0 additions & 52 deletions __mocks__/react-native-safe-area-context.js

This file was deleted.

61 changes: 61 additions & 0 deletions __mocks__/react-native-safe-area-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type {ForwardedRef, ReactNode} from 'react';
import React, {forwardRef} from 'react';
import {View} from 'react-native';
import type {EdgeInsets, useSafeAreaFrame as LibUseSafeAreaFrame, WithSafeAreaInsetsProps} from 'react-native-safe-area-context';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

type SafeAreaProviderProps = ChildrenProps;
type SafeAreaConsumerProps = {
children?: (insets: EdgeInsets) => ReactNode;
};
type SafeAreaInsetsContextValue = {
Consumer: (props: SafeAreaConsumerProps) => ReactNode;
};

const insets: EdgeInsets = {
top: 0,
right: 0,
bottom: 0,
left: 0,
};

function withSafeAreaInsets(WrappedComponent: React.ComponentType<WithSafeAreaInsetsProps & {ref: ForwardedRef<unknown>}>) {
function WithSafeAreaInsets(props: WithSafeAreaInsetsProps & {forwardedRef: React.ForwardedRef<unknown>}) {
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
// eslint-disable-next-line react/prop-types
ref={props.forwardedRef}
insets={insets}
/>
);
}

const WithSafeAreaInsetsWithRef = forwardRef((props: WithSafeAreaInsetsProps, ref: ForwardedRef<unknown>) => (
<WithSafeAreaInsets
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));

return WithSafeAreaInsetsWithRef;
}

const SafeAreaView = View;
const SafeAreaProvider = (props: SafeAreaProviderProps) => props.children;
const SafeAreaConsumer = (props: SafeAreaConsumerProps) => props.children?.(insets);
const SafeAreaInsetsContext: SafeAreaInsetsContextValue = {
Consumer: SafeAreaConsumer,
};

const useSafeAreaFrame: jest.Mock<ReturnType<typeof LibUseSafeAreaFrame>> = jest.fn(() => ({
x: 0,
y: 0,
width: 390,
height: 844,
}));
const useSafeAreaInsets: jest.Mock<EdgeInsets> = jest.fn(() => insets);

export {SafeAreaProvider, SafeAreaConsumer, SafeAreaInsetsContext, withSafeAreaInsets, SafeAreaView, useSafeAreaFrame, useSafeAreaInsets};

0 comments on commit f50ac2a

Please sign in to comment.