Skip to content

Add unit tests for Zustand stores and hooks, fix walletError stale cl… - #503

Merged
DeFiVC merged 1 commit into
ChainLearnOfficial:mainfrom
designsage8:feature/testing-and-bug-fixes
Sep 28, 2026
Merged

DeFiVC merged 1 commit into
ChainLearnOfficial:mainfrom
designsage8:feature/testing-and-bug-fixes

Conversation

@designsage8

@designsage8 designsage8 commented Sep 28, 2026 •

Copy link
Copy Markdown

Testing and Bug Fixes

This PR addresses multiple issues related to test coverage and bug fixes in the chainlearn-frontend project.

Changes

Task #478: Add unit tests for Zustand stores

  • Created src/store/__tests__/auth-store.test.ts - Comprehensive tests for auth-store including:

    • connect, disconnect, setJwt, applyRefreshedTokens
    • isTokenExpired, hydration, persistence
    • Session cookie handling
  • Created src/store/__tests__/course-store.test.ts - Comprehensive tests for course-store including:

    • setCurrentCourse, setEnrollments, enroll (with deduplication)
    • updateProgress (percent calculation, module tracking)
    • getProgress, persistence
  • Created src/store/__tests__/error-store.test.ts - Comprehensive tests for error-store including:

    • setError, clearError, setRetry
    • Transient error flag handling
    • Retry callback management

Task #479: Add missing tests for hooks without test coverage

  • Created src/lib/hooks/use-previous.test.tsx - Tests for previous value tracking
  • Created src/lib/hooks/use-unmount.test.tsx - Tests for unmount callback
  • Created src/lib/hooks/use-mount.test.tsx - Tests for mount callback
  • Created src/lib/hooks/use-boolean.test.tsx - Tests for boolean state helper
  • Created src/lib/hooks/use-swipe.test.tsx - Tests for swipe gesture detection
  • Created src/lib/hooks/use-click-outside.test.tsx - Tests for click outside detection
  • Created src/lib/hooks/use-sessions.test.tsx - Tests for user session management
  • Created src/lib/hooks/use-credentials.test.tsx - Tests for credential fetching and display
  • Created src/lib/hooks/use-rewards.test.tsx - Tests for reward fetching and claiming
  • Created src/lib/hooks/use-notifications.test.tsx - Tests for notification fetching and marking as read

Task #480: Fix stale closure over walletError in useAuth connectWallet

File: src/lib/hooks/use-auth.ts

Problem: The connectWallet callback included walletError in its dependency array, but reads it inside the catch block. Since walletError is state, the closure captures a stale value.

Solution:

  • Added walletErrorRef to track the latest walletError value
  • Changed catch block to check walletErrorRef.current instead of walletError
  • Removed walletError from the connectWallet dependency array

Task #481: Add Secure flag to session cookie in auth-store

File: src/store/auth-store.ts

Problem: The setSessionCookie function sets the chainlearn-session cookie without the Secure flag, allowing JWT transmission over unencrypted HTTP.

Solution:

  • Added logic to detect HTTPS protocol using window.location.protocol
  • Conditionally adds ; Secure flag when served over HTTPS
  • Maintains HTTP compatibility for local development

Testing

All new tests follow existing patterns from the codebase (e.g., use-debounce.test.tsx) and cover:

  • Successful operations
  • Error handling
  • Loading states
  • Abort/cleanup where applicable

Run tests with:

npm test

Type Checking

Run type checking to verify no type errors:

npm run typecheck

Branch Instructions

To create a branch and push these changes:

cd /home/emmanuel-ogheneovo/wave9/chainlearn-frontend

# Create and checkout a new branch
git checkout -b feature/testing-and-bug-fixes

# Add all changes
git add .

# Commit changes
git commit -m "Add unit tests for Zustand stores and hooks, fix walletError stale closure, add Secure flag to session cookie

- Add comprehensive unit tests for auth-store, course-store, error-store
- Add unit tests for 10 hooks without test coverage
- Fix stale closure over walletError in useAuth connectWallet (#480)
- Add Secure flag to session cookie in auth-store (#481)
- Close #478, #479, #480, #481"

# Push to remote
git push -u origin feature/testing-and-bug-fixes

Then create a pull request using the GitHub UI or CLI with the title:

Add unit tests for Zustand stores and hooks, fix walletError stale closure, add Secure flag to session cookie

And include this description in the PR body.

Closes #478,
Closes #479,
Closes #480,
Closes #481

…osure, add Secure flag to session cookie

- Add comprehensive unit tests for auth-store, course-store, error-store
- Add unit tests for 10 hooks without test coverage
- Fix stale closure over walletError in useAuth connectWallet (ChainLearnOfficial#480)
- Add Secure flag to session cookie in auth-store (ChainLearnOfficial#481)
- Close ChainLearnOfficial#478, ChainLearnOfficial#479, ChainLearnOfficial#480, ChainLearnOfficial#481
@drips-wave

drips-wave Bot commented Sep 28, 2026

Copy link
Copy Markdown

@designsage8 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@netlify

netlify Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for chainlearn failed.

Name Link
🔨 Latest commit 0158f4e
🔍 Latest deploy log https://app.netlify.com/projects/chainlearn/deploys/6aba9681004eef00080e96b5

@DeFiVC
DeFiVC merged commit 438db08 into ChainLearnOfficial:main Sep 28, 2026
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants