Skip to content

Commit

Permalink
Merge pull request #35886 from ruben-rebelo/ts-migration/react-native…
Browse files Browse the repository at this point in the history
…-permissions-mock

[TS migration] Migrate react-native-permissions mock
  • Loading branch information
jasperhuangg authored Feb 16, 2024
2 parents c050a90 + 296bd53 commit d184c70
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 84 deletions.
84 changes: 0 additions & 84 deletions __mocks__/react-native-permissions.js

This file was deleted.

77 changes: 77 additions & 0 deletions __mocks__/react-native-permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {PERMISSIONS, RESULTS} from 'react-native-permissions/dist/commonjs/permissions';
import type {ValueOf} from 'type-fest';

type Results = ValueOf<typeof RESULTS>;
type ResultsCollection = Record<string, Results>;
type NotificationSettings = Record<string, boolean>;
type Notification = {status: Results; settings: NotificationSettings};

const openLimitedPhotoLibraryPicker: jest.Mock<void> = jest.fn(() => {});
const openSettings: jest.Mock<void> = jest.fn(() => {});
const check = jest.fn(() => RESULTS.GRANTED as string);
const request = jest.fn(() => RESULTS.GRANTED as string);
const checkLocationAccuracy: jest.Mock<string> = jest.fn(() => 'full');
const requestLocationAccuracy: jest.Mock<string> = jest.fn(() => 'full');

const notificationOptions: string[] = ['alert', 'badge', 'sound', 'carPlay', 'criticalAlert', 'provisional'];

const notificationSettings: NotificationSettings = {
alert: true,
badge: true,
sound: true,
carPlay: true,
criticalAlert: true,
provisional: true,
lockScreen: true,
notificationCenter: true,
};

const checkNotifications: jest.Mock<Notification> = jest.fn(() => ({
status: RESULTS.GRANTED,
settings: notificationSettings,
}));

const requestNotifications: jest.Mock<Notification> = jest.fn((options: Record<string, string>) => ({
status: RESULTS.GRANTED,
settings: Object.keys(options)
.filter((option: string) => notificationOptions.includes(option))
.reduce((acc: NotificationSettings, option: string) => ({...acc, [option]: true}), {
lockScreen: true,
notificationCenter: true,
}),
}));

const checkMultiple: jest.Mock<ResultsCollection> = jest.fn((permissions: string[]) =>
permissions.reduce(
(acc: ResultsCollection, permission: string) => ({
...acc,
[permission]: RESULTS.GRANTED,
}),
{},
),
);

const requestMultiple: jest.Mock<ResultsCollection> = jest.fn((permissions: string[]) =>
permissions.reduce(
(acc: ResultsCollection, permission: string) => ({
...acc,
[permission]: RESULTS.GRANTED,
}),
{},
),
);

export {
PERMISSIONS,
RESULTS,
check,
checkLocationAccuracy,
checkMultiple,
checkNotifications,
openLimitedPhotoLibraryPicker,
openSettings,
request,
requestLocationAccuracy,
requestMultiple,
requestNotifications,
};
5 changes: 5 additions & 0 deletions src/types/modules/react-native-permissions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'react-native-permissions/dist/commonjs/permissions' {
import {PERMISSIONS, RESULTS} from 'react-native-permissions';

export {PERMISSIONS, RESULTS};
}

0 comments on commit d184c70

Please sign in to comment.