From ec888e5d7a8c4a763ec9ac745aaa8f70b502aca1 Mon Sep 17 00:00:00 2001 From: anandtiwary <52081890+anandtiwary@users.noreply.github.com> Date: Thu, 27 May 2021 20:54:10 -0700 Subject: [PATCH 1/3] fix: adding hyperlink capabilities to nav items and nav tabs --- .../nav-item/nav-item.component.test.ts | 107 ++++++++++++++++++ .../navigation/nav-item/nav-item.component.ts | 52 +++++---- .../navigation-list.component.test.ts | 53 +-------- .../navigation/navigation-list.component.ts | 8 +- .../src/navigation/navigation-list.module.ts | 5 +- .../navigable-tab-group.component.ts | 28 +++-- .../tabs/navigable/navigable-tab.module.ts | 12 +- 7 files changed, 177 insertions(+), 88 deletions(-) create mode 100644 projects/components/src/navigation/nav-item/nav-item.component.test.ts diff --git a/projects/components/src/navigation/nav-item/nav-item.component.test.ts b/projects/components/src/navigation/nav-item/nav-item.component.test.ts new file mode 100644 index 000000000..e2dd8b69a --- /dev/null +++ b/projects/components/src/navigation/nav-item/nav-item.component.test.ts @@ -0,0 +1,107 @@ +import { ActivatedRoute } from '@angular/router'; +import { IconType } from '@hypertrace/assets-library'; +import { + FeatureState, + FeatureStateResolver, + MemoizeModule, + NavigationParamsType, + NavigationService +} from '@hypertrace/common'; +import { BetaTagComponent, IconComponent, LinkComponent } from '@hypertrace/components'; +import { createHostFactory, mockProvider, SpectatorHost } from '@ngneat/spectator/jest'; +import { MockComponent } from 'ng-mocks'; +import { EMPTY, of } from 'rxjs'; +import { NavItemConfig, NavItemType } from '../navigation-list.component'; +import { FeatureConfigCheckModule } from './../../feature-check/feature-config-check.module'; +import { NavItemComponent } from './nav-item.component'; + +describe('Navigation Item Component', () => { + let spectator: SpectatorHost; + const activatedRoute = { + root: {} + }; + const createHost = createHostFactory({ + shallow: true, + component: NavItemComponent, + declarations: [MockComponent(IconComponent), MockComponent(LinkComponent), MockComponent(BetaTagComponent)], + imports: [MemoizeModule, FeatureConfigCheckModule], + providers: [ + mockProvider(ActivatedRoute, activatedRoute), + mockProvider(NavigationService, { + navigation$: EMPTY, + navigateWithinApp: jest.fn(), + getCurrentActivatedRoute: jest.fn().mockReturnValue(of(activatedRoute)) + }), + mockProvider(FeatureStateResolver, { + getCombinedFeatureState: () => of(FeatureState.Enabled) + }) + ] + }); + + test('should update layout when collapsed input is updated', () => { + const navItem: NavItemConfig = { + type: NavItemType.Link, + icon: IconType.TriangleLeft, + label: 'Foo Label', + matchPaths: ['foo', 'bar'] + }; + spectator = createHost(``, { + hostProps: { navItem: navItem } + }); + + expect(spectator.query('.label')).not.toExist(); + expect(spectator.query(IconComponent)?.icon).toEqual(IconType.TriangleLeft); + + spectator.setInput({ + collapsed: false + }); + + spectator.detectChanges(); + expect(spectator.query('.label')).toExist(); + expect(spectator.query(IconComponent)?.icon).toEqual(IconType.TriangleLeft); + }); + + test('should navigate to first match on click, relative to activated route', () => { + const navItem: NavItemConfig = { + type: NavItemType.Link, + icon: 'icon', + label: 'Foo Label', + matchPaths: ['foo', 'bar'] + }; + spectator = createHost(``, { + hostProps: { navItem: navItem } + }); + + const link = spectator.query(LinkComponent); + expect(link).toExist(); + expect(link?.paramsOrUrl).toEqual({ + navType: NavigationParamsType.InApp, + path: 'foo', + relativeTo: spectator.inject(ActivatedRoute), + replaceCurrentHistory: undefined + }); + }); + + test('should navigate to first match on click, relative to activated route with skip location change option', () => { + const navItem: NavItemConfig = { + type: NavItemType.Link, + icon: 'icon', + label: 'Foo Label', + matchPaths: ['foo', 'bar'], + replaceCurrentHistory: true + }; + + spectator = createHost(``, { + hostProps: { navItem: navItem } + }); + + const link = spectator.query(LinkComponent); + expect(link).toExist(); + expect(link?.paramsOrUrl).toEqual({ + navType: NavigationParamsType.InApp, + path: 'foo', + relativeTo: spectator.inject(ActivatedRoute), + replaceCurrentHistory: true + }); + }); +}); diff --git a/projects/components/src/navigation/nav-item/nav-item.component.ts b/projects/components/src/navigation/nav-item/nav-item.component.ts index c5cc76a87..4c3584b3d 100644 --- a/projects/components/src/navigation/nav-item/nav-item.component.ts +++ b/projects/components/src/navigation/nav-item/nav-item.component.ts @@ -1,5 +1,6 @@ import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; -import { FeatureState } from '@hypertrace/common'; +import { ActivatedRoute } from '@angular/router'; +import { FeatureState, NavigationParams, NavigationParamsType } from '@hypertrace/common'; import { IconSize } from '../../icon/icon-size'; import { NavItemLinkConfig } from '../navigation-list.component'; @@ -8,28 +9,30 @@ import { NavItemLinkConfig } from '../navigation-list.component'; styleUrls: ['./nav-item.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, template: ` -