diff --git a/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.test.tsx b/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.test.tsx index 2e63adb66f006..bf5f6b81c4425 100644 --- a/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.test.tsx +++ b/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.test.tsx @@ -186,6 +186,15 @@ test('close active tab', () => { expect(docsService.close).toHaveBeenCalledWith(documentToClose.uri); }); +test('do not switch tabs if tabs do not exist', () => { + const { emitKeyboardShortcutEvent, docsService } = getTestSetup({ + documents: [], + }); + emitKeyboardShortcutEvent({ type: 'tab-next' }); + + expect(docsService.open).not.toHaveBeenCalled(); +}); + test('open new tab', () => { const { emitKeyboardShortcutEvent, docsService } = getTestSetup({ documents: [], @@ -259,4 +268,13 @@ describe('open next/previous tab', () => { docsService.getDocuments()[docsService.getDocuments().length - 1].uri ); }); + + test('do not switch tabs if tabs do not exist', () => { + const { emitKeyboardShortcutEvent, docsService } = getTestSetup({ + documents: [], + }); + emitKeyboardShortcutEvent({ type: 'tab-next' }); + + expect(docsService.open).not.toHaveBeenCalled(); + }); }); diff --git a/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.ts b/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.ts index 59d7cabf7c906..270ed2e3ee14c 100644 --- a/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.ts +++ b/web/packages/teleterm/src/ui/TabHost/useTabShortcuts.ts @@ -61,6 +61,11 @@ function buildTabsShortcuts( const handleTabSwitch = (direction: 'previous' | 'next') => () => { const activeDoc = documentService.getActive(); const allDocuments = documentService.getDocuments(); + + if (allDocuments.length === 0) { + return; + } + const activeDocIndex = allDocuments.indexOf(activeDoc); const getPreviousIndex = () => (activeDocIndex - 1 + allDocuments.length) % allDocuments.length;