diff --git a/src/components/platform-entry/usePlatformProfileCenterController.recharge.test.tsx b/src/components/platform-entry/usePlatformProfileCenterController.recharge.test.tsx new file mode 100644 index 000000000..d333e061d --- /dev/null +++ b/src/components/platform-entry/usePlatformProfileCenterController.recharge.test.tsx @@ -0,0 +1,39 @@ +/* @vitest-environment jsdom */ + +import { act, waitFor } from '@testing-library/react'; +import { beforeEach, describe, expect, test, vi } from 'vitest'; + +import { usePlatformWalletStore } from '../../stores/usePlatformWalletStore'; +import { + profileClientMocks, + renderController, + userA, +} from './usePlatformProfileCenterController.testSupport'; + +describe('usePlatformProfileCenterController recharge fallback', () => { + beforeEach(() => { + window.history.replaceState(null, '', '/profile'); + usePlatformWalletStore.getState().resetWalletBalance(); + vi.clearAllMocks(); + }); + + test('keeps the response legacy balance when no shared snapshot exists', async () => { + usePlatformWalletStore.getState().setWalletOwner('user-a'); + profileClientMocks.getPlatformProfileRechargeCenter.mockResolvedValue({ + walletBalance: 37, + membership: null, + pointProducts: [], + membershipProducts: [], + benefits: [], + latestOrder: null, + hasPointsRecharged: false, + }); + const { result } = renderController(userA); + + act(() => result.current.loadRechargeCenter()); + + await waitFor(() => { + expect(result.current.rechargeCenter?.walletBalance).toBe(37); + }); + }); +}); diff --git a/src/components/platform-entry/usePlatformProfileCenterController.testSupport.tsx b/src/components/platform-entry/usePlatformProfileCenterController.testSupport.tsx new file mode 100644 index 000000000..77fc1b13c --- /dev/null +++ b/src/components/platform-entry/usePlatformProfileCenterController.testSupport.tsx @@ -0,0 +1,46 @@ +/* @vitest-environment jsdom */ + +import { renderHook } from '@testing-library/react'; +import { vi } from 'vitest'; + +import type { AuthUser } from '../../services/authService'; + +const profileClientMocks = vi.hoisted(() => ({ + confirmWechatPlatformProfileRechargeOrder: vi.fn(), + createPlatformProfileRechargeOrder: vi.fn(), + getPlatformProfileRechargeCenter: vi.fn(), + getPlatformProfileReferralInviteCenter: vi.fn(), + getPlatformProfileWalletLedger: vi.fn(), + redeemPlatformProfileReferralInviteCode: vi.fn(), + redeemPlatformProfileRewardCode: vi.fn(), + watchWechatPlatformProfileRechargeOrder: vi.fn(), +})); + +vi.mock('../../services/platform-entry/platformProfileClient', () => + profileClientMocks, +); + +export { profileClientMocks }; + +import { usePlatformProfileCenterController } from './usePlatformProfileCenterController'; + +export const userA = { id: 'user-a' } as AuthUser; +export const userB = { id: 'user-b' } as AuthUser; + +export function renderController( + currentUser: AuthUser, + onRechargeSuccess = vi.fn(), +) { + return renderHook( + ({ user }) => + usePlatformProfileCenterController({ + activeTab: 'settings', + isAuthenticated: true, + showRechargeEntry: true, + onRechargeSuccess, + requestLogin: vi.fn(), + currentUser: user, + }), + { initialProps: { user: currentUser } }, + ); +} diff --git a/src/components/platform-entry/usePlatformProfileCenterController.ts b/src/components/platform-entry/usePlatformProfileCenterController.ts index 605e6c30b..4e15f6926 100644 --- a/src/components/platform-entry/usePlatformProfileCenterController.ts +++ b/src/components/platform-entry/usePlatformProfileCenterController.ts @@ -1175,8 +1175,9 @@ export function usePlatformProfileCenterController({ } return { ...rechargeCenter, - walletBalance: mudPointBalance?.totalPoints ?? 0, - mudPointBalance: mudPointBalance ?? undefined, + walletBalance: + mudPointBalance?.totalPoints ?? rechargeCenter.walletBalance, + mudPointBalance: mudPointBalance ?? rechargeCenter.mudPointBalance, }; }, [mudPointBalance, rechargeCenter, walletOwnerMatchesCurrentUser]); diff --git a/vitest.config.ts b/vitest.config.ts index 7672c4752..24b38f2d6 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -65,7 +65,7 @@ export default defineConfig({ 'src/components/platform-entry/PlatformProfileTaskCenterModal.test.tsx', 'src/components/platform-entry/PlatformProfileWalletLedgerModal.test.tsx', 'src/components/platform-entry/platformProfile*.test.ts', - 'src/components/platform-entry/usePlatformProfileCenterController.test.tsx', + 'src/components/platform-entry/usePlatformProfileCenterController*.test.tsx', 'src/hooks/useHostNavigationCanGoBack.test.tsx', 'apps/admin-web/src/**/*.test.ts', 'apps/admin-web/src/**/*.test.tsx',