Skip to content

test(content): wait for the dialog's first focus before opening the JSON-LD code - #1753

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/before-publish-code-focus
Oct 29, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/before-publish-code-focus

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

_before-publish.test.tsx › "opens and folds the code with one button, its focus on the code it opens" failed on main whenever the machine was slow. It went red on three branches that don't touch the editor, and once even run alone.

Root cause: a race in the test, not a product bug.

  1. When the dialog opens, Base UI moves the first focus to "Continue to send" one frame later (requestAnimationFrame, about 16.7 ms under jsdom).
  2. The test clicked "Show the JSON-LD" right after the dialog opened, and the component's effect focused the <pre>.
  3. If more than a frame passed between opening and that focus, which happens under memory pressure, the deferred first focus landed afterwards. It took the focus back to the button, so the test got expected <button> to be <pre>.

The fix is one line in the test: wait for the dialog's first focus before clicking, as the Export test in the same file already does:

await vi.waitFor(() => expect(document.activeElement).toBe(button('Continue to send')));

The assertion that matters (focus lands on the opened code) is unchanged, and the component is untouched. In a browser nobody can click "Show" within the frame before the first focus settles.

How it was checked

  • A temporary 20 ms busy wait inside the click reproduced the slow machine. Before the fix: 3/3 failed with the same error. After: 3/3 passed. The busy wait was removed afterwards.
  • The file was also run under a fake Date at six dates and times (37/37 each), which rules out a dependency on today's date.

Related issue

None (flaky test on main).

Type of change

  • test — Tests only

How to test

cd web && yarn vitest run 'src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.test.tsx': 37/37 pass, also while the machine is busy.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: format, lint (0 errors, 7 warnings), typecheck, 555 test files / 5,315 tests and build pass

🤖 Generated with Claude Code

…SON-LD code

The Before you publish test "opens and folds the code with one button" clicked
Show the JSON-LD straight after the dialog rendered. Base UI's focus manager
moves the dialog's initial focus (initialFocus = Continue to send) with
requestAnimationFrame, which jsdom runs on a ~16.7 ms setInterval. Async act()
hands over to setImmediate after flushing, so a timer that came due meanwhile
runs there. When the run was slow (memory pressure, swap) and more than one
frame passed between the dialog opening and the code's focus effect, that
deferred initial focus landed after the effect and took the focus back to the
button: "expected <button> to be <pre>". Fast runs passed, so it failed or
passed by machine load, not by date (the file passes with Date faked across
several dates and times).

Not a product bug: in a browser the initial focus lands a frame after the
dialog opens, before anyone can click Show. The test now waits for that focus,
as the Export test in the same file does, and still expects the focus on the
code it opens. Reproduced with a 20 ms busy wait in the click (3/3 failed
before, 3/3 passed after).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Reviewed 724f0d1, branched from ce9a3dc. One test file, no product code.

What I checked

  • The race is as described. I put a 25 ms busy wait right before the click on "Show the JSON-LD". Without this line, the test fails with expected <button> to be <pre>, the same error seen on main. With it, the test passes. The wait is the one the Export test in the same file already uses.
  • The assertion that matters is unchanged: focus lands on the code it opens.
  • The file passes (37/37) on this branch merged with main. Prettier is clean.
  • It merges cleanly on current main.

@gkhngyk
gkhngyk merged commit 9396316 into ansvisor:main Oct 29, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants