|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { render } from '../../test-utils/render.js'; |
| 8 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'; |
| 9 | +import { CloudFreePrivacyNotice } from './CloudFreePrivacyNotice.js'; |
| 10 | +import { usePrivacySettings } from '../hooks/usePrivacySettings.js'; |
| 11 | +import { useKeypress } from '../hooks/useKeypress.js'; |
| 12 | +import type { Config } from '@google/gemini-cli-core'; |
| 13 | +import { RadioButtonSelect } from '../components/shared/RadioButtonSelect.js'; |
| 14 | + |
| 15 | +// Mocks |
| 16 | +vi.mock('../hooks/usePrivacySettings.js', () => ({ |
| 17 | + usePrivacySettings: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('../components/shared/RadioButtonSelect.js', () => ({ |
| 21 | + RadioButtonSelect: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('../hooks/useKeypress.js', () => ({ |
| 25 | + useKeypress: vi.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +const mockedUsePrivacySettings = usePrivacySettings as Mock; |
| 29 | +const mockedUseKeypress = useKeypress as Mock; |
| 30 | +const mockedRadioButtonSelect = RadioButtonSelect as Mock; |
| 31 | + |
| 32 | +describe('CloudFreePrivacyNotice', () => { |
| 33 | + const mockConfig = {} as Config; |
| 34 | + const onExit = vi.fn(); |
| 35 | + const updateDataCollectionOptIn = vi.fn(); |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + vi.resetAllMocks(); |
| 39 | + mockedUsePrivacySettings.mockReturnValue({ |
| 40 | + privacyState: { |
| 41 | + isLoading: false, |
| 42 | + error: undefined, |
| 43 | + isFreeTier: true, |
| 44 | + dataCollectionOptIn: undefined, |
| 45 | + }, |
| 46 | + updateDataCollectionOptIn, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + const defaultState = { |
| 51 | + isLoading: false, |
| 52 | + error: undefined, |
| 53 | + isFreeTier: true, |
| 54 | + dataCollectionOptIn: undefined, |
| 55 | + }; |
| 56 | + |
| 57 | + it.each([ |
| 58 | + { |
| 59 | + stateName: 'loading state', |
| 60 | + mockState: { isLoading: true }, |
| 61 | + expectedText: 'Loading...', |
| 62 | + }, |
| 63 | + { |
| 64 | + stateName: 'error state', |
| 65 | + mockState: { error: 'Something went wrong' }, |
| 66 | + expectedText: 'Error loading Opt-in settings', |
| 67 | + }, |
| 68 | + { |
| 69 | + stateName: 'non-free tier state', |
| 70 | + mockState: { isFreeTier: false }, |
| 71 | + expectedText: 'Gemini Code Assist Privacy Notice', |
| 72 | + }, |
| 73 | + { |
| 74 | + stateName: 'free tier state', |
| 75 | + mockState: { isFreeTier: true }, |
| 76 | + expectedText: 'Gemini Code Assist for Individuals Privacy Notice', |
| 77 | + }, |
| 78 | + ])('renders correctly in $stateName', ({ mockState, expectedText }) => { |
| 79 | + mockedUsePrivacySettings.mockReturnValue({ |
| 80 | + privacyState: { ...defaultState, ...mockState }, |
| 81 | + updateDataCollectionOptIn, |
| 82 | + }); |
| 83 | + |
| 84 | + const { lastFrame } = render( |
| 85 | + <CloudFreePrivacyNotice config={mockConfig} onExit={onExit} />, |
| 86 | + ); |
| 87 | + |
| 88 | + expect(lastFrame()).toContain(expectedText); |
| 89 | + }); |
| 90 | + |
| 91 | + it.each([ |
| 92 | + { |
| 93 | + stateName: 'error state', |
| 94 | + mockState: { error: 'Something went wrong' }, |
| 95 | + shouldExit: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + stateName: 'non-free tier state', |
| 99 | + mockState: { isFreeTier: false }, |
| 100 | + shouldExit: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + stateName: 'free tier state (no selection)', |
| 104 | + mockState: { isFreeTier: true }, |
| 105 | + shouldExit: false, |
| 106 | + }, |
| 107 | + ])( |
| 108 | + 'exits on Escape in $stateName: $shouldExit', |
| 109 | + ({ mockState, shouldExit }) => { |
| 110 | + mockedUsePrivacySettings.mockReturnValue({ |
| 111 | + privacyState: { ...defaultState, ...mockState }, |
| 112 | + updateDataCollectionOptIn, |
| 113 | + }); |
| 114 | + |
| 115 | + render(<CloudFreePrivacyNotice config={mockConfig} onExit={onExit} />); |
| 116 | + |
| 117 | + const keypressHandler = mockedUseKeypress.mock.calls[0][0]; |
| 118 | + keypressHandler({ name: 'escape' }); |
| 119 | + |
| 120 | + if (shouldExit) { |
| 121 | + expect(onExit).toHaveBeenCalled(); |
| 122 | + } else { |
| 123 | + expect(onExit).not.toHaveBeenCalled(); |
| 124 | + } |
| 125 | + }, |
| 126 | + ); |
| 127 | + |
| 128 | + describe('RadioButtonSelect interaction', () => { |
| 129 | + it.each([ |
| 130 | + { selection: true, label: 'Yes' }, |
| 131 | + { selection: false, label: 'No' }, |
| 132 | + ])('calls correct functions on selecting "$label"', ({ selection }) => { |
| 133 | + render(<CloudFreePrivacyNotice config={mockConfig} onExit={onExit} />); |
| 134 | + |
| 135 | + const onSelectHandler = mockedRadioButtonSelect.mock.calls[0][0].onSelect; |
| 136 | + onSelectHandler(selection); |
| 137 | + |
| 138 | + expect(updateDataCollectionOptIn).toHaveBeenCalledWith(selection); |
| 139 | + expect(onExit).toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments