summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/hooks/useFolderTrust.test.ts
blob: 61552af0b2d5da6f19afbbb341a043e78f46b5c3 (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { renderHook, act } from '@testing-library/react';
import { vi } from 'vitest';
import { useFolderTrust } from './useFolderTrust.js';
import { LoadedSettings, SettingScope } from '../../config/settings.js';
import { FolderTrustChoice } from '../components/FolderTrustDialog.js';

describe('useFolderTrust', () => {
  it('should set isFolderTrustDialogOpen to true when folderTrustFeature is true and folderTrust is undefined', () => {
    const settings = {
      merged: {
        folderTrustFeature: true,
        folderTrust: undefined,
      },
      setValue: vi.fn(),
    } as unknown as LoadedSettings;

    const { result } = renderHook(() => useFolderTrust(settings));

    expect(result.current.isFolderTrustDialogOpen).toBe(true);
  });

  it('should set isFolderTrustDialogOpen to false when folderTrustFeature is false', () => {
    const settings = {
      merged: {
        folderTrustFeature: false,
        folderTrust: undefined,
      },
      setValue: vi.fn(),
    } as unknown as LoadedSettings;

    const { result } = renderHook(() => useFolderTrust(settings));

    expect(result.current.isFolderTrustDialogOpen).toBe(false);
  });

  it('should set isFolderTrustDialogOpen to false when folderTrust is defined', () => {
    const settings = {
      merged: {
        folderTrustFeature: true,
        folderTrust: true,
      },
      setValue: vi.fn(),
    } as unknown as LoadedSettings;

    const { result } = renderHook(() => useFolderTrust(settings));

    expect(result.current.isFolderTrustDialogOpen).toBe(false);
  });

  it('should call setValue and set isFolderTrustDialogOpen to false on handleFolderTrustSelect', () => {
    const settings = {
      merged: {
        folderTrustFeature: true,
        folderTrust: undefined,
      },
      setValue: vi.fn(),
    } as unknown as LoadedSettings;

    const { result } = renderHook(() => useFolderTrust(settings));

    act(() => {
      result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
    });

    expect(settings.setValue).toHaveBeenCalledWith(
      SettingScope.User,
      'folderTrust',
      true,
    );
    expect(result.current.isFolderTrustDialogOpen).toBe(false);
  });
});