Skip to content

test(auth): add unit testing suite for LocalStorageService#541

Open
may-tas wants to merge 1 commit intoCircuitVerse:masterfrom
may-tas:test-local-storage-service
Open

test(auth): add unit testing suite for LocalStorageService#541
may-tas wants to merge 1 commit intoCircuitVerse:masterfrom
may-tas:test-local-storage-service

Conversation

@may-tas
Copy link
Copy Markdown

@may-tas may-tas commented Mar 19, 2026

Fixes #540

Describe the changes you have made in this PR -

  • Added a unit testing suite test/service_tests/local_storage_service_test.dart for the core LocalStorageService.
  • Wired up identical mock structures found in database_service_test.dart, primarily utilizing SharedPreferences.setMockInitialValues({}).
  • Hand-wrote tests guaranteeing that token, isLoggedIn, and currentUser serialize, retrieve, and delete values reliably.

Screenshots of the changes (If any) -
N/A (Test Suite only)

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced local storage system to properly remove authentication tokens and user information when cleared, ensuring stale data doesn't persist.
  • Tests

    • Added comprehensive test suite for the local storage service to verify data persistence and retrieval functionality.

@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 19, 2026

Deploy Preview for cv-mobile-app-web ready!

Name Link
🔨 Latest commit b77c779
🔍 Latest deploy log https://app.netlify.com/projects/cv-mobile-app-web/deploys/69bbcf3fdba13600087111ec
😎 Deploy Preview https://deploy-preview-541--cv-mobile-app-web.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 19, 2026

Walkthrough

This pull request adds explicit null-handling behavior to LocalStorageService._saveToDisk<T> and introduces a comprehensive test suite for the service. The method now removes keys from SharedPreferences when content is null, instead of skipping writes. The new test file verifies the service's getter/setter behavior for three persisted properties: isLoggedIn (defaults to false), token (defaults to null), and currentUser (defaults to null). Tests confirm that each property can be set, retrieved, and deleted via null assignment.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the primary change: adding unit tests for LocalStorageService.
Linked Issues check ✅ Passed All requirements from issue #540 are met: test suite created at specified location, SharedPreferences mocking implemented, and tests verify serialization/retrieval/deletion for all three properties.
Out of Scope Changes check ✅ Passed All changes are directly related to issue #540 objectives; the PR adds tests and a minor null-handling fix to LocalStorageService without introducing unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lib/services/local_storage_service.dart (1)

82-84: ⚠️ Potential issue | 🟠 Major

Bug: currentUser = null does not trigger key removal.

When userToSave is null, json.encode(userToSave?.toJson()) evaluates to json.encode(null) which returns the string "null" — not null. This bypasses the new null-handling logic at line 34 and stores the literal string "null" instead of deleting the key.

The getter's workaround (|| userJson == 'null' at line 75) masks this bug, but the behavior is inconsistent with the token setter and the PR objective of properly removing keys on null assignment.

🐛 Proposed fix to properly delete the key when userToSave is null
 set currentUser(User? userToSave) {
-  _saveToDisk(USER, json.encode(userToSave?.toJson()));
+  _saveToDisk(USER, userToSave == null ? null : json.encode(userToSave.toJson()));
 }
🧹 Nitpick comments (2)
test/service_tests/local_storage_service_test.dart (2)

11-19: Consider reinitializing _preferences in setUp for proper test isolation.

The storage instance is obtained once in setUpAll, but LocalStorageService._preferences is a static variable. While SharedPreferences.setMockInitialValues({}) in setUp replaces the underlying mock store, this pattern is fragile—if the mock implementation changes or the service caches values differently, tests could leak state.

For robust isolation, consider resetting the static _preferences to null in tearDown (would require exposing a test helper or @visibleForTesting reset method), or re-fetching the storage instance after each mock reset.


42-66: Test passes but doesn't verify actual key deletion.

This test will pass even with the bug in the currentUser setter (which stores "null" string instead of removing the key). The getter's workaround masks the issue.

Once the setter bug is fixed, consider adding a verification that the key is actually removed from SharedPreferences:

♻️ Optional: Add explicit key-removal verification
// After setting currentUser = null, verify key is removed
storage.currentUser = null;
expect(storage.currentUser, null);

// Optional: verify key is actually removed from SharedPreferences
final prefs = await SharedPreferences.getInstance();
expect(prefs.containsKey('logged_in_user'), false);

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: eb061e09-9b5a-470e-a2ce-11879ee571ad

📥 Commits

Reviewing files that changed from the base of the PR and between f621531 and b77c779.

📒 Files selected for processing (2)
  • lib/services/local_storage_service.dart
  • test/service_tests/local_storage_service_test.dart

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.

chore(tests): introduce test suite for LocalStorageService

1 participant