Migrate tests to use avoid jsdom (#12118)

This commit is contained in:
Jacob Richman
2025-10-28 10:32:15 -07:00
committed by GitHub
parent 16a005304e
commit 8edec7612e
31 changed files with 765 additions and 579 deletions
@@ -4,10 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
/** @vitest-environment jsdom */
import { vi, type Mock, type MockInstance } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { act } from 'react';
import { renderHook } from '../../test-utils/render.js';
import { useFolderTrust } from './useFolderTrust.js';
import type { LoadedSettings } from '../../config/settings.js';
import { FolderTrustChoice } from '../components/FolderTrustDialog.js';
@@ -30,7 +29,6 @@ vi.mock('node:process', async () => {
describe('useFolderTrust', () => {
let mockSettings: LoadedSettings;
let mockTrustedFolders: LoadedTrustedFolders;
let loadTrustedFoldersSpy: MockInstance;
let isWorkspaceTrustedSpy: MockInstance;
let onTrustChange: (isTrusted: boolean | undefined) => void;
let addItem: Mock;
@@ -51,9 +49,9 @@ describe('useFolderTrust', () => {
setValue: vi.fn(),
} as unknown as LoadedTrustedFolders;
loadTrustedFoldersSpy = vi
.spyOn(trustedFolders, 'loadTrustedFolders')
.mockReturnValue(mockTrustedFolders);
vi.spyOn(trustedFolders, 'loadTrustedFolders').mockReturnValue(
mockTrustedFolders,
);
isWorkspaceTrustedSpy = vi.spyOn(trustedFolders, 'isWorkspaceTrusted');
mockedCwd.mockReturnValue('/test/path');
onTrustChange = vi.fn();
@@ -82,7 +80,7 @@ describe('useFolderTrust', () => {
expect(onTrustChange).toHaveBeenCalledWith(false);
});
it('should open dialog when folder trust is undefined', () => {
it('should open dialog when folder trust is undefined', async () => {
isWorkspaceTrustedSpy.mockReturnValue({
isTrusted: undefined,
source: undefined,
@@ -90,7 +88,9 @@ describe('useFolderTrust', () => {
const { result } = renderHook(() =>
useFolderTrust(mockSettings, onTrustChange, addItem),
);
expect(result.current.isFolderTrustDialogOpen).toBe(true);
await vi.waitFor(() => {
expect(result.current.isFolderTrustDialogOpen).toBe(true);
});
expect(onTrustChange).toHaveBeenCalledWith(undefined);
});
@@ -112,26 +112,41 @@ describe('useFolderTrust', () => {
expect(addItem).not.toHaveBeenCalled();
});
it('should handle TRUST_FOLDER choice', () => {
it('should handle TRUST_FOLDER choice', async () => {
isWorkspaceTrustedSpy.mockReturnValue({
isTrusted: undefined,
source: undefined,
});
(mockTrustedFolders.setValue as Mock).mockImplementation(() => {
isWorkspaceTrustedSpy.mockReturnValue({
isTrusted: true,
source: 'file',
});
});
const { result } = renderHook(() =>
useFolderTrust(mockSettings, onTrustChange, addItem),
);
act(() => {
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
await vi.waitFor(() => {
expect(result.current.isTrusted).toBeUndefined();
});
expect(loadTrustedFoldersSpy).toHaveBeenCalled();
expect(mockTrustedFolders.setValue).toHaveBeenCalledWith(
'/test/path',
TrustLevel.TRUST_FOLDER,
);
expect(result.current.isFolderTrustDialogOpen).toBe(false);
expect(onTrustChange).toHaveBeenLastCalledWith(true);
await act(async () => {
await result.current.handleFolderTrustSelect(
FolderTrustChoice.TRUST_FOLDER,
);
});
await vi.waitFor(() => {
expect(mockTrustedFolders.setValue).toHaveBeenCalledWith(
'/test/path',
TrustLevel.TRUST_FOLDER,
);
expect(result.current.isFolderTrustDialogOpen).toBe(false);
expect(onTrustChange).toHaveBeenLastCalledWith(true);
});
});
it('should handle TRUST_PARENT choice', () => {
@@ -177,7 +192,7 @@ describe('useFolderTrust', () => {
expect(result.current.isFolderTrustDialogOpen).toBe(true);
});
it('should do nothing for default choice', () => {
it('should do nothing for default choice', async () => {
isWorkspaceTrustedSpy.mockReturnValue({
isTrusted: undefined,
source: undefined,
@@ -192,24 +207,40 @@ describe('useFolderTrust', () => {
);
});
expect(mockTrustedFolders.setValue).not.toHaveBeenCalled();
expect(mockSettings.setValue).not.toHaveBeenCalled();
expect(result.current.isFolderTrustDialogOpen).toBe(true);
expect(onTrustChange).toHaveBeenCalledWith(undefined);
await vi.waitFor(() => {
expect(mockTrustedFolders.setValue).not.toHaveBeenCalled();
expect(mockSettings.setValue).not.toHaveBeenCalled();
expect(result.current.isFolderTrustDialogOpen).toBe(true);
expect(onTrustChange).toHaveBeenCalledWith(undefined);
});
});
it('should set isRestarting to true when trust status changes from false to true', () => {
it('should set isRestarting to true when trust status changes from false to true', async () => {
isWorkspaceTrustedSpy.mockReturnValue({ isTrusted: false, source: 'file' }); // Initially untrusted
(mockTrustedFolders.setValue as Mock).mockImplementation(() => {
isWorkspaceTrustedSpy.mockReturnValue({
isTrusted: true,
source: 'file',
});
});
const { result } = renderHook(() =>
useFolderTrust(mockSettings, onTrustChange, addItem),
);
await vi.waitFor(() => {
expect(result.current.isTrusted).toBe(false);
});
act(() => {
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
});
expect(result.current.isRestarting).toBe(true);
expect(result.current.isFolderTrustDialogOpen).toBe(true); // Dialog should stay open
await vi.waitFor(() => {
expect(result.current.isRestarting).toBe(true);
expect(result.current.isFolderTrustDialogOpen).toBe(true); // Dialog should stay open
});
});
it('should not set isRestarting to true when trust status does not change', () => {