aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/stores/auth.test.tsx
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-01-04 17:43:59 +0900
committernsfisis <nsfisis@gmail.com>2026-01-04 19:09:58 +0900
commitf8e4be9b36a16969ac53bd9ce12ce8064be10196 (patch)
treeb2cf350d2e2e52803ff809311effb40da767d859 /src/client/stores/auth.test.tsx
parente1c9e5e89bb91bca2586470c786510c3e1c03826 (diff)
downloadkioku-f8e4be9b36a16969ac53bd9ce12ce8064be10196.tar.gz
kioku-f8e4be9b36a16969ac53bd9ce12ce8064be10196.tar.zst
kioku-f8e4be9b36a16969ac53bd9ce12ce8064be10196.zip
refactor(client): migrate state management from React Context to Jotai
Replace AuthProvider and SyncProvider with Jotai atoms for more granular state management and better performance. This migration: - Creates atoms for auth, sync, decks, cards, noteTypes, and study state - Uses atomFamily for parameterized state (e.g., cards by deckId) - Introduces StoreInitializer component for subscription initialization - Updates all components and pages to use useAtomValue/useSetAtom - Updates all tests to use Jotai Provider with createStore pattern 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/client/stores/auth.test.tsx')
-rw-r--r--src/client/stores/auth.test.tsx160
1 files changed, 0 insertions, 160 deletions
diff --git a/src/client/stores/auth.test.tsx b/src/client/stores/auth.test.tsx
deleted file mode 100644
index 1769011..0000000
--- a/src/client/stores/auth.test.tsx
+++ /dev/null
@@ -1,160 +0,0 @@
-/**
- * @vitest-environment jsdom
- */
-import { act, renderHook, waitFor } from "@testing-library/react";
-import type { ReactNode } from "react";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { apiClient } from "../api/client";
-import { AuthProvider, useAuth } from "./auth";
-
-// Mock the apiClient
-vi.mock("../api/client", () => ({
- apiClient: {
- login: vi.fn(),
- logout: vi.fn(),
- isAuthenticated: vi.fn(),
- getTokens: vi.fn(),
- onSessionExpired: vi.fn(() => vi.fn()),
- },
- ApiClientError: class ApiClientError extends Error {
- constructor(
- message: string,
- public status: number,
- public code?: string,
- ) {
- super(message);
- this.name = "ApiClientError";
- }
- },
-}));
-
-const wrapper = ({ children }: { children: ReactNode }) => (
- <AuthProvider>{children}</AuthProvider>
-);
-
-describe("useAuth", () => {
- beforeEach(() => {
- vi.clearAllMocks();
- vi.mocked(apiClient.getTokens).mockReturnValue(null);
- vi.mocked(apiClient.isAuthenticated).mockReturnValue(false);
- });
-
- afterEach(() => {
- vi.restoreAllMocks();
- });
-
- it("throws error when used outside AuthProvider", () => {
- // Suppress console.error for this test
- const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
-
- expect(() => {
- renderHook(() => useAuth());
- }).toThrow("useAuth must be used within an AuthProvider");
-
- consoleSpy.mockRestore();
- });
-
- it("returns initial unauthenticated state", async () => {
- const { result } = renderHook(() => useAuth(), { wrapper });
-
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- expect(result.current.user).toBeNull();
- expect(result.current.isAuthenticated).toBe(false);
- });
-
- it("returns authenticated state when tokens exist", async () => {
- vi.mocked(apiClient.getTokens).mockReturnValue({
- accessToken: "test-access-token",
- refreshToken: "test-refresh-token",
- });
- vi.mocked(apiClient.isAuthenticated).mockReturnValue(true);
-
- const { result } = renderHook(() => useAuth(), { wrapper });
-
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- expect(result.current.isAuthenticated).toBe(true);
- });
-
- describe("login", () => {
- it("logs in and sets user", async () => {
- const mockUser = { id: "user-1", username: "testuser" };
- vi.mocked(apiClient.login).mockResolvedValue({
- accessToken: "access-token",
- refreshToken: "refresh-token",
- user: mockUser,
- });
- vi.mocked(apiClient.isAuthenticated).mockReturnValue(true);
-
- const { result } = renderHook(() => useAuth(), { wrapper });
-
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- await act(async () => {
- await result.current.login("testuser", "password123");
- });
-
- expect(apiClient.login).toHaveBeenCalledWith("testuser", "password123");
- expect(result.current.user).toEqual(mockUser);
- });
-
- it("propagates login errors", async () => {
- vi.mocked(apiClient.login).mockRejectedValue(
- new Error("Invalid credentials"),
- );
-
- const { result } = renderHook(() => useAuth(), { wrapper });
-
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- await expect(
- act(async () => {
- await result.current.login("testuser", "wrongpassword");
- }),
- ).rejects.toThrow("Invalid credentials");
- });
- });
-
- describe("logout", () => {
- it("logs out and clears user", async () => {
- const mockUser = { id: "user-1", username: "testuser" };
- vi.mocked(apiClient.login).mockResolvedValue({
- accessToken: "access-token",
- refreshToken: "refresh-token",
- user: mockUser,
- });
- vi.mocked(apiClient.isAuthenticated).mockReturnValue(true);
-
- const { result } = renderHook(() => useAuth(), { wrapper });
-
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- // Login first
- await act(async () => {
- await result.current.login("testuser", "password123");
- });
-
- expect(result.current.user).toEqual(mockUser);
-
- // Now logout
- vi.mocked(apiClient.isAuthenticated).mockReturnValue(false);
- act(() => {
- result.current.logout();
- });
-
- expect(apiClient.logout).toHaveBeenCalled();
- expect(result.current.user).toBeNull();
- });
- });
-});