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

feat(local-storage): enhance local storage availability checks FE-1221 #132

Merged
merged 54 commits into from
Dec 25, 2024

Conversation

nelitow
Copy link
Contributor

@nelitow nelitow commented Dec 22, 2024

Description

This PR updates the local-storage package with some safety checking and the test should persist todos state on local storage in ReactFactory.test.tsx to account for new defensive programming features added to the local-storage package.

Also fixes AUDIT packages.

Changes

Local Storage Package Updates

  1. Added checkLocalStorageAvailability method to safely verify localStorage access:

    private checkLocalStorageAvailability(): boolean {
      try {
        if (typeof window === 'undefined' || !window.localStorage) return false;
        const testKey = '__storage_test__';
        localStorage.setItem(testKey, testKey);
        localStorage.removeItem(testKey);
        return true;
      } catch {
        return false;
      }
    }
  2. Added try-catch blocks around all localStorage operations to handle potential failures:

    • setItem: Handles quota exceeded errors
    • getItem: Handles parsing errors
    • removeItem: Handles removal failures
    • clear: Handles bulk operation failures

Test Updates

  • Modified test assertions to filter out localStorage calls made during availability checks
  • Added more specific assertions to verify the actual todo data is saved correctly
  • Maintained the original test's purpose while making it more resilient to implementation details

Why

The local-storage package was enhanced with defensive programming features to handle various edge cases:

  1. Environments where localStorage is not available (e.g., SSR, disabled by user)
  2. Storage quota exceeded errors
  3. Malformed data parsing errors
  4. General localStorage operation failures

These safety checks caused the original test to fail because:

  1. The availability check makes an additional call to localStorage.setItem
  2. The test was strictly asserting exactly one call to setItem

The updated test now:

  1. Filters out the availability check calls (using __storage_test__)
  2. Verifies there is exactly one non-test call to save the todo
  3. Explicitly checks that the saved data contains the correct todo information

Testing

  • All tests pass with the updated assertion
  • The test still catches potential issues with todo persistence

@nelitow nelitow self-assigned this Dec 22, 2024
@nelitow nelitow changed the title feat(local-storage): enhance local storage availability checks feat(local-storage): enhance local storage availability checks FE-1221 Dec 22, 2024
Copy link
Contributor

github-actions bot commented Dec 24, 2024

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 86.89% 338/389
🔴 Branches 47.62% 50/105
🟢 Functions 92.93% 92/99
🟢 Lines 89.33% 335/375

Test suite run success

23 tests passing in 3 suites.

Report generated by 🧪jest coverage report action from 90588e1

…ore, and update tests for new local-storage checks
…anch and configure upstream tracking for the main branch.
…anch and set the pull request reference. Removed unused PR number and skip-test parameters for cleaner configuration.
…eamline branch fetching. Added steps to set global user details and ensure the main branch is checked out before the pull request branch, enhancing the workflow's reliability.
@LuizAsFight LuizAsFight enabled auto-merge (squash) December 25, 2024 21:45
@LuizAsFight LuizAsFight merged commit 5a9cf49 into main Dec 25, 2024
8 checks passed
@LuizAsFight LuizAsFight deleted the nj/feat/check-localStorage-availability branch December 25, 2024 21:45
nelitow pushed a commit that referenced this pull request Dec 25, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @fuels/local-storage@0.26.0

### Minor Changes

- Check for localStorage availability, by
[@nelitow](https://github.com/nelitow) (See
[#132](#132))

## @fuels/react-xstore@0.26.0

### Minor Changes

- Update tests to handle the new local-storage checks, by
[@nelitow](https://github.com/nelitow) (See
[#132](#132))

## @fuels/changeset@0.26.0



## @fuels/eslint-plugin@0.26.0



## @fuels/jest@0.26.0



## @fuels/kms-account@0.26.0



## @fuels/prettier-config@0.26.0



## @fuels/proxy-cli@0.26.0



## @fuels/ts-config@0.26.0



## @fuels/tsup-config@0.26.0

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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.

2 participants