nmf-app icon indicating copy to clipboard operation
nmf-app copied to clipboard

BarCodeScanScreen tests

Open PierreBresson opened this issue 3 years ago • 6 comments

Summary

Do different tests for BarCodeScreen inside BarCodeScreen.test.tsx matching the different scenario

PierreBresson avatar Oct 18 '21 15:10 PierreBresson

Can I work on this issue?

Kathuria avatar Oct 24 '21 12:10 Kathuria

Can I work on this issue?

@Kathuria Did you end up picking this issue up? @PierreBresson Happy to pick this up if not

ijdershem avatar Nov 14 '21 00:11 ijdershem

You can go for it, the comment is 21 days old and no pr has been done since then. I've pushed some changes so make sure to pull main first ;)

PierreBresson avatar Nov 14 '21 08:11 PierreBresson

@PierreBresson Any chance you have experience testing useEffect/useState functionality? Not seeing any relevant tests in the code base and having a pretty difficult time figuring out how to trigger a component state update when user permission is granted for the camera.

This is the approach I'm taking right now:

it("renders correctly if permission is granted", async () => {
    // Mock useState before rendering your component
    const mockPermissionPromise:Promise<PermissionResponse> = new Promise((resolve) => ({
      status: PermissionStatus.GRANTED, 
      expires: "never", 
      granted: true, 
      canAskAgain: true,
    }));

    const permissionRequestSpy = jest.spyOn(BarCodeScanner, 'requestPermissionsAsync').mockReturnValue(mockPermissionPromise);
    let tree;
    await act( async () => {
      tree = create(<BarCodeScanScreen />);
    });
    expect(permissionRequestSpy).toHaveBeenCalled();
    expect(tree.toJSON()).toMatchSnapshot();
    permissionRequestSpy.mockRestore();
  });

However, the rendered tree still appears to be that of the hasPermission === null state:

exports['BarCodeScanScreen renders correctly if permission is granted 1'] = '
<View
  style={
    Object {
      "backgroundColor": "#FFFFFF",
      "flex": 1,
      "paddingHorizontal": 16,
    }
  }
/>
';

Will continue working for a solution but any help is appreciated! (:

ijdershem avatar Nov 15 '21 01:11 ijdershem

@ijdershem Unfortunately, I don't have a big experience with hooks testing, but you might have to mock expo-barcode-scanner by adding it to jest -> mock -> node_modules. Hope that helps :)

PierreBresson avatar Nov 15 '21 12:11 PierreBresson

@PierreBresson No worries, thank you for the direction; I will give this a shot tonight!

ijdershem avatar Nov 15 '21 15:11 ijdershem