diff options
| author | Scott Densmore <[email protected]> | 2025-06-08 14:59:18 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-08 14:59:18 -0700 |
| commit | 9104ac02f7ac68d84bf9a3a78514bd080c77eec5 (patch) | |
| tree | 1b559e825f76e44d94d188b52951bc633c6f055b /packages/cli/src/ui/hooks/useGitBranchName.test.ts | |
| parent | 394312b9c2f6006293b344a3c2b81da17332a2d5 (diff) | |
feat: display commit hash in detached HEAD state (#832)
Diffstat (limited to 'packages/cli/src/ui/hooks/useGitBranchName.test.ts')
| -rw-r--r-- | packages/cli/src/ui/hooks/useGitBranchName.test.ts | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/packages/cli/src/ui/hooks/useGitBranchName.test.ts b/packages/cli/src/ui/hooks/useGitBranchName.test.ts index f5e71fbf..a749d64d 100644 --- a/packages/cli/src/ui/hooks/useGitBranchName.test.ts +++ b/packages/cli/src/ui/hooks/useGitBranchName.test.ts @@ -88,16 +88,39 @@ describe('useGitBranchName', () => { expect(result.current).toBeUndefined(); }); - it('should return undefined if branch is HEAD (detached state)', async () => { + it('should return short commit hash if branch is HEAD (detached state)', async () => { (mockExec as MockedFunction<typeof mockExec>).mockImplementation( - (_command, _options, callback) => { - callback?.(null, 'HEAD\n', ''); + (command, _options, callback) => { + if (command === 'git rev-parse --abbrev-ref HEAD') { + callback?.(null, 'HEAD\n', ''); + } else if (command === 'git rev-parse --short HEAD') { + callback?.(null, 'a1b2c3d\n', ''); + } + return new EventEmitter() as ChildProcess; + }, + ); + + const { result, rerender } = renderHook(() => useGitBranchName(CWD)); + await act(async () => { + vi.runAllTimers(); + rerender(); + }); + expect(result.current).toBe('a1b2c3d'); + }); + + it('should return undefined if branch is HEAD and getting commit hash fails', async () => { + (mockExec as MockedFunction<typeof mockExec>).mockImplementation( + (command, _options, callback) => { + if (command === 'git rev-parse --abbrev-ref HEAD') { + callback?.(null, 'HEAD\n', ''); + } else if (command === 'git rev-parse --short HEAD') { + callback?.(new Error('Git error'), '', 'error output'); + } return new EventEmitter() as ChildProcess; }, ); const { result, rerender } = renderHook(() => useGitBranchName(CWD)); - expect(result.current).toBeUndefined(); await act(async () => { vi.runAllTimers(); rerender(); |
