mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-07-08 00:57:16 -07:00
alternate buffer support (#12471)
This commit is contained in:
@@ -194,7 +194,7 @@ describe('useKeypress', () => {
|
||||
stdin.write('do');
|
||||
});
|
||||
expect(onKeypress).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ code: '[200d' }),
|
||||
expect.objectContaining({ sequence: '\x1B[200d' }),
|
||||
);
|
||||
expect(onKeypress).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ sequence: 'o' }),
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { vi } from 'vitest';
|
||||
import { renderHook } from '../../test-utils/render.js';
|
||||
import { useMouse } from './useMouse.js';
|
||||
import { MouseProvider, useMouseContext } from '../contexts/MouseContext.js';
|
||||
|
||||
vi.mock('../contexts/MouseContext.js', async (importOriginal) => {
|
||||
const actual =
|
||||
await importOriginal<typeof import('../contexts/MouseContext.js')>();
|
||||
const subscribe = vi.fn();
|
||||
const unsubscribe = vi.fn();
|
||||
return {
|
||||
...actual,
|
||||
useMouseContext: () => ({
|
||||
subscribe,
|
||||
unsubscribe,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
describe('useMouse', () => {
|
||||
const mockOnMouseEvent = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should not subscribe when isActive is false', () => {
|
||||
renderHook(() => useMouse(mockOnMouseEvent, { isActive: false }), {
|
||||
wrapper: MouseProvider,
|
||||
});
|
||||
|
||||
const { subscribe } = useMouseContext();
|
||||
expect(subscribe).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should subscribe when isActive is true', () => {
|
||||
renderHook(() => useMouse(mockOnMouseEvent, { isActive: true }), {
|
||||
wrapper: MouseProvider,
|
||||
});
|
||||
|
||||
const { subscribe } = useMouseContext();
|
||||
expect(subscribe).toHaveBeenCalledWith(mockOnMouseEvent);
|
||||
});
|
||||
|
||||
it('should unsubscribe on unmount', () => {
|
||||
const { unmount } = renderHook(
|
||||
() => useMouse(mockOnMouseEvent, { isActive: true }),
|
||||
{ wrapper: MouseProvider },
|
||||
);
|
||||
|
||||
const { unsubscribe } = useMouseContext();
|
||||
unmount();
|
||||
expect(unsubscribe).toHaveBeenCalledWith(mockOnMouseEvent);
|
||||
});
|
||||
|
||||
it('should unsubscribe when isActive becomes false', () => {
|
||||
const { rerender } = renderHook(
|
||||
({ isActive }: { isActive: boolean }) =>
|
||||
useMouse(mockOnMouseEvent, { isActive }),
|
||||
{
|
||||
initialProps: { isActive: true },
|
||||
wrapper: MouseProvider,
|
||||
},
|
||||
);
|
||||
|
||||
const { unsubscribe } = useMouseContext();
|
||||
rerender({ isActive: false });
|
||||
expect(unsubscribe).toHaveBeenCalledWith(mockOnMouseEvent);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import type { MouseHandler, MouseEvent } from '../contexts/MouseContext.js';
|
||||
import { useMouseContext } from '../contexts/MouseContext.js';
|
||||
|
||||
export type { MouseEvent };
|
||||
|
||||
/**
|
||||
* A hook that listens for mouse events from stdin.
|
||||
*
|
||||
* @param onMouseEvent - The callback function to execute on each mouse event.
|
||||
* @param options - Options to control the hook's behavior.
|
||||
* @param options.isActive - Whether the hook should be actively listening for input.
|
||||
*/
|
||||
export function useMouse(
|
||||
onMouseEvent: MouseHandler,
|
||||
{ isActive }: { isActive: boolean },
|
||||
) {
|
||||
const { subscribe, unsubscribe } = useMouseContext();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
subscribe(onMouseEvent);
|
||||
return () => {
|
||||
unsubscribe(onMouseEvent);
|
||||
};
|
||||
}, [isActive, onMouseEvent, subscribe, unsubscribe]);
|
||||
}
|
||||
Reference in New Issue
Block a user