From 7114321da6dd80f78cad2ad886e55bf33b1f41da Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Tue, 6 Feb 2024 09:56:29 +0000 Subject: [PATCH 1/7] [TS migration] Migrate react-native-permissions mock --- __mocks__/react-native-permissions.js | 84 ------------------- __mocks__/react-native-permissions.ts | 76 +++++++++++++++++ .../modules/react-native-permissions.d.ts | 1 + 3 files changed, 77 insertions(+), 84 deletions(-) delete mode 100644 __mocks__/react-native-permissions.js create mode 100644 __mocks__/react-native-permissions.ts create mode 100644 src/types/modules/react-native-permissions.d.ts diff --git a/__mocks__/react-native-permissions.js b/__mocks__/react-native-permissions.js deleted file mode 100644 index e4cc02123e7f..000000000000 --- a/__mocks__/react-native-permissions.js +++ /dev/null @@ -1,84 +0,0 @@ -const {PERMISSIONS} = require('react-native-permissions/dist/commonjs/permissions'); -const {RESULTS} = require('react-native-permissions/dist/commonjs/results'); -const _ = require('underscore'); - -export {PERMISSIONS, RESULTS}; - -const openLimitedPhotoLibraryPicker = jest.fn(() => {}); -const openSettings = jest.fn(() => {}); -const check = jest.fn(() => RESULTS.GRANTED); -const request = jest.fn(() => RESULTS.GRANTED); -const checkLocationAccuracy = jest.fn(() => 'full'); -const requestLocationAccuracy = jest.fn(() => 'full'); - -const notificationOptions = ['alert', 'badge', 'sound', 'carPlay', 'criticalAlert', 'provisional']; - -const notificationSettings = { - alert: true, - badge: true, - sound: true, - carPlay: true, - criticalAlert: true, - provisional: true, - lockScreen: true, - notificationCenter: true, -}; - -const checkNotifications = jest.fn(() => ({ - status: RESULTS.GRANTED, - settings: notificationSettings, -})); - -const requestNotifications = jest.fn((options) => ({ - status: RESULTS.GRANTED, - settings: _.chain(options) - .filter((option) => _.contains(notificationOptions, option)) - .reduce((acc, option) => ({...acc, [option]: true}), { - lockScreen: true, - notificationCenter: true, - }) - .value(), -})); - -const checkMultiple = jest.fn((permissions) => - _.reduce(permissions, (acc, permission) => ({ - ...acc, - [permission]: RESULTS.GRANTED, - })), -); - -const requestMultiple = jest.fn((permissions) => - _.reduce(permissions, (acc, permission) => ({ - ...acc, - [permission]: RESULTS.GRANTED, - })), -); - -export default { - PERMISSIONS, - RESULTS, - - check, - checkLocationAccuracy, - checkMultiple, - checkNotifications, - openLimitedPhotoLibraryPicker, - openSettings, - request, - requestLocationAccuracy, - requestMultiple, - requestNotifications, -}; - -export { - openLimitedPhotoLibraryPicker, - openSettings, - check, - request, - checkLocationAccuracy, - requestLocationAccuracy, - checkNotifications, - requestNotifications, - checkMultiple, - requestMultiple, -}; diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts new file mode 100644 index 000000000000..f8bfe3cd6376 --- /dev/null +++ b/__mocks__/react-native-permissions.ts @@ -0,0 +1,76 @@ +import {PERMISSIONS, RESULTS} from 'react-native-permissions/dist/commonjs/permissions'; +import type {ValueOf} from 'type-fest'; + +type Results = ValueOf; +type ResultsCollection = Record; +type NotificationSettings = Record; + +const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); +const openSettings: jest.Mock = jest.fn(() => {}); +const check: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); +const request: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); +const checkLocationAccuracy: jest.Mock = jest.fn(() => 'full'); +const requestLocationAccuracy: jest.Mock = 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(() => ({ + status: RESULTS.GRANTED, + settings: notificationSettings, +})); + +const requestNotifications: jest.Mock<{status: Results; settings: typeof notificationSettings}> = jest.fn((options: Record) => ({ + status: RESULTS.GRANTED, + settings: Object.keys(options) + .filter((option: string) => notificationOptions.includes(option)) + .reduce((acc: ResultsCollection, option: string) => ({...acc, [option]: true}), { + lockScreen: true, + notificationCenter: true, + }), +})); + +const checkMultiple: jest.Mock = jest.fn((permissions: string[]) => + permissions.reduce( + (acc: ResultsCollection, permission: string) => ({ + ...acc, + [permission]: RESULTS.GRANTED, + }), + {}, + ), +); + +const requestMultiple: jest.Mock = 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, +}; diff --git a/src/types/modules/react-native-permissions.d.ts b/src/types/modules/react-native-permissions.d.ts new file mode 100644 index 000000000000..fa9a73cc8f25 --- /dev/null +++ b/src/types/modules/react-native-permissions.d.ts @@ -0,0 +1 @@ +declare module 'react-native-permissions/dist/commonjs/permissions'; From 989a2a7b43b4d047903c8615619a26e2190fe45f Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Fri, 9 Feb 2024 11:47:27 +0000 Subject: [PATCH 2/7] [TS migration] Code improvements --- __mocks__/react-native-permissions.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts index f8bfe3cd6376..77b7b8e6e248 100644 --- a/__mocks__/react-native-permissions.ts +++ b/__mocks__/react-native-permissions.ts @@ -4,6 +4,7 @@ import type {ValueOf} from 'type-fest'; type Results = ValueOf; type ResultsCollection = Record; type NotificationSettings = Record; +type Notification = {status: Results; settings: typeof notificationSettings} const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); const openSettings: jest.Mock = jest.fn(() => {}); @@ -25,12 +26,12 @@ const notificationSettings: NotificationSettings = { notificationCenter: true, }; -const checkNotifications: jest.Mock<{status: Results; settings: typeof notificationSettings}> = jest.fn(() => ({ +const checkNotifications: jest.Mock = jest.fn(() => ({ status: RESULTS.GRANTED, settings: notificationSettings, })); -const requestNotifications: jest.Mock<{status: Results; settings: typeof notificationSettings}> = jest.fn((options: Record) => ({ +const requestNotifications: jest.Mock = jest.fn((options: Record) => ({ status: RESULTS.GRANTED, settings: Object.keys(options) .filter((option: string) => notificationOptions.includes(option)) From 32d775d9150036ec2846b319ed9201d4c53626f3 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Fri, 9 Feb 2024 11:52:14 +0000 Subject: [PATCH 3/7] [TS migration] Lint fixed --- __mocks__/react-native-permissions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts index 77b7b8e6e248..e6f1aabff4c8 100644 --- a/__mocks__/react-native-permissions.ts +++ b/__mocks__/react-native-permissions.ts @@ -4,7 +4,7 @@ import type {ValueOf} from 'type-fest'; type Results = ValueOf; type ResultsCollection = Record; type NotificationSettings = Record; -type Notification = {status: Results; settings: typeof notificationSettings} +type Notification = {status: Results; settings: typeof notificationSettings}; const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); const openSettings: jest.Mock = jest.fn(() => {}); From 0e0399d3d31cebe1c4382d5ccea051c494ca8925 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Mon, 12 Feb 2024 09:49:13 +0000 Subject: [PATCH 4/7] [TS migration] Small code improvements --- __mocks__/react-native-permissions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts index e6f1aabff4c8..a1497c7cc2ac 100644 --- a/__mocks__/react-native-permissions.ts +++ b/__mocks__/react-native-permissions.ts @@ -4,7 +4,7 @@ import type {ValueOf} from 'type-fest'; type Results = ValueOf; type ResultsCollection = Record; type NotificationSettings = Record; -type Notification = {status: Results; settings: typeof notificationSettings}; +type Notification = {status: Results; settings: NotificationSettings}; const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); const openSettings: jest.Mock = jest.fn(() => {}); @@ -35,7 +35,7 @@ const requestNotifications: jest.Mock = jest.fn((options: Record notificationOptions.includes(option)) - .reduce((acc: ResultsCollection, option: string) => ({...acc, [option]: true}), { + .reduce((acc: NotificationSettings, option: string) => ({...acc, [option]: true}), { lockScreen: true, notificationCenter: true, }), From 1fdb8fdffafbd3be495c43ecdd4b83a486be9d99 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 14 Feb 2024 12:32:01 +0000 Subject: [PATCH 5/7] [TS migration][react-native-permissions] Remove the declare module --- __mocks__/react-native-permissions.ts | 6 +++--- src/types/modules/react-native-permissions.d.ts | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 src/types/modules/react-native-permissions.d.ts diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts index a1497c7cc2ac..b30931e6b85c 100644 --- a/__mocks__/react-native-permissions.ts +++ b/__mocks__/react-native-permissions.ts @@ -1,4 +1,4 @@ -import {PERMISSIONS, RESULTS} from 'react-native-permissions/dist/commonjs/permissions'; +import {PERMISSIONS, RESULTS} from 'react-native-permissions'; import type {ValueOf} from 'type-fest'; type Results = ValueOf; @@ -8,8 +8,8 @@ type Notification = {status: Results; settings: NotificationSettings}; const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); const openSettings: jest.Mock = jest.fn(() => {}); -const check: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); -const request: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); +const check = jest.fn(() => RESULTS.GRANTED as string); +const request = jest.fn(() => RESULTS.GRANTED as string); const checkLocationAccuracy: jest.Mock = jest.fn(() => 'full'); const requestLocationAccuracy: jest.Mock = jest.fn(() => 'full'); diff --git a/src/types/modules/react-native-permissions.d.ts b/src/types/modules/react-native-permissions.d.ts deleted file mode 100644 index fa9a73cc8f25..000000000000 --- a/src/types/modules/react-native-permissions.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'react-native-permissions/dist/commonjs/permissions'; From 81214959a52b318fda4e76b923de862389971efe Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 14 Feb 2024 12:43:50 +0000 Subject: [PATCH 6/7] Revert "[TS migration][react-native-permissions] Remove the declare module" This reverts commit 1fdb8fdffafbd3be495c43ecdd4b83a486be9d99. --- __mocks__/react-native-permissions.ts | 6 +++--- src/types/modules/react-native-permissions.d.ts | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 src/types/modules/react-native-permissions.d.ts diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts index b30931e6b85c..a1497c7cc2ac 100644 --- a/__mocks__/react-native-permissions.ts +++ b/__mocks__/react-native-permissions.ts @@ -1,4 +1,4 @@ -import {PERMISSIONS, RESULTS} from 'react-native-permissions'; +import {PERMISSIONS, RESULTS} from 'react-native-permissions/dist/commonjs/permissions'; import type {ValueOf} from 'type-fest'; type Results = ValueOf; @@ -8,8 +8,8 @@ type Notification = {status: Results; settings: NotificationSettings}; const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); const openSettings: jest.Mock = jest.fn(() => {}); -const check = jest.fn(() => RESULTS.GRANTED as string); -const request = jest.fn(() => RESULTS.GRANTED as string); +const check: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); +const request: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); const checkLocationAccuracy: jest.Mock = jest.fn(() => 'full'); const requestLocationAccuracy: jest.Mock = jest.fn(() => 'full'); diff --git a/src/types/modules/react-native-permissions.d.ts b/src/types/modules/react-native-permissions.d.ts new file mode 100644 index 000000000000..fa9a73cc8f25 --- /dev/null +++ b/src/types/modules/react-native-permissions.d.ts @@ -0,0 +1 @@ +declare module 'react-native-permissions/dist/commonjs/permissions'; From 296bd533813fa978e941b0ac0bf0b9d486c221ef Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 14 Feb 2024 12:46:43 +0000 Subject: [PATCH 7/7] [TS migration][react-native-permissions] Updated module declaration --- __mocks__/react-native-permissions.ts | 4 ++-- src/types/modules/react-native-permissions.d.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/__mocks__/react-native-permissions.ts b/__mocks__/react-native-permissions.ts index a1497c7cc2ac..67b7db830d94 100644 --- a/__mocks__/react-native-permissions.ts +++ b/__mocks__/react-native-permissions.ts @@ -8,8 +8,8 @@ type Notification = {status: Results; settings: NotificationSettings}; const openLimitedPhotoLibraryPicker: jest.Mock = jest.fn(() => {}); const openSettings: jest.Mock = jest.fn(() => {}); -const check: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); -const request: jest.Mock = jest.fn(() => RESULTS.GRANTED as string); +const check = jest.fn(() => RESULTS.GRANTED as string); +const request = jest.fn(() => RESULTS.GRANTED as string); const checkLocationAccuracy: jest.Mock = jest.fn(() => 'full'); const requestLocationAccuracy: jest.Mock = jest.fn(() => 'full'); diff --git a/src/types/modules/react-native-permissions.d.ts b/src/types/modules/react-native-permissions.d.ts index fa9a73cc8f25..82a008b95b66 100644 --- a/src/types/modules/react-native-permissions.d.ts +++ b/src/types/modules/react-native-permissions.d.ts @@ -1 +1,5 @@ -declare module 'react-native-permissions/dist/commonjs/permissions'; +declare module 'react-native-permissions/dist/commonjs/permissions' { + import {PERMISSIONS, RESULTS} from 'react-native-permissions'; + + export {PERMISSIONS, RESULTS}; +}