Add test for handling empty file path in saveLastOpenedFile function

This commit is contained in:
2025-06-04 18:46:31 +02:00
parent 1e80edd5ca
commit 9854deb43b
2 changed files with 17 additions and 17 deletions

View File

@@ -183,6 +183,22 @@ describe('useLastOpenedFile', () => {
expect(fileApi.updateLastOpenedFile).not.toHaveBeenCalled(); expect(fileApi.updateLastOpenedFile).not.toHaveBeenCalled();
}); });
it('does nothing when file path is empty', async () => {
const mockUpdateLastOpenedFile = vi.mocked(fileApi.updateLastOpenedFile);
mockUpdateLastOpenedFile.mockResolvedValue(undefined);
const { result } = renderHook(() => useLastOpenedFile());
await act(async () => {
await result.current.saveLastOpenedFile('');
});
expect(mockUpdateLastOpenedFile).not.toHaveBeenCalledWith(
'test-workspace',
''
);
});
it('handles different file path formats', async () => { it('handles different file path formats', async () => {
const mockUpdateLastOpenedFile = vi.mocked(fileApi.updateLastOpenedFile); const mockUpdateLastOpenedFile = vi.mocked(fileApi.updateLastOpenedFile);
mockUpdateLastOpenedFile.mockResolvedValue(undefined); mockUpdateLastOpenedFile.mockResolvedValue(undefined);
@@ -211,22 +227,6 @@ describe('useLastOpenedFile', () => {
expect(mockUpdateLastOpenedFile).toHaveBeenCalledTimes(testCases.length); expect(mockUpdateLastOpenedFile).toHaveBeenCalledTimes(testCases.length);
}); });
it('handles empty file path', async () => {
const mockUpdateLastOpenedFile = vi.mocked(fileApi.updateLastOpenedFile);
mockUpdateLastOpenedFile.mockResolvedValue(undefined);
const { result } = renderHook(() => useLastOpenedFile());
await act(async () => {
await result.current.saveLastOpenedFile('');
});
expect(mockUpdateLastOpenedFile).toHaveBeenCalledWith(
'test-workspace',
''
);
});
}); });
describe('workspace dependency', () => { describe('workspace dependency', () => {

View File

@@ -24,7 +24,7 @@ export const useLastOpenedFile = (): UseLastOpenedFileResult => {
const saveLastOpenedFile = useCallback( const saveLastOpenedFile = useCallback(
async (filePath: string): Promise<void> => { async (filePath: string): Promise<void> => {
if (!currentWorkspace) return; if (!currentWorkspace || !filePath.trim()) return;
try { try {
await updateLastOpenedFile(currentWorkspace.name, filePath); await updateLastOpenedFile(currentWorkspace.name, filePath);