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

chore: typescript hooks and base folder coverage #10253

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('InstallSnapPermissionsRequest', () => {
approvalRequest={installSnapDataApprovalRequest}
onConfirm={onConfirm}
onCancel={onCancel}
snapId="mockId"
/>,
);
const permissionCells = getAllByTestId(SNAP_PERMISSION_CELL);
Expand Down
4 changes: 3 additions & 1 deletion app/components/Base/Alert.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ storiesOf('Components / Base / Alert', module)
small={boolean('small', false)}
renderIcon={
renderIconKnob
? () => <EvilIcons name="bell" style={styles.alertIcon} size={20} />
? //@ts-expect-error needs to be replaced by Icon of component library, EvilIcons are deprecated
// All this component is deprecated so it should be replaced and removed
() => <EvilIcons name="bell" style={styles.alertIcon} size={20} />
: () => null
}
onPress={action('onPress')}
Expand Down
6 changes: 5 additions & 1 deletion app/components/Base/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ const Alert = ({
onPress={onDismiss}
hitSlop={{ top: 20, left: 20, right: 20, bottom: 20 }}
>
<IonicIcon name="ios-close" style={styles.closeIcon} size={30} />
{
//@ts-expect-error needs to be replaced by Icon of component library, IonicIcon are deprecated
// All this component is deprecated so it should be replaced and removed
<IonicIcon name="ios-close" style={styles.closeIcon} size={30} />
}
</TouchableOpacity>
</View>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const useAddressBalance = (

useEffect(() => {
const setBalance = () => {
if (!address) return;
const parsedTicker = getTicker(ticker);
const checksumAddress = safeToChecksumAddress(address);
if (!checksumAddress) {
Expand Down
60 changes: 30 additions & 30 deletions app/components/hooks/useBluetoothPermissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ describe('useBluetoothPermissions', () => {
});

it('grants permissions on iOS', async () => {
Device.isIos.mockReturnValue(true);
request.mockResolvedValue(RESULTS.GRANTED);
(Device.isIos as jest.Mock).mockReturnValue(true);
(request as jest.Mock).mockResolvedValue(RESULTS.GRANTED);

const { result, waitForNextUpdate } = renderHook(() =>
useBluetoothPermissions(),
Expand All @@ -64,8 +64,8 @@ describe('useBluetoothPermissions', () => {
});

it('denies permissions on iOS', async () => {
Device.isIos.mockReturnValue(true);
request.mockResolvedValue(RESULTS.DENIED);
(Device.isIos as jest.Mock).mockReturnValue(true);
(request as jest.Mock).mockResolvedValue(RESULTS.DENIED);

const { result, waitForNextUpdate } = renderHook(() =>
useBluetoothPermissions(),
Expand All @@ -79,9 +79,9 @@ describe('useBluetoothPermissions', () => {
});

it('grants permissions on Android 12+', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('12');
requestMultiple.mockResolvedValue({
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('12');
(requestMultiple as jest.Mock).mockResolvedValue({
[PERMISSIONS.ANDROID.BLUETOOTH_CONNECT]: RESULTS.GRANTED,
[PERMISSIONS.ANDROID.BLUETOOTH_SCAN]: RESULTS.GRANTED,
});
Expand All @@ -96,9 +96,9 @@ describe('useBluetoothPermissions', () => {
});

it('denies permissions on Android 12+', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('12');
requestMultiple.mockResolvedValue({
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('12');
(requestMultiple as jest.Mock).mockResolvedValue({
[PERMISSIONS.ANDROID.BLUETOOTH_CONNECT]: RESULTS.DENIED,
[PERMISSIONS.ANDROID.BLUETOOTH_SCAN]: RESULTS.DENIED,
});
Expand All @@ -115,9 +115,9 @@ describe('useBluetoothPermissions', () => {
});

it('grants permissions on Android <12', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('11');
request.mockResolvedValue(RESULTS.GRANTED);
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('11');
(request as jest.Mock).mockResolvedValue(RESULTS.GRANTED);

const { result, waitForNextUpdate } = renderHook(() =>
useBluetoothPermissions(),
Expand All @@ -129,9 +129,9 @@ describe('useBluetoothPermissions', () => {
});

it('denies permissions on Android <12', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('11');
request.mockResolvedValue(RESULTS.DENIED);
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('11');
(request as jest.Mock).mockResolvedValue(RESULTS.DENIED);

const { result, waitForNextUpdate } = renderHook(() =>
useBluetoothPermissions(),
Expand All @@ -145,9 +145,9 @@ describe('useBluetoothPermissions', () => {
});

it('checks permissions when app state changes to active', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('12');
requestMultiple.mockResolvedValue({
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('12');
(requestMultiple as jest.Mock).mockResolvedValue({
[PERMISSIONS.ANDROID.BLUETOOTH_CONNECT]: RESULTS.GRANTED,
[PERMISSIONS.ANDROID.BLUETOOTH_SCAN]: RESULTS.GRANTED,
});
Expand All @@ -158,17 +158,17 @@ describe('useBluetoothPermissions', () => {
expect(requestMultiple).toHaveBeenCalledTimes(1);

act(() => {
AppState.addEventListener.mock.calls[0][1]('active');
(AppState.addEventListener as jest.Mock).mock.calls[0][1]('active');
});

//checkPermission run again when app state changes to active
expect(requestMultiple).toHaveBeenCalledTimes(2);
});

it('does not check permissions when app state changes to background', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('12');
requestMultiple.mockResolvedValue({
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('12');
(requestMultiple as jest.Mock).mockResolvedValue({
[PERMISSIONS.ANDROID.BLUETOOTH_CONNECT]: RESULTS.GRANTED,
[PERMISSIONS.ANDROID.BLUETOOTH_SCAN]: RESULTS.GRANTED,
});
Expand All @@ -179,17 +179,17 @@ describe('useBluetoothPermissions', () => {
expect(requestMultiple).toHaveBeenCalledTimes(1);

act(() => {
AppState.addEventListener.mock.calls[0][1]('inactive');
(AppState.addEventListener as jest.Mock).mock.calls[0][1]('inactive');
});

//checkPermission does not run when app state changes to inactive
expect(requestMultiple).toHaveBeenCalledTimes(1);
});

it('grants permissions when getSystemVersion is null', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue(null);
request.mockResolvedValue(RESULTS.GRANTED);
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue(null);
(request as jest.Mock).mockResolvedValue(RESULTS.GRANTED);

const { result, waitForNextUpdate } = renderHook(() =>
useBluetoothPermissions(),
Expand All @@ -201,9 +201,9 @@ describe('useBluetoothPermissions', () => {
});

it('grants permissions when getSystemVersion return is not a number', async () => {
Device.isAndroid.mockReturnValue(true);
getSystemVersion.mockReturnValue('adbd');
request.mockResolvedValue(RESULTS.GRANTED);
(Device.isAndroid as jest.Mock).mockReturnValue(true);
(getSystemVersion as jest.Mock).mockReturnValue('adbd');
(request as jest.Mock).mockResolvedValue(RESULTS.GRANTED);

const { result, waitForNextUpdate } = renderHook(() =>
useBluetoothPermissions(),
Expand Down
16 changes: 6 additions & 10 deletions app/components/hooks/useThunkDispatch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { ThunkAction as ReduxThunkAction, ThunkDispatch } from 'redux-thunk';
import { useDispatch } from 'react-redux';
import { AnyAction, Store } from 'redux';
import { AnyAction } from 'redux';
import { RootState } from '../../reducers';

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Dispatch = Store<any, AnyAction>['dispatch'];
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type GetState = Store<any, AnyAction>['getState'];

export type ThunkAction = (dispatch: Dispatch, getState: GetState) => void;
export type ThunkAction = ReduxThunkAction<void, RootState, unknown, AnyAction>;

function useThunkDispatch() {
return useDispatch<(thunkAction: ThunkAction) => void>();
return useDispatch<ThunkDispatch<RootState, unknown, AnyAction>>();
}

export default useThunkDispatch;
2 changes: 1 addition & 1 deletion app/components/hooks/useTokenBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect, Dispatch, SetStateAction } from 'react';
import Engine from '../../core/Engine';
import { BN } from '@metamask/assets-controllers';
import { BN } from 'ethereumjs-util';

/**
* Hook to handle the balance of ERC20 tokens
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useSelector } from 'react-redux';
import { isEqual } from 'lodash';
import { ControllerHookType } from '../controllerHook.types';
import { BN } from 'ethereumjs-util';
import { selectContractBalances } from '../../../selectors/tokenBalancesController';

interface TokenBalances {
[address: string]: BN;
[address: string]: string;
Cal-L marked this conversation as resolved.
Show resolved Hide resolved
}

const useTokenBalancesController = (): ControllerHookType<TokenBalances> => {
Expand Down
5 changes: 5 additions & 0 deletions app/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,8 @@ declare module 'react-native-vector-icons/Zocial' {
*/
export default class Zocial extends ZocialIconType {}
}

declare module '@metamask/contract-metadata' {
const content: Record<string, TokenListToken>;
export default content;
}
Loading