Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarCodeScanScreen tests #340

Open
PierreBresson opened this issue Oct 18, 2021 · 6 comments
Open

BarCodeScanScreen tests #340

PierreBresson opened this issue Oct 18, 2021 · 6 comments
Assignees

Comments

@PierreBresson
Copy link
Member

PierreBresson commented Oct 18, 2021

Summary

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

@PierreBresson PierreBresson added the hacktoberfest https://hacktoberfest.digitalocean.com/ label Oct 18, 2021
@Kathuria
Copy link

Can I work on this issue?

@PierreBresson PierreBresson removed the hacktoberfest https://hacktoberfest.digitalocean.com/ label Nov 1, 2021
@ijdershem
Copy link
Contributor

ijdershem commented Nov 14, 2021

Can I work on this issue?

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

@PierreBresson
Copy link
Member Author

PierreBresson commented Nov 14, 2021

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 PierreBresson changed the title BarCodeScanScreen BarCodeScanScreen tests Nov 14, 2021
@ijdershem
Copy link
Contributor

ijdershem commented Nov 15, 2021

@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
Copy link
Member Author

PierreBresson commented Nov 15, 2021

@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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants