Skip to content

Commit

Permalink
Upgrade to Expo 51.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed May 28, 2024
1 parent 45c7df4 commit 225b2a5
Show file tree
Hide file tree
Showing 9 changed files with 9,174 additions and 21,862 deletions.
8 changes: 0 additions & 8 deletions jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ jest.mock('react-native/Libraries/Linking/Linking', () => ({
openURL: jest.fn()
}))

jest.mock('expo-permissions', () => {
return {
askAsync: jest.fn(),
MEDIA_LIBRARY: 'Example Media Library',
CAMERA: 'Example Camera'
}
})

jest.mock('sentry-expo', () => {
return {
Native: {
Expand Down
30,891 changes: 9,109 additions & 21,782 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 18 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/react": "^18.2.14",
"@types/react": "~18.2.79",
"@types/react-test-renderer": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"del-cli": "^5.0.0",
Expand All @@ -29,14 +29,14 @@
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-react": "^7.33.2",
"expo-doctor": "^1.1.2",
"jest": "^29.2.1",
"jest-expo": "^49.0.0",
"jest": "^29.4.0",
"jest-expo": "^51.0.0",
"metro-react-native-babel-preset": "0.67.0",
"npm-run-all": "^4.1.5",
"react": "^18.2.0",
"react-native": "0.72.10",
"react-test-renderer": "^18.2.0",
"typescript": "^5.1.6"
"react": "18.2.0",
"react-native": "0.74.1",
"react-test-renderer": "18.2.0",
"typescript": "~5.3.3"
},
"peerDependencies": {
"react": "*",
Expand All @@ -59,18 +59,17 @@
"sideEffects": false,
"dependencies": {
"events": "3.3.0",
"expo": "^49.0.8",
"expo-constants": "^14.2.1",
"expo-crypto": "^12.4.1",
"expo-file-system": "~15.4.5",
"expo-image-picker": "^14.3.2",
"expo-intent-launcher": "^10.7.0",
"expo-media-library": "^15.4.1",
"expo-permissions": "^14.2.1",
"expo-secure-store": "^12.3.1",
"expo": "^51.0.8",
"expo-constants": "~16.0.1",
"expo-crypto": "~13.0.2",
"expo-file-system": "~17.0.1",
"expo-image-picker": "~15.0.5",
"expo-intent-launcher": "~11.0.1",
"expo-media-library": "~16.0.3",
"expo-secure-store": "~13.0.1",
"filter-validate-email": "^1.1.3",
"react-native-gesture-handler": "^2.12.0",
"react-native-safe-area-context": "^4.6.3",
"sentry-expo": "~7.1.0"
"react-native-gesture-handler": "~2.16.1",
"react-native-safe-area-context": "4.10.1",
"sentry-expo": "~7.0.0"
}
}
19 changes: 9 additions & 10 deletions react-native/services/PermissionHelper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as Permissions from 'expo-permissions'
import type { PermissionResponse } from 'expo-modules-core'
import type { PermissionHelperInterface } from '../../types/PermissionHelperInterface'
import { showSettingsScreen } from '../../utilities/showSettingsScreen'

/**
* Provides helpers for working with permissions.
*/
export class PermissionHelper implements PermissionHelperInterface {
// TODO: this requires revision as expo-permissions is deprecated

/**
* Acquires one or more Expo permissions.
* @param permissions The permission(s) to acquire.
Expand All @@ -19,16 +17,17 @@ export class PermissionHelper implements PermissionHelperInterface {
* might only grant access to a small subset of resources.
*/
async acquire (
permissions: readonly Permissions.PermissionType[],
permissions: ReadonlyArray<() => Promise<PermissionResponse>>,
onFailure: (showSettingsScreen: () => Promise<void>) => Promise<void>,
onSuccess: () => Promise<void>
): Promise<void> {
const result = await Permissions.askAsync(...permissions)

if (result.granted) {
await onSuccess()
} else {
await onFailure(showSettingsScreen)
for (const permission of permissions) {
if (!(await permission()).granted) {
await onFailure(showSettingsScreen)
return
}
}

await onSuccess()
}
}
4 changes: 2 additions & 2 deletions react-native/services/PermissionHelper/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Provides helpers for working with permissions.
## Usage

```tsx
import * as Permissions from "expo-permissions";
import * as ImagePicker from "expo-image-picker";
import type { PermissionHelper } from "react-native-app-helpers";

const permissionHelper = new PermissionHelper();

await permissionHelper.acquire(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
async (showSettingsScreen) => {
console.log(`Redirecting to settings screen for privacy settings as permissions were denied...`);
await showSettingsScreen();
Expand Down
35 changes: 16 additions & 19 deletions react-native/services/PermissionHelper/unit.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
import { showSettingsScreen, PermissionHelper } from '../../..'
const Permissions = jest.requireMock('expo-permissions')

test('executes the success callback when permissions are granted', async () => {
Permissions.askAsync.mockResolvedValue({ granted: true })

const onFailure = jest.fn().mockResolvedValue(undefined)
const onSuccess = jest.fn().mockResolvedValue(undefined)

const firstPermission = jest.fn().mockResolvedValue({ granted: true })
const secondPermission = jest.fn().mockResolvedValue({ granted: true })
const thirdPermission = jest.fn().mockResolvedValue({ granted: true })

const permissionHelper = new PermissionHelper()

await permissionHelper.acquire(
['locationForeground', 'motion', 'notifications'],
[firstPermission, secondPermission, thirdPermission],
onFailure,
onSuccess
)

expect(Permissions.askAsync).toBeCalledTimes(1)
expect(Permissions.askAsync).toBeCalledWith(
'locationForeground',
'motion',
'notifications'
)
expect(firstPermission).toHaveBeenCalledTimes(1)
expect(secondPermission).toHaveBeenCalledTimes(1)
expect(thirdPermission).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect(onSuccess).toBeCalledTimes(1)
})

test('executes the failure callback when permissions are denied', async () => {
Permissions.askAsync.mockResolvedValue({ granted: false })

const onFailure = jest.fn().mockResolvedValue(undefined)
const onSuccess = jest.fn().mockResolvedValue(undefined)

const firstPermission = jest.fn().mockResolvedValue({ granted: true })
const secondPermission = jest.fn().mockResolvedValue({ granted: false })
const thirdPermission = jest.fn()

const permissionHelper = new PermissionHelper()

await permissionHelper.acquire(
['locationForeground', 'motion', 'notifications'],
[firstPermission, secondPermission, thirdPermission],
onFailure,
onSuccess
)

expect(Permissions.askAsync).toBeCalledTimes(1)
expect(Permissions.askAsync).toBeCalledWith(
'locationForeground',
'motion',
'notifications'
)
expect(firstPermission).toHaveBeenCalledTimes(1)
expect(secondPermission).toHaveBeenCalledTimes(1)
expect(thirdPermission).not.toHaveBeenCalled()
expect(onFailure).toBeCalledTimes(1)
expect(onFailure).toBeCalledWith(showSettingsScreen)
expect(onSuccess).not.toHaveBeenCalled()
Expand Down
7 changes: 3 additions & 4 deletions react-native/services/PictureHelper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Permissions from 'expo-permissions'
import * as ImagePicker from 'expo-image-picker'
import * as MediaLibrary from 'expo-media-library'
import type { FileStoreInterface } from '../../..'
Expand Down Expand Up @@ -26,7 +25,7 @@ export class PictureHelper implements PictureHelperInterface {
onSuccess: (uuid: string) => Promise<void>
): Promise<void> {
await this.permissionHelper.acquire(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
async () => {
const result = await ImagePicker.launchCameraAsync({
Expand Down Expand Up @@ -66,7 +65,7 @@ export class PictureHelper implements PictureHelperInterface {
onSuccess: (uuid: string) => Promise<void>
): Promise<void> {
await this.permissionHelper.acquire(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
async () => {
const result = await ImagePicker.launchImageLibraryAsync({
Expand Down Expand Up @@ -99,7 +98,7 @@ export class PictureHelper implements PictureHelperInterface {
onSuccess: (uuids: readonly string[]) => Promise<void>
): Promise<void> {
await this.permissionHelper.acquire(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
async () => {
const result = await ImagePicker.launchImageLibraryAsync({
Expand Down
31 changes: 15 additions & 16 deletions react-native/services/PictureHelper/unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '../../..'
const ImagePicker = jest.requireMock('expo-image-picker')
const MediaLibrary = jest.requireMock('expo-media-library')
const Permissions = jest.requireMock('expo-permissions')

test('take picture without saving to media library permission denied', async () => {
const fileStore: FileStoreInterface = {
Expand Down Expand Up @@ -53,7 +52,7 @@ test('take picture without saving to media library permission denied', async ()
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -104,7 +103,7 @@ test('take picture saving to media library permission denied', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -166,7 +165,7 @@ test('take picture without saving to media library cancelled', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -229,7 +228,7 @@ test('take picture without saving to media library none selected', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -291,7 +290,7 @@ test('take picture saving to media library cancelled', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -354,7 +353,7 @@ test('take picture saving to media library none selected', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -421,7 +420,7 @@ test('take picture without saving to media library successful', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -498,7 +497,7 @@ test('take picture saving to media library successful', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.CAMERA, Permissions.MEDIA_LIBRARY],
[ImagePicker.requestCameraPermissionsAsync, ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -552,7 +551,7 @@ test('select one picture from media library permission denied', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -614,7 +613,7 @@ test('select one picture from media library cancelled', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -677,7 +676,7 @@ test('select one picture from media library no assets', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -744,7 +743,7 @@ test('select one picture from media library successful', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -794,7 +793,7 @@ test('select multiple pictures from media library permission denied', async () =
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -856,7 +855,7 @@ test('select multiple pictures from media library cancelled', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down Expand Up @@ -941,7 +940,7 @@ test('select multiple pictures from media library successful', async () => {
expect(fileStore.unload).not.toHaveBeenCalled()
expect(permissionHelper.acquire).toBeCalledTimes(1)
expect(permissionHelper.acquire).toBeCalledWith(
[Permissions.MEDIA_LIBRARY],
[ImagePicker.requestMediaLibraryPermissionsAsync],
onPermissionDenied,
expect.any(Function)
)
Expand Down
4 changes: 2 additions & 2 deletions react-native/types/PermissionHelperInterface/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as Permissions from 'expo-permissions'
import type { PermissionResponse } from 'expo-modules-core'

/**
* The methods made available by the PermissionHelper implementation.
Expand All @@ -15,7 +15,7 @@ export interface PermissionHelperInterface {
* might only grant access to a small subset of resources.
*/
acquire: (
permissions: readonly Permissions.PermissionType[],
permissions: ReadonlyArray<() => Promise<PermissionResponse>>,
onFailure: (showSettingsScreen: () => Promise<void>) => Promise<void>,
onSuccess: () => Promise<void>
) => Promise<void>
Expand Down

0 comments on commit 225b2a5

Please sign in to comment.