Skip to content

Commit

Permalink
Seperate special cases from logic && revise test
Browse files Browse the repository at this point in the history
Signed-off-by: Willie Hung <[email protected]>
  • Loading branch information
willie-hung committed Nov 14, 2023
1 parent 6dae6d1 commit d05929e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/core/public/chrome/ui/header/nav_link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import { ChromeNavLink } from '../../..';
import { httpServiceMock } from '../../../http/http_service.mock';

describe('isActiveNavLink', () => {
it('should return true if the currentId is "discover" and targetId is "discover"', () => {
it('should return true if the appId is "discover" and linkId is "discover"', () => {
expect(isActiveNavLink('discover', 'discover')).toBe(true);
});

it('should return true if the currentId is "data-explorer" and targetId is "data-explorer"', () => {
it('should return true if the appId is "data-explorer" and linkId is "data-explorer"', () => {
expect(isActiveNavLink('data-explorer', 'data-explorer')).toBe(true);
});

it('should return true if the currentId is "discover" and targetId is "data-explorer"', () => {
expect(isActiveNavLink('discover', 'data-explorer')).toBe(true);
it('should return true if the appId is "data-explorer" and linkId is "discover"', () => {
expect(isActiveNavLink('data-explorer', 'discover')).toBe(true);
});

it('should return false if the currentId and targetId do not match', () => {
it('should return false if the appId and linkId do not match', () => {
expect(isActiveNavLink('dashboard', 'discover')).toBe(false);
});
});
Expand Down
13 changes: 6 additions & 7 deletions src/core/public/chrome/ui/header/nav_link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ import { relativeToAbsolute } from '../../nav_links/to_nav_link';
export const isModifiedOrPrevented = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) =>
event.metaKey || event.altKey || event.ctrlKey || event.shiftKey || event.defaultPrevented;

// TODO: make this function more generic
export const isActiveNavLink = (currentId: string | undefined, targetId: string): boolean => {
if (currentId === 'discover' || currentId === 'data-explorer') {
return ['discover', 'data-explorer'].includes(targetId);
} else {
return currentId === targetId;
}
// TODO: replace hard-coded values with a registration function, so that apps can control active nav links similar to breadcrumbs
const aliasedApps: { [key: string]: string[] } = {
discover: ['data-explorer'],
};

export const isActiveNavLink = (appId: string | undefined, linkId: string) =>
appId === linkId || aliasedApps[linkId]?.includes(appId || '');

interface Props {
link: ChromeNavLink;
appId?: string;
Expand Down

0 comments on commit d05929e

Please sign in to comment.