mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-21 10:34:35 -07:00
Migrate tests to use avoid jsdom (#12118)
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { renderHook } from './render.js';
|
||||
|
||||
describe('renderHook', () => {
|
||||
it('should rerender with previous props when called without arguments', async () => {
|
||||
const useTestHook = ({ value }: { value: number }) => {
|
||||
const [count, setCount] = useState(0);
|
||||
useEffect(() => {
|
||||
setCount((c) => c + 1);
|
||||
}, [value]);
|
||||
return { count, value };
|
||||
};
|
||||
|
||||
const { result, rerender } = renderHook(useTestHook, {
|
||||
initialProps: { value: 1 },
|
||||
});
|
||||
|
||||
expect(result.current.value).toBe(1);
|
||||
await vi.waitFor(() => expect(result.current.count).toBe(1));
|
||||
|
||||
// Rerender with new props
|
||||
rerender({ value: 2 });
|
||||
expect(result.current.value).toBe(2);
|
||||
await vi.waitFor(() => expect(result.current.count).toBe(2));
|
||||
|
||||
// Rerender without arguments should use previous props (value: 2)
|
||||
// This would previously crash or pass undefined if not fixed
|
||||
rerender();
|
||||
expect(result.current.value).toBe(2);
|
||||
// Count should not increase because value didn't change
|
||||
await vi.waitFor(() => expect(result.current.count).toBe(2));
|
||||
});
|
||||
|
||||
it('should handle initial render without props', () => {
|
||||
const useTestHook = () => {
|
||||
const [count, setCount] = useState(0);
|
||||
return { count, increment: () => setCount((c) => c + 1) };
|
||||
};
|
||||
|
||||
const { result, rerender } = renderHook(useTestHook);
|
||||
|
||||
expect(result.current.count).toBe(0);
|
||||
|
||||
rerender();
|
||||
expect(result.current.count).toBe(0);
|
||||
});
|
||||
|
||||
it('should update props if undefined is passed explicitly', () => {
|
||||
const useTestHook = (val: string | undefined) => val;
|
||||
const { result, rerender } = renderHook(useTestHook, {
|
||||
initialProps: 'initial',
|
||||
});
|
||||
|
||||
expect(result.current).toBe('initial');
|
||||
|
||||
rerender(undefined);
|
||||
expect(result.current).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import { render } from 'ink-testing-library';
|
||||
import type React from 'react';
|
||||
import { act } from 'react';
|
||||
import { LoadedSettings, type Settings } from '../config/settings.js';
|
||||
import { KeypressProvider } from '../ui/contexts/KeypressContext.js';
|
||||
import { SettingsContext } from '../ui/contexts/SettingsContext.js';
|
||||
@@ -128,3 +129,59 @@ export const renderWithProviders = (
|
||||
</ConfigContext.Provider>,
|
||||
);
|
||||
};
|
||||
|
||||
export function renderHook<Result, Props>(
|
||||
renderCallback: (props: Props) => Result,
|
||||
options?: {
|
||||
initialProps?: Props;
|
||||
wrapper?: React.ComponentType<{ children: React.ReactNode }>;
|
||||
},
|
||||
): {
|
||||
result: { current: Result };
|
||||
rerender: (props?: Props) => void;
|
||||
unmount: () => void;
|
||||
} {
|
||||
const result = { current: undefined as unknown as Result };
|
||||
let currentProps = options?.initialProps as Props;
|
||||
|
||||
function TestComponent({
|
||||
renderCallback,
|
||||
props,
|
||||
}: {
|
||||
renderCallback: (props: Props) => Result;
|
||||
props: Props;
|
||||
}) {
|
||||
result.current = renderCallback(props);
|
||||
return null;
|
||||
}
|
||||
|
||||
const Wrapper = options?.wrapper || (({ children }) => <>{children}</>);
|
||||
|
||||
let inkRerender: (tree: React.ReactElement) => void = () => {};
|
||||
let unmount: () => void = () => {};
|
||||
|
||||
act(() => {
|
||||
const renderResult = render(
|
||||
<Wrapper>
|
||||
<TestComponent renderCallback={renderCallback} props={currentProps} />
|
||||
</Wrapper>,
|
||||
);
|
||||
inkRerender = renderResult.rerender;
|
||||
unmount = renderResult.unmount;
|
||||
});
|
||||
|
||||
function rerender(props?: Props) {
|
||||
if (arguments.length > 0) {
|
||||
currentProps = props as Props;
|
||||
}
|
||||
act(() => {
|
||||
inkRerender(
|
||||
<Wrapper>
|
||||
<TestComponent renderCallback={renderCallback} props={currentProps} />
|
||||
</Wrapper>,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return { result, rerender, unmount };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user