NMF-earth / nmf-app

Understand and reduce your carbon footprint 🌱 iOS & Android.
https://nmf.earth
GNU General Public License v3.0
488 stars 157 forks source link

BarCodeScanScreen tests #340

Open PierreBresson opened 2 years ago

PierreBresson commented 2 years ago

Summary

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

Kathuria commented 2 years ago

Can I work on this issue?

ijdershem commented 2 years ago

Can I work on this issue?

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

PierreBresson commented 2 years ago

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 ;)

ijdershem commented 2 years ago

@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! (:

PierreBresson commented 2 years ago

@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 :)

ijdershem commented 2 years ago

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