Skip to content

Add Vitest unit tests for auth-store Zustand state for issue 443#530

Open
parthpatidar03 wants to merge 1 commit into
param20h:devfrom
parthpatidar03:test/443-auth-store-unit-tests
Open

Add Vitest unit tests for auth-store Zustand state for issue 443#530
parthpatidar03 wants to merge 1 commit into
param20h:devfrom
parthpatidar03:test/443-auth-store-unit-tests

Conversation

@parthpatidar03

Copy link
Copy Markdown

Closes #443


📝 What does this PR do?

Adds 35 Vitest unit tests for the frontend auth-store.ts Zustand store, covering every action in the store:

  • login() — credential login, token saving, error handling
  • loginWithGoogle() — Google OAuth token flow
  • logout() — state/localStorage clearing, network failure resilience
  • register() — response data, state and localStorage cleanup
  • initializeAuth() — token rehydration from localStorage, 401 handling, skip-if-initialized guard
  • syncTokensRefreshed() — partial updates, preserves existing values, no-op on undefined
  • syncLoggedOut() — cross-tab logout sync
  • setHfToken() — HuggingFace token update

File added: frontend/src/store/auth-store.test.ts


🗂️ Type of Change

  • 🧪 Tests

🧪 How was this tested?

  • Added / updated tests

Tests run with the existing Vitest setup (vitest.config.ts + src/test/setup.ts), no new dependencies required.

Results:

Suite Tests Status
Initial state 4
login() 6
loginWithGoogle() 3
logout() 6
register() 3
initializeAuth() 4
syncTokensRefreshed() 5
syncLoggedOut() 2
setHfToken() 2
Total 35 ✅ All passed
✓ src/store/auth-store.test.ts (35 tests) 69ms

Test Files  1 passed (1)
     Tests  35 passed (35)
  Duration  4.65s

Run tests locally:

cd frontend
npx vitest run src/store/auth-store.test.ts

📸 Screenshots (if UI change)

N/A — test-only change, no UI affected.


⚠️ Anything to flag for reviewers?

  • api module is mocked via vi.mock("@/lib/api") — no real network calls made
  • Zustand store is reset before each test via useAuthStore.setState({...}) to ensure full isolation
  • logout() is tested with both successful and failing API calls — confirms state is cleared regardless of network errors (important for UX)
  • initializeAuth() skip-if-initialized guard is explicitly tested to prevent double-fetch bugs

✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@parthpatidar03 parthpatidar03 requested a review from param20h as a code owner June 8, 2026 06:25
@parthpatidar03

Copy link
Copy Markdown
Author

@param20h plz review

@param20h

Copy link
Copy Markdown
Owner

solve

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.

test(frontend): Write unit tests for auth-store (Zustand state)

2 participants