Skip to content

Commit 94a0439

Browse files
authored
feat: move a common method into navigation service (#969)
* feat: move a common method into navigation service
1 parent 71f8c34 commit 94a0439

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

projects/common/src/navigation/navigation.service.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Location } from '@angular/common';
22
import { Router, UrlSegment } from '@angular/router';
33
import { RouterTestingModule } from '@angular/router/testing';
4+
import { IconType } from '@hypertrace/assets-library';
5+
import { NavItemType } from '@hypertrace/components';
46
import { patchRouterNavigateForTest } from '@hypertrace/test-utils';
57
import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest';
68
import {
@@ -47,6 +49,7 @@ describe('Navigation Service', () => {
4749
RouterTestingModule.withRoutes([
4850
{
4951
path: 'root',
52+
data: { features: ['test-feature'] },
5053
children: [firstChildRouteConfig, secondChildRouteConfig]
5154
}
5255
])
@@ -285,4 +288,34 @@ describe('Navigation Service', () => {
285288
);
286289
}
287290
});
291+
292+
test('decorating navItem with features work as expected', () => {
293+
expect(
294+
spectator.service.decorateNavItem(
295+
{
296+
type: NavItemType.Header,
297+
label: 'Label'
298+
},
299+
spectator.service.getCurrentActivatedRoute()
300+
)
301+
).toEqual({ type: NavItemType.Header, label: 'Label' });
302+
303+
expect(
304+
spectator.service.decorateNavItem(
305+
{
306+
type: NavItemType.Link,
307+
label: 'Label',
308+
icon: IconType.None,
309+
matchPaths: ['root']
310+
},
311+
spectator.service.rootRoute()
312+
)
313+
).toEqual({
314+
type: NavItemType.Link,
315+
label: 'Label',
316+
icon: IconType.None,
317+
matchPaths: ['root'],
318+
features: ['test-feature']
319+
});
320+
});
288321
});

projects/common/src/navigation/navigation.service.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
UrlSegment,
1414
UrlTree
1515
} from '@angular/router';
16+
import { NavItemConfig, NavItemType } from '@hypertrace/components';
17+
import { uniq } from 'lodash-es';
1618
import { from, Observable, of } from 'rxjs';
1719
import { distinctUntilChanged, filter, map, share, skip, startWith, switchMap, take } from 'rxjs/operators';
1820
import { isEqualIgnoreFunctions, throwIfNil } from '../utilities/lang/lang-utils';
@@ -229,6 +231,26 @@ export class NavigationService {
229231
return this.findRouteConfig(path, childRoutes ? childRoutes : []);
230232
}
231233

234+
public decorateNavItem(navItem: NavItemConfig, activatedRoute: ActivatedRoute): NavItemConfig {
235+
if (navItem.type !== NavItemType.Link) {
236+
return { ...navItem };
237+
}
238+
const features = navItem.matchPaths
239+
.map(path => this.getRouteConfig([path], activatedRoute))
240+
.filter((maybeRoute): maybeRoute is HtRoute => maybeRoute !== undefined)
241+
.flatMap(route => this.getFeaturesForRoute(route))
242+
.concat(navItem.features || []);
243+
244+
return {
245+
...navItem,
246+
features: uniq(features)
247+
};
248+
}
249+
250+
private getFeaturesForRoute(route: HtRoute): string[] {
251+
return (route.data && route.data.features) || [];
252+
}
253+
232254
public rootRoute(): ActivatedRoute {
233255
return this.router.routerState.root;
234256
}

src/app/shared/navigation/navigation.component.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ describe('NavigationComponent', () => {
1818
data: {
1919
features: ['example-feature']
2020
}
21-
})
21+
}),
22+
decorateNavItem: jest.fn().mockImplementation(navItem => ({ ...navItem, features: ['example-feature'] }))
2223
}),
2324
mockProvider(ActivatedRoute),
2425
mockProvider(PreferenceService, { get: jest.fn().mockReturnValue(of(false)) })

src/app/shared/navigation/navigation.component.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
33
import { IconType } from '@hypertrace/assets-library';
4-
import { HtRoute, NavigationService, PreferenceService } from '@hypertrace/common';
4+
import { NavigationService, PreferenceService } from '@hypertrace/common';
55
import { NavItemConfig, NavItemType } from '@hypertrace/components';
66
import { ObservabilityIconType } from '@hypertrace/observability';
7-
import { uniq } from 'lodash-es';
87
import { Observable } from 'rxjs';
98

109
@Component({
@@ -79,31 +78,13 @@ export class NavigationComponent {
7978
private readonly preferenceService: PreferenceService,
8079
private readonly activatedRoute: ActivatedRoute
8180
) {
82-
this.navItems = this.navItemDefinitions.map(definition => this.decorateNavItem(definition));
81+
this.navItems = this.navItemDefinitions.map(definition =>
82+
this.navigationService.decorateNavItem(definition, this.activatedRoute)
83+
);
8384
this.isCollapsed$ = this.preferenceService.get(NavigationComponent.COLLAPSED_PREFERENCE, false);
8485
}
8586

8687
public onViewToggle(collapsed: boolean): void {
8788
this.preferenceService.set(NavigationComponent.COLLAPSED_PREFERENCE, collapsed);
8889
}
89-
90-
private decorateNavItem(navItem: NavItemConfig): NavItemConfig {
91-
if (navItem.type !== NavItemType.Link) {
92-
return { ...navItem };
93-
}
94-
const features = navItem.matchPaths
95-
.map(path => this.navigationService.getRouteConfig([path], this.activatedRoute))
96-
.filter((maybeRoute): maybeRoute is HtRoute => maybeRoute !== undefined)
97-
.flatMap(route => this.getFeaturesForRoute(route))
98-
.concat(navItem.features || []);
99-
100-
return {
101-
...navItem,
102-
features: uniq(features)
103-
};
104-
}
105-
106-
private getFeaturesForRoute(route: HtRoute): string[] {
107-
return (route.data && route.data.features) || [];
108-
}
10990
}

0 commit comments

Comments
 (0)