Skip to content

Fix: Improve useTorchLight hook and add tests #13

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JonatanSalas
Copy link
Member

This commit addresses issues with the useTorchLight hook to improve its reliability across different browsers and adds comprehensive testing.

Key changes include:

  • Refactored turnOn function:
    • Prioritizes track.getCapabilities() for torch detection.
    • Uses ImageCapture API as a secondary method.
    • Includes a fallback to direct applyConstraints.
  • Refactored turnOff function:
    • Consolidated logic to use track.applyConstraints({ advanced: [{ torch: false }] }).
    • Removed outdated stream.applyConstraints and unrelated RTCPeerConnection logic.
  • Improved Error Handling and Logging:
    • Added comprehensive try-catch blocks.
    • Ensured consistent onError callback usage.
    • Implemented descriptive debug logging.
  • Enhanced Type Checking and Safeguards:
    • Added instance checks for MediaStream and MediaStreamTrack.
    • Implemented checks for method existence (e.g., getCapabilities, applyConstraints, ImageCapture) before calls.
    • Added checks for capabilities objects.
  • Added Unit Tests:
    • Created useTorchLight.test.ts with extensive tests using @testing-library/react-hooks.
    • Mocked browser APIs (MediaStream, MediaStreamTrack, ImageCapture, navigator.vibrate).
    • Covered successful on/off cycles, various error scenarios, callback invocations, and debug logging.
    • Installed necessary devDependencies (@testing-library/react-hooks, react, react-dom).
  • Set up GitHub Actions:
    • Created a ci.yml workflow to run tests automatically on push to main and pull requests.
    • Added a "test": "jest" script to package.json.

google-labs-jules bot and others added 2 commits May 20, 2025 18:30
This commit addresses issues with the `useTorchLight` hook to improve its reliability across different browsers and adds comprehensive testing.

Key changes include:

- Refactored `turnOn` function:
    - Prioritizes `track.getCapabilities()` for torch detection.
    - Uses `ImageCapture` API as a secondary method.
    - Includes a fallback to direct `applyConstraints`.
- Refactored `turnOff` function:
    - Consolidated logic to use `track.applyConstraints({ advanced: [{ torch: false }] })`.
    - Removed outdated `stream.applyConstraints` and unrelated RTCPeerConnection logic.
- Improved Error Handling and Logging:
    - Added comprehensive try-catch blocks.
    - Ensured consistent `onError` callback usage.
    - Implemented descriptive debug logging.
- Enhanced Type Checking and Safeguards:
    - Added instance checks for MediaStream and MediaStreamTrack.
    - Implemented checks for method existence (e.g., `getCapabilities`, `applyConstraints`, `ImageCapture`) before calls.
    - Added checks for capabilities objects.
- Added Unit Tests:
    - Created `useTorchLight.test.ts` with extensive tests using `@testing-library/react-hooks`.
    - Mocked browser APIs (`MediaStream`, `MediaStreamTrack`, `ImageCapture`, `navigator.vibrate`).
    - Covered successful on/off cycles, various error scenarios, callback invocations, and debug logging.
    - Installed necessary devDependencies (`@testing-library/react-hooks`, `react`, `react-dom`).
- Set up GitHub Actions:
    - Created a `ci.yml` workflow to run tests automatically on push to main and pull requests.
    - Added a "test": "jest" script to `package.json`.
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

Successfully merging this pull request may close these issues.

1 participant