|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import { describe, it, beforeEach, jest } from '@jest/globals'; |
| 3 | +import Sidebar from '../Sidebar'; |
| 4 | + |
| 5 | +// Mock theme hook |
| 6 | +jest.mock('../../lib/useTheme', () => ({ |
| 7 | + __esModule: true, |
| 8 | + default: () => ['light', jest.fn()], |
| 9 | +})); |
| 10 | + |
| 11 | +describe('Sidebar Environment Variables', () => { |
| 12 | + const defaultProps = { |
| 13 | + connectionStatus: 'disconnected' as const, |
| 14 | + transportType: 'stdio' as const, |
| 15 | + setTransportType: jest.fn(), |
| 16 | + command: '', |
| 17 | + setCommand: jest.fn(), |
| 18 | + args: '', |
| 19 | + setArgs: jest.fn(), |
| 20 | + sseUrl: '', |
| 21 | + setSseUrl: jest.fn(), |
| 22 | + env: {}, |
| 23 | + setEnv: jest.fn(), |
| 24 | + bearerToken: '', |
| 25 | + setBearerToken: jest.fn(), |
| 26 | + onConnect: jest.fn(), |
| 27 | + stdErrNotifications: [], |
| 28 | + logLevel: 'info' as const, |
| 29 | + sendLogLevelRequest: jest.fn(), |
| 30 | + loggingSupported: true, |
| 31 | + }; |
| 32 | + |
| 33 | + const renderSidebar = (props = {}) => { |
| 34 | + return render(<Sidebar {...defaultProps} {...props} />); |
| 35 | + }; |
| 36 | + |
| 37 | + const openEnvVarsSection = () => { |
| 38 | + const button = screen.getByText('Environment Variables'); |
| 39 | + fireEvent.click(button); |
| 40 | + }; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + jest.clearAllMocks(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Basic Operations', () => { |
| 47 | + it('should add a new environment variable', () => { |
| 48 | + const setEnv = jest.fn(); |
| 49 | + renderSidebar({ env: {}, setEnv }); |
| 50 | + |
| 51 | + openEnvVarsSection(); |
| 52 | + |
| 53 | + const addButton = screen.getByText('Add Environment Variable'); |
| 54 | + fireEvent.click(addButton); |
| 55 | + |
| 56 | + expect(setEnv).toHaveBeenCalledWith({ '': '' }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should remove an environment variable', () => { |
| 60 | + const setEnv = jest.fn(); |
| 61 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 62 | + renderSidebar({ env: initialEnv, setEnv }); |
| 63 | + |
| 64 | + openEnvVarsSection(); |
| 65 | + |
| 66 | + const removeButton = screen.getByRole('button', { name: '×' }); |
| 67 | + fireEvent.click(removeButton); |
| 68 | + |
| 69 | + expect(setEnv).toHaveBeenCalledWith({}); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should update environment variable value', () => { |
| 73 | + const setEnv = jest.fn(); |
| 74 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 75 | + renderSidebar({ env: initialEnv, setEnv }); |
| 76 | + |
| 77 | + openEnvVarsSection(); |
| 78 | + |
| 79 | + const valueInput = screen.getByDisplayValue('test_value'); |
| 80 | + fireEvent.change(valueInput, { target: { value: 'new_value' } }); |
| 81 | + |
| 82 | + expect(setEnv).toHaveBeenCalledWith({ TEST_KEY: 'new_value' }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should toggle value visibility', () => { |
| 86 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 87 | + renderSidebar({ env: initialEnv }); |
| 88 | + |
| 89 | + openEnvVarsSection(); |
| 90 | + |
| 91 | + const valueInput = screen.getByDisplayValue('test_value'); |
| 92 | + expect(valueInput).toHaveProperty('type', 'password'); |
| 93 | + |
| 94 | + const toggleButton = screen.getByRole('button', { name: /show value/i }); |
| 95 | + fireEvent.click(toggleButton); |
| 96 | + |
| 97 | + expect(valueInput).toHaveProperty('type', 'text'); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('Key Editing', () => { |
| 102 | + it('should maintain order when editing first key', () => { |
| 103 | + const setEnv = jest.fn(); |
| 104 | + const initialEnv = { |
| 105 | + FIRST_KEY: 'first_value', |
| 106 | + SECOND_KEY: 'second_value', |
| 107 | + THIRD_KEY: 'third_value', |
| 108 | + }; |
| 109 | + renderSidebar({ env: initialEnv, setEnv }); |
| 110 | + |
| 111 | + openEnvVarsSection(); |
| 112 | + |
| 113 | + const firstKeyInput = screen.getByDisplayValue('FIRST_KEY'); |
| 114 | + fireEvent.change(firstKeyInput, { target: { value: 'NEW_FIRST_KEY' } }); |
| 115 | + |
| 116 | + expect(setEnv).toHaveBeenCalledWith({ |
| 117 | + NEW_FIRST_KEY: 'first_value', |
| 118 | + SECOND_KEY: 'second_value', |
| 119 | + THIRD_KEY: 'third_value', |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should maintain order when editing middle key', () => { |
| 124 | + const setEnv = jest.fn(); |
| 125 | + const initialEnv = { |
| 126 | + FIRST_KEY: 'first_value', |
| 127 | + SECOND_KEY: 'second_value', |
| 128 | + THIRD_KEY: 'third_value', |
| 129 | + }; |
| 130 | + renderSidebar({ env: initialEnv, setEnv }); |
| 131 | + |
| 132 | + openEnvVarsSection(); |
| 133 | + |
| 134 | + const middleKeyInput = screen.getByDisplayValue('SECOND_KEY'); |
| 135 | + fireEvent.change(middleKeyInput, { target: { value: 'NEW_SECOND_KEY' } }); |
| 136 | + |
| 137 | + expect(setEnv).toHaveBeenCalledWith({ |
| 138 | + FIRST_KEY: 'first_value', |
| 139 | + NEW_SECOND_KEY: 'second_value', |
| 140 | + THIRD_KEY: 'third_value', |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should maintain order when editing last key', () => { |
| 145 | + const setEnv = jest.fn(); |
| 146 | + const initialEnv = { |
| 147 | + FIRST_KEY: 'first_value', |
| 148 | + SECOND_KEY: 'second_value', |
| 149 | + THIRD_KEY: 'third_value', |
| 150 | + }; |
| 151 | + renderSidebar({ env: initialEnv, setEnv }); |
| 152 | + |
| 153 | + openEnvVarsSection(); |
| 154 | + |
| 155 | + const lastKeyInput = screen.getByDisplayValue('THIRD_KEY'); |
| 156 | + fireEvent.change(lastKeyInput, { target: { value: 'NEW_THIRD_KEY' } }); |
| 157 | + |
| 158 | + expect(setEnv).toHaveBeenCalledWith({ |
| 159 | + FIRST_KEY: 'first_value', |
| 160 | + SECOND_KEY: 'second_value', |
| 161 | + NEW_THIRD_KEY: 'third_value', |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('Multiple Operations', () => { |
| 167 | + it('should maintain state after multiple key edits', () => { |
| 168 | + const setEnv = jest.fn(); |
| 169 | + const initialEnv = { |
| 170 | + FIRST_KEY: 'first_value', |
| 171 | + SECOND_KEY: 'second_value', |
| 172 | + }; |
| 173 | + const { rerender } = renderSidebar({ env: initialEnv, setEnv }); |
| 174 | + |
| 175 | + openEnvVarsSection(); |
| 176 | + |
| 177 | + // First key edit |
| 178 | + const firstKeyInput = screen.getByDisplayValue('FIRST_KEY'); |
| 179 | + fireEvent.change(firstKeyInput, { target: { value: 'NEW_FIRST_KEY' } }); |
| 180 | + |
| 181 | + // Get the updated env from the first setEnv call |
| 182 | + const updatedEnv = (setEnv.mock.calls[0][0] as Record<string, string>); |
| 183 | + |
| 184 | + // Rerender with the updated env |
| 185 | + rerender(<Sidebar {...defaultProps} env={updatedEnv} setEnv={setEnv} />); |
| 186 | + |
| 187 | + // Second key edit |
| 188 | + const secondKeyInput = screen.getByDisplayValue('SECOND_KEY'); |
| 189 | + fireEvent.change(secondKeyInput, { target: { value: 'NEW_SECOND_KEY' } }); |
| 190 | + |
| 191 | + // Verify the final state matches what we expect |
| 192 | + expect(setEnv).toHaveBeenLastCalledWith({ |
| 193 | + NEW_FIRST_KEY: 'first_value', |
| 194 | + NEW_SECOND_KEY: 'second_value', |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should maintain visibility state after key edit', () => { |
| 199 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 200 | + const { rerender } = renderSidebar({ env: initialEnv }); |
| 201 | + |
| 202 | + openEnvVarsSection(); |
| 203 | + |
| 204 | + // Show the value |
| 205 | + const toggleButton = screen.getByRole('button', { name: /show value/i }); |
| 206 | + fireEvent.click(toggleButton); |
| 207 | + |
| 208 | + const valueInput = screen.getByDisplayValue('test_value'); |
| 209 | + expect(valueInput).toHaveProperty('type', 'text'); |
| 210 | + |
| 211 | + // Edit the key |
| 212 | + const keyInput = screen.getByDisplayValue('TEST_KEY'); |
| 213 | + fireEvent.change(keyInput, { target: { value: 'NEW_KEY' } }); |
| 214 | + |
| 215 | + // Rerender with updated env |
| 216 | + rerender(<Sidebar {...defaultProps} env={{ NEW_KEY: 'test_value' }} />); |
| 217 | + |
| 218 | + // Value should still be visible |
| 219 | + const updatedValueInput = screen.getByDisplayValue('test_value'); |
| 220 | + expect(updatedValueInput).toHaveProperty('type', 'text'); |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + describe('Edge Cases', () => { |
| 225 | + it('should handle empty key', () => { |
| 226 | + const setEnv = jest.fn(); |
| 227 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 228 | + renderSidebar({ env: initialEnv, setEnv }); |
| 229 | + |
| 230 | + openEnvVarsSection(); |
| 231 | + |
| 232 | + const keyInput = screen.getByDisplayValue('TEST_KEY'); |
| 233 | + fireEvent.change(keyInput, { target: { value: '' } }); |
| 234 | + |
| 235 | + expect(setEnv).toHaveBeenCalledWith({ '': 'test_value' }); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should handle special characters in key', () => { |
| 239 | + const setEnv = jest.fn(); |
| 240 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 241 | + renderSidebar({ env: initialEnv, setEnv }); |
| 242 | + |
| 243 | + openEnvVarsSection(); |
| 244 | + |
| 245 | + const keyInput = screen.getByDisplayValue('TEST_KEY'); |
| 246 | + fireEvent.change(keyInput, { target: { value: 'TEST-KEY@123' } }); |
| 247 | + |
| 248 | + expect(setEnv).toHaveBeenCalledWith({ 'TEST-KEY@123': 'test_value' }); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should handle unicode characters', () => { |
| 252 | + const setEnv = jest.fn(); |
| 253 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 254 | + renderSidebar({ env: initialEnv, setEnv }); |
| 255 | + |
| 256 | + openEnvVarsSection(); |
| 257 | + |
| 258 | + const keyInput = screen.getByDisplayValue('TEST_KEY'); |
| 259 | + fireEvent.change(keyInput, { target: { value: 'TEST_🔑' } }); |
| 260 | + |
| 261 | + expect(setEnv).toHaveBeenCalledWith({ 'TEST_🔑': 'test_value' }); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should handle very long key names', () => { |
| 265 | + const setEnv = jest.fn(); |
| 266 | + const initialEnv = { TEST_KEY: 'test_value' }; |
| 267 | + renderSidebar({ env: initialEnv, setEnv }); |
| 268 | + |
| 269 | + openEnvVarsSection(); |
| 270 | + |
| 271 | + const keyInput = screen.getByDisplayValue('TEST_KEY'); |
| 272 | + const longKey = 'A'.repeat(100); |
| 273 | + fireEvent.change(keyInput, { target: { value: longKey } }); |
| 274 | + |
| 275 | + expect(setEnv).toHaveBeenCalledWith({ [longKey]: 'test_value' }); |
| 276 | + }); |
| 277 | + }); |
| 278 | +}); |
0 commit comments