Files
Genarrative/src/components/platform-entry/usePlatformProfileCenterController.test.tsx
T
menghao 62e0fe94ef
Project CI / Repository checks (push) Successful in 1m2s
Project CI / Frontend tests (push) Successful in 3m5s
Project CI / Backend tests (push) Successful in 3m41s
Project CI / Native shell tests (push) Successful in 13m7s
资源卡依赖关系及类型分类预览 (#129)
完成资源卡按类型和按依赖分类展现的功能

Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/129
Co-authored-by: menghao <mh18530625731@163.com>
Co-committed-by: menghao <mh18530625731@163.com>
2026-08-05 19:15:46 +08:00

83 lines
2.5 KiB
TypeScript

/* @vitest-environment jsdom */
import { act, renderHook } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { ProfileRechargeCenterResponse } from '../../../packages/shared/src/contracts/runtime';
import { usePlatformProfileCenterController } from './usePlatformProfileCenterController';
const getPlatformProfileRechargeCenterMock = vi.hoisted(() => vi.fn());
vi.mock('../../services/platform-entry/platformProfileClient', async () => {
const actual = await vi.importActual<
typeof import('../../services/platform-entry/platformProfileClient')
>('../../services/platform-entry/platformProfileClient');
return {
...actual,
getPlatformProfileRechargeCenter: getPlatformProfileRechargeCenterMock,
};
});
const rechargeCenter: ProfileRechargeCenterResponse = {
walletBalance: 100,
mudPointBalance: {
totalPoints: 100,
permanentPoints: 100,
limitedPoints: 0,
limitedExpiresAt: null,
dailyFreePoints: 0,
dailyFreeResetPoints: 0,
dailyFreeResetsAt: null,
},
membership: {
status: 'normal',
tier: 'normal',
startedAt: null,
expiresAt: null,
updatedAt: null,
cycleStartedAt: null,
cycleResetsAt: null,
cycleGrantedPoints: 0,
cycleRemainingPoints: 0,
cyclePeriodDays: 30,
},
pointProducts: [],
membershipProducts: [],
benefits: [],
latestOrder: null,
hasPointsRecharged: false,
};
describe('usePlatformProfileCenterController', () => {
afterEach(() => {
getPlatformProfileRechargeCenterMock.mockReset();
});
it('aborts an unfinished recharge center read when the consumer unmounts', async () => {
let resolveRechargeCenter!: (center: ProfileRechargeCenterResponse) => void;
const pendingRead = new Promise<ProfileRechargeCenterResponse>((resolve) => {
resolveRechargeCenter = resolve;
});
getPlatformProfileRechargeCenterMock.mockReturnValue(pendingRead);
const { result, unmount } = renderHook(() =>
usePlatformProfileCenterController({
activeTab: 'editor-canvas',
isAuthenticated: false,
showRechargeEntry: true,
requestLogin: vi.fn(),
currentUser: null,
}),
);
act(() => result.current.loadRechargeCenter());
const requestOptions = getPlatformProfileRechargeCenterMock.mock.calls[0]?.[0];
expect(requestOptions?.signal.aborted).toBe(false);
unmount();
expect(requestOptions?.signal.aborted).toBe(true);
resolveRechargeCenter(rechargeCenter);
await pendingRead;
});
});