/* @vitest-environment jsdom */
import { act, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { PlatformGameLoadingSurface } from './PlatformGameLoadingSurface';
import {
PLATFORM_GAME_LOADING_ASSET_PHASE_MS,
PLATFORM_GAME_LOADING_ENGINE_PHASE_MS,
PLATFORM_GAME_LOADING_SLOW_PHASE_MS,
resolvePlatformGameLoadingMessage,
} from './platformGameLoadingSurfaceModel';
function readMessage() {
return screen
.getByRole('status')
.querySelector('.platform-game-loading__message')?.textContent;
}
afterEach(() => {
vi.useRealTimers();
});
describe('PlatformGameLoadingSurface', () => {
test('阶段文案随等待时长推进,慢加载才追加提示行', () => {
vi.useFakeTimers();
render();
const surface = screen.getByRole('status');
expect(surface.getAttribute('aria-busy')).toBe('true');
expect(surface.getAttribute('aria-label')).toContain('星尘防线');
expect(readMessage()).toBe(resolvePlatformGameLoadingMessage(0));
expect(surface.querySelector('.platform-game-loading__hint')).toBeNull();
act(() => {
vi.advanceTimersByTime(PLATFORM_GAME_LOADING_ASSET_PHASE_MS);
});
expect(readMessage()).toBe(
resolvePlatformGameLoadingMessage(PLATFORM_GAME_LOADING_ASSET_PHASE_MS),
);
expect(
screen.getByRole('status').querySelector('.platform-game-loading__hint'),
).toBeNull();
act(() => {
vi.advanceTimersByTime(
PLATFORM_GAME_LOADING_ENGINE_PHASE_MS -
PLATFORM_GAME_LOADING_ASSET_PHASE_MS,
);
});
expect(readMessage()).toBe(
resolvePlatformGameLoadingMessage(PLATFORM_GAME_LOADING_ENGINE_PHASE_MS),
);
act(() => {
vi.advanceTimersByTime(
PLATFORM_GAME_LOADING_SLOW_PHASE_MS -
PLATFORM_GAME_LOADING_ENGINE_PHASE_MS,
);
});
expect(
screen.getByRole('status').querySelector('.platform-game-loading__hint')
?.textContent,
).toBeTruthy();
});
test('宿主文案与提示覆盖默认阶段文案,封面按宿主给的内容渲染', () => {
render(
}
/>,
);
const surface = screen.getByRole('status');
expect(surface.className).toContain('platform-game-loading--panel');
expect(readMessage()).toBe('正在创建试玩会话…');
expect(
surface.querySelector('.platform-game-loading__hint')?.textContent,
).toBe('会话过期后请重新创建');
expect(screen.getByRole('img', { name: '封面' })).toBeTruthy();
});
test('宿主判定停止推进时不再声明 busy,并停掉进度动画', () => {
render();
const surface = screen.getByRole('status');
expect(surface.getAttribute('aria-busy')).toBe('false');
expect(surface.className).toContain('platform-game-loading--stalled');
});
test('没有封面时渲染图标占位而不是空白区块', () => {
const { container } = render(
,
);
const surface = container.querySelector('.platform-game-loading');
expect(surface?.className).toContain('platform-game-loading--player');
expect(
surface?.querySelector('.platform-game-loading__cover svg'),
).toBeTruthy();
});
});