fix: pr-bug-scan validated finding from #3552 (#3857)

* fix: address pr-bug-scan validated finding from #3552

Clear actionPending in setOverlayRootRef when node attaches, mirroring sibling BrowserMobileDriverOverlay; blocks stale-disabled-button on re-mount.

* test: cover mobile driver overlay pending reset

---------

Co-authored-by: orca-bug-scan-bot <orca-bug-scan-bot@stably.ai>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
buf0-bot[bot]
2026-05-31 02:13:36 -07:00
committed by GitHub
co-authored by orca-bug-scan-bot Neil
parent 2c8bbacca1
commit cb233d9c83
2 changed files with 101 additions and 0 deletions
@@ -0,0 +1,96 @@
import type React from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { MobileDriverOverlay } from './MobileDriverOverlay'
type OverlayProps = {
actionPending: boolean
onAction: () => void | Promise<void>
rootRef: (node: HTMLDivElement | null) => void
}
type OverlayElement = React.ReactElement<OverlayProps>
const hookRuntime = vi.hoisted(() => ({
states: [] as unknown[],
stateIndex: 0,
refs: [] as { current: unknown }[],
refIndex: 0,
id: 0
}))
vi.mock('react', async () => {
const actual = await vi.importActual<typeof import('react')>('react') // eslint-disable-line @typescript-eslint/consistent-type-imports -- vi.importActual requires inline import()
return {
...actual,
useCallback<T extends (...args: never[]) => unknown>(callback: T): T {
return callback
},
useId(): string {
hookRuntime.id += 1
return `test-id-${hookRuntime.id}`
},
useRef<T>(initial: T) {
const refIndex = hookRuntime.refIndex++
if (!(refIndex in hookRuntime.refs)) {
hookRuntime.refs[refIndex] = { current: initial }
}
return hookRuntime.refs[refIndex] as { current: T }
},
useState<T>(initial: T | (() => T)) {
const stateIndex = hookRuntime.stateIndex++
if (!(stateIndex in hookRuntime.states)) {
hookRuntime.states[stateIndex] =
typeof initial === 'function' ? (initial as () => T)() : initial
}
const setState = (next: T | ((previous: T) => T)): void => {
hookRuntime.states[stateIndex] =
typeof next === 'function'
? (next as (previous: T) => T)(hookRuntime.states[stateIndex] as T)
: next
}
return [hookRuntime.states[stateIndex] as T, setState] as const
}
}
})
function renderOverlay(onAction: () => void | Promise<void>): OverlayElement {
hookRuntime.stateIndex = 0
hookRuntime.refIndex = 0
return MobileDriverOverlay({
driver: { kind: 'mobile', clientId: 'phone-1' } as never,
hasFitOverride: false,
onAction
}) as OverlayElement
}
describe('MobileDriverOverlay', () => {
beforeEach(() => {
hookRuntime.states = []
hookRuntime.stateIndex = 0
hookRuntime.refs = []
hookRuntime.refIndex = 0
hookRuntime.id = 0
})
it('clears stale pending action state when the overlay root reattaches', async () => {
let currentRootRef: OverlayProps['rootRef'] | null = null
const onAction = vi.fn(async () => {
currentRootRef?.(null)
})
let overlay = renderOverlay(onAction)
currentRootRef = overlay.props.rootRef
currentRootRef({} as HTMLDivElement)
await overlay.props.onAction()
overlay = renderOverlay(onAction)
expect(overlay.props.actionPending).toBe(true)
currentRootRef = overlay.props.rootRef
currentRootRef({} as HTMLDivElement)
overlay = renderOverlay(onAction)
expect(overlay.props.actionPending).toBe(false)
})
})
@@ -37,6 +37,11 @@ export function MobileDriverOverlay({
const setOverlayRootRef = useCallback((node: HTMLDivElement | null): void => {
mountedRef.current = node !== null
if (node) {
// Why: take-back can resolve after the overlay renders null; a later
// mobile session must not inherit the stale disabled state.
setActionPending(false)
}
}, [])
const currentCollapseState = getMobileDriverOverlayCollapseState(collapseState, driverClientId)