0

I am trying to do a unit test to one of my react native components. One scenario requires me to mock expo-camera requestCameraPermissionsAsync() method but don't know how. What I'm trying to do is to mock the status to always have the granted value.

Initial approach, below:

jest.mock('expo-camera', () => {
  const PermissionsCamera = jest.requireActual('expo-camera');

  return {
    ...PermissionsCamera,
    requestCameraPermissionsAsync: () =>
      new Promise(resolve => resolve({granted: true, status: 'granted'})),
  };
});

But that doesn't work. Need help, is there something wrong with the code above? Thank you

Update: How I implemented in the component:

import {Camera} from 'expo-camera'

useEffect(() => {
 (async () => {
   const {status} = await Camera.requestCameraPermissionsAsync();
   // additional logic when status is equal to 'granted'
 })();
}, []);
fmsthird
  • 1,745
  • 2
  • 17
  • 34

1 Answers1

1

2023 answer

I was struggling —the same as you— and then I realized Camera.requestPermissionsAsync() is Deprecated.

I ended up using the hook useCameraPermissions

and using the last version of expo and jest-expo

"expo": "^48.0.6",
"jest-expo": "^48.0.2",
David Leuliette
  • 1,595
  • 18
  • 26