nmf-app
nmf-app copied to clipboard
BarCodeScanScreen tests
Summary
Do different tests for BarCodeScreen
inside BarCodeScreen.test.tsx
matching the different scenario
Can I work on this issue?
Can I work on this issue?
@Kathuria Did you end up picking this issue up? @PierreBresson Happy to pick this up if not
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 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 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 No worries, thank you for the direction; I will give this a shot tonight!