Skip to content

Commit

Permalink
wrap switchTab in a conditional (#941)
Browse files Browse the repository at this point in the history
* wrap switchTab in a conditional

* unnested tabswitch happy path and added tests
  • Loading branch information
avatus authored Jun 30, 2022
1 parent 986bff2 commit d41205e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions web/packages/teleterm/src/ui/TabHost/useTabShortcuts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ test('close active tab', () => {
expect(docsService.close).toHaveBeenCalledWith(documentToClose.uri);
});

test('should ignore close command if no tabs are open', () => {
const { emitKeyboardShortcutEvent, docsService } = getTestSetup({
documents: [],
});

emitKeyboardShortcutEvent({ type: 'tab-close' });

expect(docsService.close).not.toHaveBeenCalled();
});

test('open new tab', () => {
const { emitKeyboardShortcutEvent, docsService } = getTestSetup({
documents: [],
Expand Down Expand Up @@ -259,4 +269,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();
});
});
5 changes: 5 additions & 0 deletions web/packages/teleterm/src/ui/TabHost/useTabShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d41205e

Please sign in to comment.