File size: 4,714 Bytes
52efc7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { renderWithProviders } from '../../test-utils/render.js';
import { EventEmitter } from 'node:events';
import { useFocus } from './useFocus.js';
import { vi, type Mock } from 'vitest';
import { useStdin, useStdout } from 'ink';
import { act } from 'react';
// Mock the ink hooks
vi.mock('ink', async (importOriginal) => {
const original = await importOriginal<typeof import('ink')>();
return {
...original,
useStdin: vi.fn(),
useStdout: vi.fn(),
};
});
const mockedUseStdin = vi.mocked(useStdin);
const mockedUseStdout = vi.mocked(useStdout);
describe('useFocus', () => {
let stdin: EventEmitter & { resume: Mock; pause: Mock };
let stdout: { write: Mock };
beforeEach(() => {
stdin = Object.assign(new EventEmitter(), {
resume: vi.fn(),
pause: vi.fn(),
});
stdout = { write: vi.fn() };
mockedUseStdin.mockReturnValue({ stdin } as unknown as ReturnType<
typeof useStdin
>);
mockedUseStdout.mockReturnValue({ stdout } as unknown as ReturnType<
typeof useStdout
>);
});
afterEach(() => {
vi.clearAllMocks();
stdin.removeAllListeners();
});
const renderFocusHook = async () => {
let hookResult: ReturnType<typeof useFocus>;
function TestComponent() {
hookResult = useFocus();
return null;
}
const { unmount } = await renderWithProviders(<TestComponent />);
return {
result: {
get current() {
return hookResult;
},
},
unmount,
};
};
it('should initialize with focus and enable focus reporting', async () => {
const { result } = await renderFocusHook();
expect(result.current.isFocused).toBe(true);
expect(stdout.write).toHaveBeenCalledWith('\x1b[?1004h');
});
it('should set isFocused to false when a focus-out event is received', async () => {
const { result } = await renderFocusHook();
// Initial state is focused
expect(result.current.isFocused).toBe(true);
// Simulate focus-out event
act(() => {
stdin.emit('data', '\x1b[O');
});
// State should now be unfocused
expect(result.current.isFocused).toBe(false);
});
it('should set isFocused to true when a focus-in event is received', async () => {
const { result } = await renderFocusHook();
// Simulate focus-out to set initial state to false
act(() => {
stdin.emit('data', '\x1b[O');
});
expect(result.current.isFocused).toBe(false);
// Simulate focus-in event
act(() => {
stdin.emit('data', '\x1b[I');
});
// State should now be focused
expect(result.current.isFocused).toBe(true);
});
it('should clean up and disable focus reporting on unmount', async () => {
const { unmount } = await renderFocusHook();
// At this point we should have listeners from both KeypressProvider and useFocus
const listenerCountAfterMount = stdin.listenerCount('data');
expect(listenerCountAfterMount).toBeGreaterThanOrEqual(1);
unmount();
// Assert that the cleanup function was called
expect(stdout.write).toHaveBeenCalledWith('\x1b[?1004l');
// Ensure useFocus listener was removed (but KeypressProvider listeners may remain)
expect(stdin.listenerCount('data')).toBeLessThan(listenerCountAfterMount);
});
it('should handle multiple focus events correctly', async () => {
const { result } = await renderFocusHook();
act(() => {
stdin.emit('data', '\x1b[O');
});
expect(result.current.isFocused).toBe(false);
act(() => {
stdin.emit('data', '\x1b[O');
});
expect(result.current.isFocused).toBe(false);
act(() => {
stdin.emit('data', '\x1b[I');
});
expect(result.current.isFocused).toBe(true);
act(() => {
stdin.emit('data', '\x1b[I');
});
expect(result.current.isFocused).toBe(true);
});
it('restores focus on keypress after focus is lost', async () => {
const { result } = await renderFocusHook();
// Simulate focus-out event
act(() => {
stdin.emit('data', '\x1b[O');
});
expect(result.current.isFocused).toBe(false);
// Simulate a keypress
act(() => {
stdin.emit('data', 'a');
});
expect(result.current.isFocused).toBe(true);
});
it('tracks whether any focus event has been received', async () => {
const { result } = await renderFocusHook();
expect(result.current.hasReceivedFocusEvent).toBe(false);
act(() => {
stdin.emit('data', '\x1b[O');
});
expect(result.current.hasReceivedFocusEvent).toBe(true);
expect(result.current.isFocused).toBe(false);
});
});
|