Skip to content

Commit ee99bc2

Browse files
test(mosaic): fold guard tests into the usePendingAction suite
1 parent f0bc48b commit ee99bc2

2 files changed

Lines changed: 133 additions & 135 deletions

File tree

‎packages/mosaic/src/hooks/__tests__/use-pending-action-shared.test.tsx‎

Lines changed: 0 additions & 135 deletions
This file was deleted.

‎packages/mosaic/src/hooks/__tests__/use-pending-action.test.tsx‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ import { describe, expect, it, vi } from 'vitest';
55

66
import { deferred } from '../../__tests__/async';
77
import { clerkApiError } from '../../__tests__/clerk-errors';
8+
import { useConfirmController } from '../../blocks/confirm';
89
import { MosaicProvider } from '../../mosaic-provider';
10+
import { SaveError } from '../../utils/errors';
911
import { usePendingAction } from '../use-pending-action';
1012

1113
function blocked() {
1214
return clerkApiError('action_blocked', 'Raw server sentence.');
1315
}
1416

17+
function useGuardedConfirm(onConfirm: () => Promise<void>) {
18+
const actions = usePendingAction();
19+
const remove = useConfirmController({
20+
onConfirm,
21+
reverification: { status: 'loading', visible: true, onCancel: vi.fn() },
22+
});
23+
return { actions, remove: actions.guard(remove) };
24+
}
25+
1526
describe('usePendingAction', () => {
1627
it('reports success and leaves no error', async () => {
1728
const { result } = renderHook(() => usePendingAction());
@@ -264,3 +275,125 @@ describe('usePendingAction', () => {
264275
expect(result.current.errorKey).toBeUndefined();
265276
});
266277
});
278+
279+
describe('usePendingAction guard', () => {
280+
it('refuses a guarded confirm while a run holds the lock, leaving the dialog unchanged', async () => {
281+
const pending = deferred<void>();
282+
const onConfirm = vi.fn(() => Promise.resolve());
283+
const { result } = renderHook(() => useGuardedConfirm(onConfirm));
284+
act(() => result.current.remove.onOpenChange(true));
285+
const owners = result.current;
286+
287+
act(() => {
288+
void owners.actions.run('row', () => pending.promise);
289+
void owners.remove.confirm();
290+
});
291+
292+
expect(onConfirm).not.toHaveBeenCalled();
293+
expect(result.current.remove.isOpen).toBe(true);
294+
expect(result.current.remove.isPending).toBe(false);
295+
expect(result.current.actions.isPending).toBe(true);
296+
await act(() => pending.resolve());
297+
expect(result.current.actions.isPending).toBe(false);
298+
await act(() => result.current.remove.confirm());
299+
expect(onConfirm).toHaveBeenCalledOnce();
300+
expect(result.current.remove.isOpen).toBe(false);
301+
});
302+
303+
it('refuses a run while a guarded confirm holds the lock, leaving its key and error unset', async () => {
304+
const pending = deferred<void>();
305+
const direct = vi.fn();
306+
const { result } = renderHook(() => useGuardedConfirm(() => pending.promise));
307+
act(() => result.current.remove.onOpenChange(true));
308+
const owners = result.current;
309+
310+
await act(async () => {
311+
void owners.remove.confirm();
312+
expect(await owners.actions.run('row', direct)).toBe(false);
313+
});
314+
315+
expect(direct).not.toHaveBeenCalled();
316+
expect(result.current.actions.lastKey).toBeUndefined();
317+
expect(result.current.actions.error).toBeUndefined();
318+
expect(result.current.actions.isPending).toBe(true);
319+
await act(() => pending.resolve());
320+
await act(async () => expect(await owners.actions.run('row', direct)).toBe(true));
321+
expect(direct).toHaveBeenCalledOnce();
322+
});
323+
324+
it('keeps the dialog error when a run refuses its retry', async () => {
325+
const pending = deferred<void>();
326+
const failure = new SaveError({ global: { code: 'action_blocked', message: 'Confirm refused.' } });
327+
const onConfirm = vi.fn(() => Promise.reject(failure));
328+
const { result } = renderHook(() => useGuardedConfirm(onConfirm));
329+
act(() => result.current.remove.onOpenChange(true));
330+
await act(() => result.current.remove.confirm());
331+
const error = result.current.remove.errorMessage;
332+
expect(error).toBeDefined();
333+
expect(result.current.remove.isOpen).toBe(true);
334+
expect(result.current.actions.error).toBeUndefined();
335+
336+
act(() => {
337+
void result.current.actions.run('row', () => pending.promise);
338+
void result.current.remove.confirm();
339+
});
340+
341+
expect(onConfirm).toHaveBeenCalledOnce();
342+
expect(result.current.remove.errorMessage).toBe(error);
343+
expect(result.current.remove.isOpen).toBe(true);
344+
expect(result.current.remove.isPending).toBe(false);
345+
await act(() => pending.resolve());
346+
});
347+
348+
it('keeps the last key and clears the run error while a guarded confirm runs', async () => {
349+
const pending = deferred<void>();
350+
const failure = new SaveError({ global: { code: 'action_blocked', message: 'Run refused.' } });
351+
const { result } = renderHook(() => useGuardedConfirm(() => pending.promise));
352+
await act(() => result.current.actions.run('row', () => Promise.reject(failure)));
353+
expect(result.current.actions.error).toBeDefined();
354+
355+
act(() => result.current.remove.onOpenChange(true));
356+
act(() => {
357+
void result.current.remove.confirm();
358+
});
359+
360+
expect(result.current.actions.isPending).toBe(true);
361+
expect(result.current.actions.pendingKey).toBeUndefined();
362+
expect(result.current.actions.lastKey).toBe('row');
363+
expect(result.current.actions.errorKey).toBeUndefined();
364+
expect(result.current.actions.error).toBeUndefined();
365+
await act(() => pending.resolve());
366+
expect(result.current.actions.isPending).toBe(false);
367+
expect(result.current.actions.lastKey).toBe('row');
368+
});
369+
370+
it.each(['resolve', 'reject'])(
371+
'holds the lock after the dialog is cancelled until its action promises %s',
372+
async outcome => {
373+
const pending = deferred<void>();
374+
const direct = vi.fn();
375+
const { result } = renderHook(() => useGuardedConfirm(() => pending.promise));
376+
act(() => result.current.remove.onOpenChange(true));
377+
act(() => {
378+
void result.current.remove.confirm();
379+
});
380+
act(() => result.current.remove.onOpenChange(false));
381+
expect(result.current.remove.isOpen).toBe(false);
382+
await act(async () => expect(await result.current.actions.run('row', direct)).toBe(false));
383+
expect(direct).not.toHaveBeenCalled();
384+
expect(result.current.actions.isPending).toBe(true);
385+
expect(result.current.actions.pendingKey).toBeUndefined();
386+
387+
await act(() => {
388+
if (outcome === 'reject') {
389+
pending.reject(new Error('Late confirm failure'));
390+
} else {
391+
pending.resolve();
392+
}
393+
});
394+
expect(result.current.actions.isPending).toBe(false);
395+
expect(result.current.remove.errorMessage).toBeUndefined();
396+
await act(async () => expect(await result.current.actions.run('row', direct)).toBe(true));
397+
},
398+
);
399+
});

0 commit comments

Comments
 (0)