feat(learning): replace courses with project catalog
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { MainLayout } from '@/components/layout/MainLayout';
|
||||
@@ -7,18 +7,7 @@ import { useProjectConfigStore } from '@/stores/project-config';
|
||||
import { useSettingsStore } from '@/stores/settings';
|
||||
import { createProjectConfig } from '../../shared/project-config';
|
||||
|
||||
const useCurrentUserProfileMock = vi.hoisted(() => vi.fn());
|
||||
const learningIpcMock = vi.hoisted(() => vi.fn());
|
||||
const syncProfileNowMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock('@/hooks/use-current-user-profile', () => ({
|
||||
useCurrentUserProfile: useCurrentUserProfileMock,
|
||||
}));
|
||||
vi.mock('@/components/profile/UserProfileDialog', () => ({
|
||||
UserProfileDialog: ({ open, required }: { open: boolean; required?: boolean }) => open ? (
|
||||
<div data-testid="profile-dialog-stub" data-required={required ? 'true' : 'false'} />
|
||||
) : null,
|
||||
}));
|
||||
|
||||
vi.mock('@/components/layout/Sidebar', () => ({
|
||||
Sidebar: ({ sidebarCollapsedOverride }: { sidebarCollapsedOverride?: boolean }) => (
|
||||
@@ -57,14 +46,6 @@ function renderLayout(path: string) {
|
||||
describe('MainLayout module isolation', () => {
|
||||
beforeEach(() => {
|
||||
learningIpcMock.mockReset();
|
||||
syncProfileNowMock.mockReset();
|
||||
useCurrentUserProfileMock.mockReset();
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'ready', error: null },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
const project = {
|
||||
id: 'local-project',
|
||||
path: '/tmp/local-project',
|
||||
@@ -89,20 +70,6 @@ describe('MainLayout module isolation', () => {
|
||||
expect(screen.queryByTestId('project-initialization-gate')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not apply a stale profile sync error gate outside Learning', () => {
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'error', error: '同步失败' },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
|
||||
renderLayout('/image-canvas');
|
||||
|
||||
expect(screen.queryByTestId('learning-profile-sync-error-gate')).not.toBeInTheDocument();
|
||||
expect(screen.getByTestId('route-content')).toBeVisible();
|
||||
});
|
||||
|
||||
it('uses the full-height workspace shell for AI painting', () => {
|
||||
renderLayout('/image-canvas');
|
||||
|
||||
@@ -144,130 +111,16 @@ describe('MainLayout module isolation', () => {
|
||||
expect(screen.getByTestId('main-content')).toHaveClass('basis-0', 'overflow-hidden', 'p-0', 'sm:p-6');
|
||||
});
|
||||
|
||||
it('gives the production learning Stage the full classroom width', () => {
|
||||
renderLayout('/learning/course/course-1');
|
||||
it.each(['/learning', '/learning/project/project-1'])(
|
||||
'keeps the project catalog route %s in the standard integrated shell',
|
||||
(path) => {
|
||||
renderLayout(path);
|
||||
|
||||
expect(screen.queryByTestId('sidebar-stub')).not.toBeInTheDocument();
|
||||
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-integrated', 'false');
|
||||
expect(screen.getByTestId('main-content')).toHaveClass('basis-0', 'h-full', 'overflow-hidden', 'p-0');
|
||||
expect(screen.getByTestId('route-content')).toBeVisible();
|
||||
expect(useCurrentUserProfileMock).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it('keeps the required personal-profile gate on direct learning course links', async () => {
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: true,
|
||||
profileSyncState: { status: 'ready', error: null },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
|
||||
renderLayout('/learning/course/course-1');
|
||||
|
||||
expect(await screen.findByTestId('profile-dialog-stub')).toHaveAttribute('data-required', 'true');
|
||||
expect(screen.queryByTestId('sidebar-stub')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('route-content')).not.toBeInTheDocument();
|
||||
expect(learningIpcMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not mount a direct learning course while profile sync is loading', () => {
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'loading', error: null },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
|
||||
renderLayout('/learning/course/course-1');
|
||||
|
||||
expect(screen.queryByTestId('route-content')).not.toBeInTheDocument();
|
||||
expect(learningIpcMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('mounts the current learning deep link only after profile sync and required fields are complete', () => {
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'loading', error: null },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
const view = renderLayout('/learning/course/course-1');
|
||||
|
||||
expect(learningIpcMock).not.toHaveBeenCalled();
|
||||
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'ready', error: null },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
view.rerender(
|
||||
<MemoryRouter initialEntries={['/learning/course/course-1']}>
|
||||
<Routes>
|
||||
<Route element={<MainLayout />}>
|
||||
<Route path="*" element={<LearningRouteContent />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('route-content')).toBeVisible();
|
||||
expect(learningIpcMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps a failed profile sync visible and mounts the deep link only after retry succeeds', async () => {
|
||||
syncProfileNowMock.mockResolvedValue(null);
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'error', error: '同步失败' },
|
||||
profileSyncError: '同步失败',
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
const view = renderLayout('/learning/course/course-1');
|
||||
|
||||
expect(screen.getByRole('alert')).toHaveTextContent('同步失败');
|
||||
expect(screen.getByRole('button', { name: '返回课程库' })).toBeVisible();
|
||||
expect(learningIpcMock).not.toHaveBeenCalled();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: '重新同步' }));
|
||||
await waitFor(() => expect(syncProfileNowMock).toHaveBeenCalledTimes(1));
|
||||
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'ready', error: null },
|
||||
profileSyncError: null,
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
view.rerender(
|
||||
<MemoryRouter initialEntries={['/learning/course/course-1']}>
|
||||
<Routes>
|
||||
<Route element={<MainLayout />}>
|
||||
<Route path="*" element={<LearningRouteContent />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
|
||||
expect(screen.queryByTestId('learning-profile-sync-error-gate')).not.toBeInTheDocument();
|
||||
expect(screen.getByTestId('route-content')).toBeVisible();
|
||||
expect(learningIpcMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps the error gate and deep link unmounted when profile retry fails', async () => {
|
||||
syncProfileNowMock.mockRejectedValue(new Error('仍然失败'));
|
||||
useCurrentUserProfileMock.mockReturnValue({
|
||||
profileRequired: false,
|
||||
profileSyncState: { status: 'error', error: '同步失败' },
|
||||
profileSyncError: '同步失败',
|
||||
syncProfileNow: syncProfileNowMock,
|
||||
});
|
||||
renderLayout('/learning/course/course-1');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: '重新同步' }));
|
||||
|
||||
await waitFor(() => expect(syncProfileNowMock).toHaveBeenCalledTimes(1));
|
||||
expect(screen.getByTestId('learning-profile-sync-error-gate')).toBeVisible();
|
||||
expect(screen.getByRole('alert')).toHaveTextContent('同步失败');
|
||||
expect(learningIpcMock).not.toHaveBeenCalled();
|
||||
});
|
||||
expect(screen.getByTestId('sidebar-stub')).toBeVisible();
|
||||
expect(screen.getByTestId('titlebar-stub')).toHaveAttribute('data-integrated', 'true');
|
||||
expect(screen.getByTestId('main-content')).toHaveClass('p-5', 'sm:p-6');
|
||||
expect(screen.getByTestId('route-content')).toBeVisible();
|
||||
expect(screen.queryByTestId('learning-profile-sync-error-gate')).not.toBeInTheDocument();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user