Skip to content

Commit

Permalink
[ML] Fixes getTab() to always return an array. (#45616)
Browse files Browse the repository at this point in the history
- Fixes getTabs() to always return an array. I was wondering why TypeScript didn't flag the tabs.map(...) part because tabs could possibly be undefined. It's because Record assumes that every key exists. More on that can be found in this blog post.
- This PR fixes a) the type of TAB_MAP by wrapping it in Partial<...> so it correctly flags the tabs.map() part with tabs possibly being undefined and b) fixes getTabs()'s return value by always returning an array.
  • Loading branch information
walterra authored Sep 13, 2019
1 parent fa7614b commit ec5d1a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { getTabs } from './tabs';

describe('Navigation Menu: Tabs', () => {
test('getTabs() always returns an array', () => {
const tabs1 = getTabs('anomaly_detection', false);
expect(Array.isArray(tabs1)).toBeTruthy();
expect(tabs1).toHaveLength(4);

const tabs2 = getTabs('access-denied', false);
expect(Array.isArray(tabs2)).toBeTruthy();
expect(tabs2).toHaveLength(0);

const tabs3 = getTabs('datavisualizer', false);
expect(Array.isArray(tabs3)).toBeTruthy();
expect(tabs3).toHaveLength(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ interface Props {
tabId: TabId;
}

function getTabs(tabId: TabId, disableLinks: boolean): Tab[] {
const TAB_MAP: Record<TabId, Tab[]> = {
export function getTabs(tabId: TabId, disableLinks: boolean): Tab[] {
const TAB_MAP: Partial<Record<TabId, Tab[]>> = {
// overview: [],
datavisualizer: [],
data_frames: [],
Expand Down Expand Up @@ -55,7 +55,7 @@ function getTabs(tabId: TabId, disableLinks: boolean): Tab[] {
],
};

return TAB_MAP[tabId];
return TAB_MAP[tabId] || [];
}

enum TAB_TEST_SUBJECT {
Expand Down

0 comments on commit ec5d1a9

Please sign in to comment.