Skip to content
This repository was archived by the owner on Aug 5, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ describe('BpkBottomSheet', () => {
expect(titleEl).toBeInTheDocument();
});

it('does not call onClose after unmount (timer leak regression)', () => {
jest.useFakeTimers();
const onClose = jest.fn();
const { unmount } = render(
<BpkBottomSheet
ariaLabelledby="bottom-sheet"
closeLabel="Close"
id="my-bottom-sheet"
isOpen
onClose={onClose}
>
Content
</BpkBottomSheet>,
);
// Simulate close button click would normally schedule a 240ms timer.
// Unmounting before the timer fires should cancel it.
unmount();
jest.advanceTimersByTime(300);
expect(onClose).not.toHaveBeenCalled();
jest.useRealTimers();
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed — the test now clicks the Close button first (via userEvent) so the 240ms timer is actually scheduled before unmounting. The assertion now meaningfully catches the regression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed — moved timer restoration to afterEach(() => jest.useRealTimers()) so it runs even if the test throws, and wrapped advanceTimersByTime in act() to avoid React update warnings.


it('adds correct id to custom title element for aria-labelledby reference', () => {
const { container } = render(
<BpkBottomSheet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/
import type { SyntheticEvent, ReactNode, ReactElement } from 'react';
import { useCallback, useState, isValidElement, cloneElement } from 'react';
import { useCallback, useEffect, useRef, useState, isValidElement, cloneElement } from 'react';

import BpkBreakpoint, { BREAKPOINTS } from '../../bpk-component-breakpoint';
// @ts-expect-error Untyped import. See `decisions/imports-ts-suppressions.md`.
Expand Down Expand Up @@ -130,9 +130,19 @@ const BpkBottomSheet = ({
isAboveMobile: boolean;
}) => {
const [exiting, setExiting] = useState(false);
const closeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);

const animationTimeout = 240;

useEffect(
() => () => {
if (closeTimerRef.current !== null) {
clearTimeout(closeTimerRef.current);
}
},
[],
);

const handleClose = useCallback(
(
arg0?:
Expand All @@ -147,7 +157,7 @@ const BpkBottomSheet = ({
const timeoutDuration = isAboveMobile ? 0 : animationTimeout;

setExiting(true);
setTimeout(() => {
closeTimerRef.current = setTimeout(() => {
onClose(arg0, arg1);
setExiting(false);
}, timeoutDuration);
Comment on lines 146 to 167

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed — now clears any in-flight timer before scheduling a new one, and resets closeTimerRef.current = null after the timer fires so state accurately reflects whether a timer is pending.

Expand Down
Loading