Files
Genarrative/src/components/rpg-entry/useRpgEntryBootstrap.test.tsx
T
kdletters 387a1c26e3 完成全量检查整改
清理已跟踪原始日志与个人本地配置
修复前后端有效门禁、测试和构建问题
补齐生产 Jenkins、SpacetimeDB 本地命令与文档约束
收紧图片编辑器状态、附件与媒体引用测试
排除已下线旧创作入口测试并清理 warning
2026-07-17 16:56:46 +08:00

94 lines
2.9 KiB
TypeScript

/* @vitest-environment jsdom */
import { renderHook, waitFor } from '@testing-library/react';
import { beforeEach, expect, test, vi } from 'vitest';
import type { AuthUser } from '../../services/authService';
import { listRpgCreationWorks } from '../../services/rpg-creation/index';
import {
listRpgEntryWorldGallery,
listRpgEntryWorldLibrary,
listRpgProfileBrowseHistory,
listRpgProfileSaveArchives,
} from '../../services/rpg-entry';
import { useRpgEntryBootstrap } from './useRpgEntryBootstrap';
vi.mock('../../services/rpg-entry', () => ({
listRpgEntryWorldGallery: vi.fn(),
listRpgEntryWorldLibrary: vi.fn(),
listRpgProfileBrowseHistory: vi.fn(),
listRpgProfileSaveArchives: vi.fn(),
resumeRpgProfileSaveArchive: vi.fn(),
upsertRpgProfileBrowseHistory: vi.fn(),
}));
vi.mock('../../services/rpg-creation/index', () => ({
listRpgCreationWorks: vi.fn(),
}));
const mockListRpgEntryWorldGallery = vi.mocked(listRpgEntryWorldGallery);
const mockListRpgEntryWorldLibrary = vi.mocked(listRpgEntryWorldLibrary);
const mockListRpgCreationWorks = vi.mocked(listRpgCreationWorks);
const mockListRpgProfileBrowseHistory = vi.mocked(listRpgProfileBrowseHistory);
const mockListRpgProfileSaveArchives = vi.mocked(listRpgProfileSaveArchives);
const testUser: AuthUser = {
id: 'user-1',
publicUserCode: '100001',
displayName: 'Test User',
avatarUrl: null,
phoneNumberMasked: null,
loginMethod: 'password',
bindingStatus: 'active',
wechatBound: false,
};
beforeEach(() => {
vi.clearAllMocks();
mockListRpgEntryWorldGallery.mockResolvedValue([]);
mockListRpgEntryWorldLibrary.mockResolvedValue([]);
mockListRpgCreationWorks.mockResolvedValue([]);
mockListRpgProfileBrowseHistory.mockResolvedValue([]);
mockListRpgProfileSaveArchives.mockResolvedValue([]);
});
test('login refreshes platform data without showing page loading again', async () => {
const getProfileDashboard = vi.fn(async () => null);
let resolveInitialGallery: (value: []) => void = () => undefined;
mockListRpgEntryWorldGallery.mockReturnValueOnce(
new Promise<[]>((resolve) => {
resolveInitialGallery = resolve;
}),
);
const { result, rerender } = renderHook(
({ user }: { user: AuthUser | null }) =>
useRpgEntryBootstrap({
user,
canAccessProtectedData: Boolean(user),
getProfileDashboard,
handleContinueGame: vi.fn(),
hasInitialAgentSession: false,
}),
{ initialProps: { user: null as AuthUser | null } },
);
await waitFor(() => {
expect(result.current.isLoadingPlatform).toBe(true);
});
resolveInitialGallery([]);
await waitFor(() => {
expect(result.current.isLoadingPlatform).toBe(false);
});
rerender({ user: testUser });
await waitFor(() => {
expect(mockListRpgEntryWorldLibrary).toHaveBeenCalledTimes(1);
});
expect(result.current.isLoadingPlatform).toBe(false);
expect(result.current.isLoadingDashboard).toBe(false);
});