admin: a withdrawal that went through is reported even when the state re-read fails

This commit is contained in:
Maurice
2026-10-30 23:14:32 +00:00
parent 314b087f98
commit dc76efc418
2 changed files with 33 additions and 1 deletions
@@ -0,0 +1,31 @@
// FE-ADMIN-CONTRIBHOOK-001
import { act, renderHook, waitFor } from '@testing-library/react';
import type { PlaceContribState } from '@trek/shared';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { placeContribRepo } from '../../repo/placeContribRepo';
import { usePlaceContrib } from './usePlaceContrib';
const STATE = { ineligibleReason: null, active: true } as unknown as PlaceContribState;
afterEach(() => {
vi.restoreAllMocks();
});
describe('usePlaceContrib', () => {
it('FE-ADMIN-CONTRIBHOOK-001: a withdrawal that went through is answered even when the state cannot be read again', async () => {
const state = vi.spyOn(placeContribRepo, 'state').mockResolvedValueOnce(STATE);
vi.spyOn(placeContribRepo, 'withdraw').mockResolvedValue({ withdrawn: { website: 2 }, cleared: 1 });
const { result } = renderHook(() => usePlaceContrib());
await waitFor(() => expect(result.current.state).toBe(STATE));
state.mockRejectedValueOnce(new Error('offline'));
let answer: unknown;
await act(async () => {
answer = await result.current.withdraw();
});
expect(answer).toEqual({ withdrawn: { website: 2 }, cleared: 1 });
expect(result.current.state).toBe(STATE);
expect(result.current.busy).toBe(false);
});
});
@@ -53,7 +53,8 @@ export function usePlaceContrib() {
setBusy(true);
try {
const result = await placeContribRepo.withdraw();
setState(await placeContribRepo.state());
// The withdrawal went through; a failed re-read keeps the old state rather than reporting it failed.
await placeContribRepo.state().then(setState, () => undefined);
return result;
} finally {
setBusy(false);