Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

How to test expo permissions has been called with camera permissions

$
0
0

Here I have a withPermissions HOC component like below

export function withPermissions<T>(
  WrappedComponent: React.ComponentType<T>,
  requestedPermissions: RequestedPermissions,
) {
  return class WithPermissions extends React.Component<
    WithPermissionsProps & T,
    WithPermissionsState
  > {
    readonly state: WithPermissionsState;
    constructor(props: WithPermissionsProps & T) {
      super(props);
      this.state = {
        focusSubscription: undefined,
        permissions: {},
      };
    }

    readonly checkPermissions = async () => {
      const { permissions } = await Permissions.askAsync(...requestedPermissions);

      const nextPermissions: RecievedPermissions = Object.entries(permissions).reduce(
        (allPermissions, [permissionType, value]) => ({
          ...allPermissions,
          [permissionType]: value.status,
        }),
        {},
      );

      this.setState(state => ({ ...state, permissions: nextPermissions }));
    };

    componentWillMount() {
      this.checkPermissions();
    }

    render() {
      const { permissions } = this.state;

      return <WrappedComponent permissions={permissions} {...(this.props as T)} />;
    }
  };
}

I wrapped my screen with HOC

export const ScanScreen = withPermissions<ScanScreenProps>(component, [
  Permissions.CAMERA,
]);

my test code is looks like

 .mock("expo-permissions", () => ({
    Permissions: {
      askAsync: jest.fn(),
    },
  }));

test("Correct permissions are asked for in scanner screen", async () => {
  const { environment } = renderWithPermissions(ScanScreen);
  await resolveMostRecentOperation<CompleteScanScreenQuery>(environment);
  Permissions.askAsync(Permissions.CAMERA);
});

when i tried with above code i got the below error

TypeError: Permissions.askAsync is not a function

when i change the mocking from expo-permissions to expo like below

.mock("expo", () => ({
    Permissions: {
      askAsync: jest.fn(),
    },
  }));

  const { environment } = renderWithPermissions(ScanScreen);
  await resolveMostRecentOperation<CompleteScanScreenQuery>(environment);
  Permissions.askAsync(Permissions.CAMERA); //undefined

the error will be gone and test is passed but result is undefined

and i tried below code but i am unable to render the BarCodeScanner

test("Correct permissions are asked for in scanner screen", async () => {
  const { environment, getByTestId } = renderWithPermissions(ScanScreen);
  await resolveMostRecentOperation<CompleteScreenQuery>(environment);
  const permissionsSpy = jest.spyOn(Permissions, "askAsync");
  Permissions.askAsync(Permissions.CAMERA);
  expect(permissionsSpy).toHaveBeenCalledWith(Permissions.CAMERA);
  getByTestId("barCodeScanner");
});

error

Unable to find an element with the testID of: barCodeScanner

I am not sure how to do it. Can someone please help me out


Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>