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-permissions mock #35886

84 changes: 0 additions & 84 deletions __mocks__/react-native-permissions.js

This file was deleted.

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

Choose a reason for hiding this comment

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

Would that work?

Suggested change
import {PERMISSIONS, RESULTS} from 'react-native-permissions/dist/commonjs/permissions';
import {PERMISSIONS, RESULTS} from 'react-native-permissions';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, importing these from react-native-permissions directly are breaking the unit tests, this is the reason why I've added this module declaration.
I've tried that on this commit: 1fdb8fd but had to revert it

import type {ValueOf} from 'type-fest';

type Results = ValueOf<typeof RESULTS>;
type ResultsCollection = Record<string, Results>;
type NotificationSettings = Record<string, boolean>;

const openLimitedPhotoLibraryPicker: jest.Mock<void> = jest.fn(() => {});
const openSettings: jest.Mock<void> = jest.fn(() => {});
const check: jest.Mock<Results> = jest.fn(() => RESULTS.GRANTED as string);
const request: jest.Mock<Results> = 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<{status: Results; settings: typeof notificationSettings}> = jest.fn(() => ({
Copy link
Contributor

Choose a reason for hiding this comment

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

Extract {status: Results; settings: typeof notificationSettings} to a separate type and use it here

status: RESULTS.GRANTED,
settings: notificationSettings,
}));

const requestNotifications: jest.Mock<{status: Results; settings: typeof notificationSettings}> = jest.fn((options: Record<string, string>) => ({
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

status: RESULTS.GRANTED,
settings: Object.keys(options)
.filter((option: string) => notificationOptions.includes(option))
.reduce((acc: ResultsCollection, option: string) => ({...acc, [option]: true}), {
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
.reduce((acc: ResultsCollection, option: string) => ({...acc, [option]: true}), {
.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,
};
1 change: 1 addition & 0 deletions src/types/modules/react-native-permissions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'react-native-permissions/dist/commonjs/permissions';
Copy link
Contributor

Choose a reason for hiding this comment

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

Because of this declaration {PERMISSIONS, RESULTS} are of type any, is this expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blazejkustra This was not expected. I have updated the module declaration.
Please have a look, thank you!

Loading