Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 4c212b9

Browse files
test(console): wait on the leave guard itself after a settings save, not on the bar (#13322)
Fixes #13317 ## The race The case "after a successful save the page is clean again: no prompt" in `SettingsView.leaveGuard-13163.test.tsx` waited for the unsaved bar (`settings-unsaved-count`) to go and then asserted, synchronously, that a `beforeunload` is not held. Those are two different moments: - the bar goes in the **commit** that renders `setDraft({})` after the save resolves; - the guard goes one step later, in the **passive effect** cleanup of `useRegisterUnsavedEdits` (app-shell's unsaved-edits seam), which removes the `beforeunload` and capture-phase `click` listeners. The save settles after an `await`, outside `act()`, so the update renders on the default lane and react-dom 19.2.8 hands the passive flush to the scheduler as a task of its own (only sync-lane commits flush passive effects inline). When the commit task overruns the scheduler's 5ms frame budget, the scheduler yields, and RTL's `waitFor` (whose wrapper ends on a `setTimeout(0)`) can return before that task runs. The assertion then reads the still-registered guard: `expected true to be false`, the CI signature. Product code is not changed: the release is one task after the bar disappears, before any paint a user could react to. What was wrong is the test, which waited on a stand-in for the thing it asserts. The fix waits on the guard itself (a `waitFor` around the `beforeunload` check), the same shape `discardGuard.test.tsx` in `plugin-form` already uses for its dirty case. The link listener is removed in the same uninstall, so the click assertion that follows needs no wait of its own. ## Measured PM mechanism assumption 1 holds, with one correction: the draft reset is not late. The bar goes on time, and what lands later is the guard's release, in a passive effect. Assumption 2 holds: the diff from the base commit touches neither file, and the race is the test's own. - **The gap, on an unloaded run (probe, not committed):** a MutationObserver that fires when the bar leaves the DOM read the guard **still held 20/20**. Right after `waitFor` returned it was released 20/20, which is why the file passes locally (5/5 reruns on the card, 1/1 here at the base commit, 12/12 tests). - **Same probe with a slow machine modelled** (`performance.now` skewed +10ms per read, so React's scheduler yields after every task): right after `waitFor`, the guard was **still held 13/20**. - **The real case, before and after, under that same load** (uncommitted copies of the file with the case repeated 20 times): - before (base commit): `Tests 10 failed | 21 passed (31)`, every failure `AssertionError: expected true to be false` at `expect(unload.defaultPrevented).toBe(false)` in that case; the file's other 11 cases are green; - after (this branch): `Tests 31 passed (31)`. - **Ablation** (a mutation, run once and restored): with `useUnsavedLeaveGuard` registering unconditionally (`useRegisterUnsavedEdits(true, message)`), the fixed case goes red inside the new wait (`Tests 1 failed | 11 skipped`, `expected true to be false`). So the wait does not pass when the guard is never released. The mutation was applied through `ablation-replace.mjs`: anchor 1 to 0, blob `42a45f17ddd5` to `97ee57239286`, then restored to the HEAD blob with `git diff HEAD` empty. ## Verification (head `42cc11c`) - `pnpm exec vitest run apps/console/`: `Test Files 236 passed (236)`, `Tests 2408 passed (2408)`. - `node scripts/check-changeset-presence.mjs` 0 · `node scripts/check-changeset-no-major.mjs` 0 · `pnpm check:new-line-citations` 0 · `pnpm check:control-bytes` 0 · `pnpm check:test-path-roots` 0 · `pnpm check:changeset-claims` 0 · `pnpm check:pending-changeset-literals` 0 · `eslint` on the changed file (console config) 0. - NOT MEASURED locally: `type-check` for `@object-ui/console` exits 2 in this worktree because the workspace packages' `dist` is not built. Every error is module resolution or cascades from it, and the only error reported in the changed file is on its existing `@object-ui/i18n` import, a line this diff does not touch. CI's `type-check` and repository-wide `pnpm lint` decide these. The changeset `.changeset/13317-settings-leaveguard-race.md` has an empty frontmatter: test-only, releases nothing (AGENTS.md section 9). ## Acceptance notes - I surveyed the other suites that assert a `beforeunload` (`git grep -ln beforeunload` over tests). None of them checks a guard that is released after an awaited save while waiting on a DOM stand-in; their clean checks follow `act`-wrapped events or a pristine mount. No other instance of this shape was found. Session: `https://claude.ai/code/session_019Bd3muSBdHUFdJiKqnsM2W` --- _Generated by [Claude Code](https://claude.ai/code/session_019Bd3muSBdHUFdJiKqnsM2W)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent b8fa223 commit 4c212b9

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
Test-only change to the settings page's leave-guard suite: the case "after a successful save the page is clean again" now waits on the guard itself rather than on the unsaved bar. No published behaviour changes.

‎apps/console/src/pages/settings/__tests__/SettingsView.leaveGuard-13163.test.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,18 @@ describe('CONTROLS: no prompt where nothing would be lost (objectui#13163)', ()
266266
fireEvent.click(screen.getByRole('button', { name: /save changes/i }));
267267
await waitFor(() => expect(screen.queryByTestId('settings-unsaved-count')).toBeNull());
268268

269-
const unload = new Event('beforeunload', { cancelable: true });
270-
window.dispatchEvent(unload);
271-
expect(unload.defaultPrevented).toBe(false);
269+
// The bar goes in the commit that clears the draft; the guard goes a step
270+
// later, in the passive effect that releases the page's registration
271+
// (`useRegisterUnsavedEdits`). The save settles outside `act`, so React
272+
// runs that effect as a task of its own, and on a loaded machine the wait
273+
// above can return in between (objectui#13317). So wait on the guard
274+
// itself, the fact this case asserts. The link listener is removed in the
275+
// same step, so once a tab close is not held, the click below meets none.
276+
await waitFor(() => {
277+
const unload = new Event('beforeunload', { cancelable: true });
278+
window.dispatchEvent(unload);
279+
expect(unload.defaultPrevented).toBe(false);
280+
});
272281

273282
fireEvent.click(link('Business Units'));
274283
expect(await screen.findByText('Business Units page')).toBeTruthy();

0 commit comments

Comments
 (0)