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

[No QA][TS migration] Migrate 'react-native-safe-area-context.js' mock to TypeScript #35884

Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 0 additions & 52 deletions __mocks__/react-native-safe-area-context.js

This file was deleted.

66 changes: 66 additions & 0 deletions __mocks__/react-native-safe-area-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type {ForwardedRef, ReactNode} from 'react';
import React, {forwardRef} from 'react';
import {View} from 'react-native';
import type {EdgeInsets, 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: typeof View = View;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const SafeAreaView: typeof View = View;
const SafeAreaView = View;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kubabutkiewicz are you going to commit this suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ this is my only concern @kubabutkiewicz

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its commited, I missed that comment

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

const useSafeAreaFrame: jest.Mock<{
x: number;
y: number;
width: number;
height: number;
}> = 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};
Loading