Skip to content

test(modal): wait for Radix's focus restore before asserting it #475

Description

@Ardnived

Summary

src/components/atoms/Modal/Modal.focus-opener.test.tsx fails about one run in four on main (a5f3b29): 3 of 12 runs alone, and 2 of 4 runs of the full lane. The failure is expected undefined to be <button id="elsewhere">, meaning no focus() call had been made when the assertion ran. That reddens the lean gate and CI's test:run on any PR at random.

The cause is timing in the spec, not the hook. Radix's FocusScope returns focus on unmount inside a setTimeout(…, 0) (node_modules/@radix-ui/react-focus-scope/dist/index.mjs:132, v1.3.1). That timer dispatches the unmount auto-focus event, which reaches useOpenerRestore's restoreFocus and its opening.focus() (src/hooks/use-opener-restore.ts:113). The spec's close() awaits one act(), which flushes React work and microtasks but not a macrotask, so the assertion sometimes runs before the timer fires.

Approach

  • Modal.focus-opener.test.tsx: make close() (lines 101–109) also flush one macrotask before it returns, for example await act(() => new Promise((resolve) => setTimeout(resolve, 0))). An alternative is to assert with vi.waitFor(() => expect(asked.at(-1)).toBe(elsewhere)). Measured locally, the first form took the spec from 3 of 12 failing to 0 of 15.
  • Modal.focus.test.tsx: apply the same fix. It has the same shape: await close(host) and then an immediate expect(asked.at(-1)).toBe(opener) (lines 127–129), so it races the same timer.
  • Docblock: re-measure the "one dialog per FILE" rule in Modal.focus.test.tsx's docblock once the timer is awaited. It blames cross-case teardown for failures of one in five and one in eight. That may have been this same timer. If several cases in one file now pass repeatedly, correct the docblock. The caller-wins case that Modal.focus-opener.test.tsx notes as missing (lines 125–129) could then live beside the others. If they still fail, leave the rule and its measurement as they are.

Acceptance criteria

  • Modal.focus-opener.test.tsx and Modal.focus.test.tsx wait for Radix's restore timer before asserting the focus call.
  • Each spec passes 20 consecutive runs on its own and 5 consecutive full-lane runs.
  • Each spec still fails when the opening.focus() call in useOpenerRestore.restoreFocus is removed. This checks the wait has not made either spec vacuous.
  • The "one dialog per FILE" docblock matches what the re-measurement shows.

Verification checklist

  • for i in $(seq 20); do pnpm test:run src/components/atoms/Modal/Modal.focus-opener.test.tsx src/components/atoms/Modal/Modal.focus.test.tsx || break; done finishes with no failure.
  • pnpm test:run passes 5 times in a row.
  • With opening.focus() commented out in src/hooks/use-opener-restore.ts, both specs fail. Restore it afterwards.
  • .claude/skills/pr-prep/check.sh passes.

Notes

Activity

  1. added theissue type on Nov 24, 2026
  2. Ardnived commented on Nov 24, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot implement

  3. added
    bot:working
    on Nov 24, 2026
  4. sydevs-dispatcher commented on Nov 24, 2026

    @sydevs-dispatcher
    Contributor

    🤖 no session running
    approved by Ardnived

    recent dispatches
    • 18:18Z implement · attempt 1 · session · done
    • 18:18Z implement · attempt 1 · session

    Written by the sydevs dispatcher. Sessions never edit this comment.

  5. removed
    bot:working
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    Priority

    High

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions