From 2f37d4bb68c0dbef0e09dcde09bcebda84731eb3 Mon Sep 17 00:00:00 2001 From: clean99 <53188310+clean99@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:32:02 +0800 Subject: [PATCH] fix: release option lock when delayed close is canceled --- src/hooks/useOpen.ts | 11 +++-- tests/Hooks.test.tsx | 103 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 8 deletions(-) diff --git a/src/hooks/useOpen.ts b/src/hooks/useOpen.ts index 591ec6c74..9c73a6bbe 100644 --- a/src/hooks/useOpen.ts +++ b/src/hooks/useOpen.ts @@ -80,13 +80,12 @@ export default function useOpen( setLock(!nextOpenVal); function triggerUpdate() { - if ( - // Always check if id is match - id === taskIdRef.current && + // Always check if id is match + if (id === taskIdRef.current) { // Check if need to cancel - !cancelFun?.() - ) { - triggerEvent(nextOpenVal); + if (!cancelFun?.()) { + triggerEvent(nextOpenVal); + } setLock(false); } } diff --git a/tests/Hooks.test.tsx b/tests/Hooks.test.tsx index 1868f29cd..74a6d7d3d 100644 --- a/tests/Hooks.test.tsx +++ b/tests/Hooks.test.tsx @@ -1,11 +1,110 @@ -import React from 'react'; -import { render } from '@testing-library/react'; +import React, { act } from 'react'; +import { render, renderHook } from '@testing-library/react'; import useLock from '../src/hooks/useLock'; +import useOpen from '../src/hooks/useOpen'; import { injectRunAllTimers } from './utils/common'; describe('Hooks', () => { injectRunAllTimers(jest); + describe('useOpen', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.runAllTimers(); + jest.useRealTimers(); + }); + + it.each([undefined, true])('unlocks a canceled close with open=%s', (open) => { + const onOpen = jest.fn(); + const cancelFun = jest.fn(() => true); + const { result } = renderHook(() => useOpen(true, open, onOpen, (value) => value)); + + act(() => { + result.current[2](false, { cancelFun }); + }); + expect(result.current[3]).toBe(true); + expect(cancelFun).not.toHaveBeenCalled(); + + jest.runAllTimers(); + + expect(cancelFun).toHaveBeenCalledTimes(1); + expect(result.current[0]).toBe(true); + expect(result.current[1]).toBe(true); + expect(result.current[3]).toBe(false); + expect(onOpen).not.toHaveBeenCalled(); + }); + + it('closes and unlocks when not canceled', () => { + const onOpen = jest.fn(); + const { result } = renderHook(() => useOpen(true, undefined, onOpen, (value) => value)); + + act(() => { + result.current[2](false, { cancelFun: () => false }); + }); + expect(result.current[1]).toBe(true); + expect(result.current[3]).toBe(true); + + jest.runAllTimers(); + + expect(result.current[0]).toBe(false); + expect(result.current[1]).toBe(false); + expect(result.current[3]).toBe(false); + expect(onOpen).toHaveBeenCalledTimes(1); + expect(onOpen).toHaveBeenCalledWith(false); + }); + + it('unlocks immediately when reopened before a close completes', () => { + const onOpen = jest.fn(); + const cancelFun = jest.fn(() => false); + const { result } = renderHook(() => useOpen(true, undefined, onOpen, (value) => value)); + + act(() => { + result.current[2](false, { cancelFun }); + }); + expect(result.current[3]).toBe(true); + + act(() => { + result.current[2](true); + }); + expect(result.current[3]).toBe(false); + + jest.runAllTimers(); + + expect(result.current[1]).toBe(true); + expect(result.current[3]).toBe(false); + expect(cancelFun).not.toHaveBeenCalled(); + expect(onOpen).not.toHaveBeenCalled(); + }); + + it('does not let a stale close unlock a newer close', () => { + const onOpen = jest.fn(); + const cancelFun = jest.fn(() => true); + const { result } = renderHook(() => useOpen(true, undefined, onOpen, (value) => value)); + + act(() => { + result.current[2](false, { cancelFun }); + jest.advanceTimersByTime(5); + result.current[2](false); + jest.advanceTimersByTime(5); + }); + + expect(cancelFun).not.toHaveBeenCalled(); + expect(result.current[1]).toBe(true); + expect(result.current[3]).toBe(true); + expect(onOpen).not.toHaveBeenCalled(); + + jest.runAllTimers(); + + expect(result.current[1]).toBe(false); + expect(result.current[3]).toBe(false); + expect(onOpen).toHaveBeenCalledTimes(1); + expect(onOpen).toHaveBeenCalledWith(false); + }); + }); + it('useLock', () => { jest.useFakeTimers(); const clearTimeoutSpy = jest.spyOn(window, 'clearTimeout');