diff --git a/CHANGELOG.md b/CHANGELOG.md index 18d0b92..56fa57d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [CalVer](https://calver.org/). - A deck no longer ends up with 0 cards when it is selected after saving the token again. The deck is now fetched for the new repository. +- The selected deck is restored after reloading the page and saving the token + again. ## [2026.11.09] - 2026-11-09 diff --git a/src/hooks/use-game-setup.test.ts b/src/hooks/use-game-setup.test.ts index fa25d51..32ae3a5 100644 --- a/src/hooks/use-game-setup.test.ts +++ b/src/hooks/use-game-setup.test.ts @@ -1,4 +1,4 @@ -import { act, renderHook } from '@testing-library/react'; +import { act, renderHook, waitFor } from '@testing-library/react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { DeckManager } from '@/lib/karuta/deck/deck-manager'; @@ -8,6 +8,11 @@ import { useGameSetup } from './use-game-setup'; import type { ProtopediaInMemoryRepository } from 'promidas'; +const STORAGE_KEY = 'pp-karuta-deck-setup'; + +const savedDeckRecipeId = () => + JSON.parse(sessionStorage.getItem(STORAGE_KEY) ?? '{}').selectedDeckRecipeId; + const createRepository = () => ({ getAllFromSnapshot: vi.fn().mockResolvedValue([]), @@ -17,9 +22,9 @@ describe('useGameSetup', () => { // These recipes share the same apiParams, so the snapshot can be reused const [firstRecipe, secondRecipe] = ETO_RECIPES; - const render = (repository: ProtopediaInMemoryRepository) => + const render = (repository: ProtopediaInMemoryRepository | null) => renderHook( - (props: { repository: ProtopediaInMemoryRepository }) => + (props: { repository: ProtopediaInMemoryRepository | null }) => useGameSetup({ ...props, onGameStateCreated: vi.fn() }), { initialProps: { repository } }, ); @@ -56,4 +61,27 @@ describe('useGameSetup', () => { ); expect(replaced.getAllFromSnapshot).not.toHaveBeenCalled(); }); + + it('restores the saved deck once the repository is available', async () => { + sessionStorage.setItem( + STORAGE_KEY, + JSON.stringify({ selectedDeckRecipeId: firstRecipe.id }), + ); + const repository = createRepository(); + const { result, rerender } = render(null); + + await act(async () => {}); + expect(savedDeckRecipeId()).toBe(firstRecipe.id); + + await act(async () => rerender({ repository })); + + await waitFor(() => + expect(result.current.selectedDeckRecipe?.id).toBe(firstRecipe.id), + ); + expect(DeckManager.getPrototypesFromRecipe).toHaveBeenCalledWith( + firstRecipe, + repository, + ); + expect(savedDeckRecipeId()).toBe(firstRecipe.id); + }); }); diff --git a/src/hooks/use-game-setup.ts b/src/hooks/use-game-setup.ts index 7a35586..3f05b48 100644 --- a/src/hooks/use-game-setup.ts +++ b/src/hooks/use-game-setup.ts @@ -537,7 +537,11 @@ export function useGameSetup({ // Save state to sessionStorage whenever it changes useEffect(() => { const state: SavedSetupState = { - selectedDeckRecipeId: selectedDeckRecipe?.id || null, + // Until the repository is ready the deck cannot be restored, so keep the + // saved id for the restore effect to read + selectedDeckRecipeId: + selectedDeckRecipe?.id || + (repository ? null : (loadSetupState()?.selectedDeckRecipeId ?? null)), selectedStackRecipeId: selectedStackRecipe?.id || null, selectedPlayerIds, selectedPlayMode, @@ -545,6 +549,7 @@ export function useGameSetup({ }; saveSetupState(state); }, [ + repository, selectedDeckRecipe, selectedStackRecipe, selectedPlayerIds,