Skip to content

Commit 3cc0402

Browse files
authored
fix: common project should not depend on components project (#1264)
* fix: common project should not depend on components project
1 parent 7013af5 commit 3cc0402

File tree

7 files changed

+97
-60
lines changed

7 files changed

+97
-60
lines changed

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { Location } from '@angular/common';
22
import { Title } from '@angular/platform-browser';
33
import { Router, UrlSegment } from '@angular/router';
44
import { RouterTestingModule } from '@angular/router/testing';
5-
import { IconType } from '@hypertrace/assets-library';
65
import { APP_TITLE } from '@hypertrace/common';
7-
import { NavItemType } from '@hypertrace/components';
86
import { patchRouterNavigateForTest } from '@hypertrace/test-utils';
97
import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest';
108
import {
@@ -298,36 +296,6 @@ describe('Navigation Service', () => {
298296
}
299297
});
300298

301-
test('decorating navItem with features work as expected', () => {
302-
expect(
303-
spectator.service.decorateNavItem(
304-
{
305-
type: NavItemType.Header,
306-
label: 'Label'
307-
},
308-
spectator.service.getCurrentActivatedRoute()
309-
)
310-
).toEqual({ type: NavItemType.Header, label: 'Label' });
311-
312-
expect(
313-
spectator.service.decorateNavItem(
314-
{
315-
type: NavItemType.Link,
316-
label: 'Label',
317-
icon: IconType.None,
318-
matchPaths: ['root']
319-
},
320-
spectator.service.rootRoute()
321-
)
322-
).toEqual({
323-
type: NavItemType.Link,
324-
label: 'Label',
325-
icon: IconType.None,
326-
matchPaths: ['root'],
327-
features: ['test-feature']
328-
});
329-
});
330-
331299
test('setting title should work as expected', () => {
332300
router.navigate(['root', 'child']);
333301
expect(spectator.inject(Title).setTitle).toHaveBeenCalledWith('defaultAppTitle | child1');

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414
UrlSegment,
1515
UrlTree
1616
} from '@angular/router';
17-
import { NavItemConfig, NavItemType } from '@hypertrace/components';
18-
import { uniq } from 'lodash-es';
1917
import { from, Observable, of } from 'rxjs';
2018
import { distinctUntilChanged, filter, map, share, skip, startWith, switchMap, take, tap } from 'rxjs/operators';
2119
import { isEqualIgnoreFunctions, throwIfNil } from '../utilities/lang/lang-utils';
@@ -243,26 +241,6 @@ export class NavigationService {
243241
return this.findRouteConfig(path, childRoutes ? childRoutes : []);
244242
}
245243

246-
public decorateNavItem(navItem: NavItemConfig, activatedRoute: ActivatedRoute): NavItemConfig {
247-
if (navItem.type !== NavItemType.Link) {
248-
return { ...navItem };
249-
}
250-
const features = navItem.matchPaths
251-
.map(path => this.getRouteConfig([path], activatedRoute))
252-
.filter((maybeRoute): maybeRoute is HtRoute => maybeRoute !== undefined)
253-
.flatMap(route => this.getFeaturesForRoute(route))
254-
.concat(navItem.features || []);
255-
256-
return {
257-
...navItem,
258-
features: uniq(features)
259-
};
260-
}
261-
262-
private getFeaturesForRoute(route: HtRoute): string[] {
263-
return (route.data && route.data.features) || [];
264-
}
265-
266244
public rootRoute(): ActivatedRoute {
267245
return this.router.routerState.root;
268246
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { ActivatedRoute } from '@angular/router';
2+
import { IconType } from '@hypertrace/assets-library';
3+
import { NavigationService } from '@hypertrace/common';
4+
import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest';
5+
import { NavigationListService } from './navigation-list.service';
6+
import { NavItemType } from './navigation.config';
7+
8+
describe('Navigation List Service', () => {
9+
let spectator: SpectatorService<NavigationListService>;
10+
11+
const buildService = createServiceFactory({
12+
service: NavigationListService,
13+
providers: [
14+
mockProvider(ActivatedRoute),
15+
mockProvider(NavigationService, {
16+
getRouteConfig: jest.fn().mockReturnValue({
17+
path: 'root',
18+
data: { features: ['test-feature'] },
19+
children: []
20+
})
21+
})
22+
]
23+
});
24+
25+
beforeEach(() => {
26+
spectator = buildService();
27+
});
28+
29+
test('decorating navItem with features work as expected', () => {
30+
expect(
31+
spectator.service.decorateNavItem(
32+
{
33+
type: NavItemType.Header,
34+
label: 'Label'
35+
},
36+
spectator.inject(ActivatedRoute)
37+
)
38+
).toEqual({ type: NavItemType.Header, label: 'Label' });
39+
40+
expect(
41+
spectator.service.decorateNavItem(
42+
{
43+
type: NavItemType.Link,
44+
label: 'Label',
45+
icon: IconType.None,
46+
matchPaths: ['root']
47+
},
48+
spectator.inject(ActivatedRoute)
49+
)
50+
).toEqual({
51+
type: NavItemType.Link,
52+
label: 'Label',
53+
icon: IconType.None,
54+
matchPaths: ['root'],
55+
features: ['test-feature']
56+
});
57+
});
58+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRoute } from '@angular/router';
3+
import { HtRoute, NavigationService } from '@hypertrace/common';
4+
import { uniq } from 'lodash-es';
5+
import { NavItemConfig, NavItemType } from './navigation.config';
6+
7+
@Injectable({ providedIn: 'root' })
8+
export class NavigationListService {
9+
public constructor(private readonly navigationService: NavigationService) {}
10+
11+
public decorateNavItem(navItem: NavItemConfig, activatedRoute: ActivatedRoute): NavItemConfig {
12+
if (navItem.type !== NavItemType.Link) {
13+
return { ...navItem };
14+
}
15+
const features = navItem.matchPaths
16+
.map(path => this.navigationService.getRouteConfig([path], activatedRoute))
17+
.filter((maybeRoute): maybeRoute is HtRoute => maybeRoute !== undefined)
18+
.flatMap(route => this.getFeaturesForRoute(route))
19+
.concat(navItem.features || []);
20+
21+
return {
22+
...navItem,
23+
features: uniq(features)
24+
};
25+
}
26+
27+
private getFeaturesForRoute(route: HtRoute): string[] {
28+
return (route.data && route.data.features) || [];
29+
}
30+
}

projects/components/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export * from './navigation/navigation-list.module';
157157
export * from './navigation/nav-item/nav-item.component';
158158
export * from './navigation/navigation.config';
159159
export * from './navigation/navigation-list-component.service';
160+
export * from './navigation/navigation-list.service';
160161

161162
// Let async
162163
export { LetAsyncDirective } from './let-async/let-async.directive';

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ActivatedRoute } from '@angular/router';
22
import { NavigationService, PreferenceService } from '@hypertrace/common';
3-
import { LetAsyncModule, NavigationListComponent } from '@hypertrace/components';
3+
import { LetAsyncModule, NavigationListComponent, NavigationListService } from '@hypertrace/components';
44
import { createComponentFactory, mockProvider } from '@ngneat/spectator/jest';
55
import { MockComponent } from 'ng-mocks';
66
import { BehaviorSubject, of } from 'rxjs';
@@ -18,7 +18,9 @@ describe('NavigationComponent', () => {
1818
data: {
1919
features: ['example-feature']
2020
}
21-
}),
21+
})
22+
}),
23+
mockProvider(NavigationListService, {
2224
decorateNavItem: jest.fn().mockImplementation(navItem => ({ ...navItem, features: ['example-feature'] }))
2325
}),
2426
mockProvider(ActivatedRoute),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
33
import { IconType } from '@hypertrace/assets-library';
4-
import { NavigationService, PreferenceService } from '@hypertrace/common';
5-
import { NavItemConfig, NavItemType } from '@hypertrace/components';
4+
import { PreferenceService } from '@hypertrace/common';
5+
import { NavigationListService, NavItemConfig, NavItemType } from '@hypertrace/components';
66
import { ObservabilityIconType } from '@hypertrace/observability';
77
import { Observable } from 'rxjs';
88

@@ -74,12 +74,12 @@ export class NavigationComponent {
7474
];
7575

7676
public constructor(
77-
private readonly navigationService: NavigationService,
77+
private readonly navigationListService: NavigationListService,
7878
private readonly preferenceService: PreferenceService,
7979
private readonly activatedRoute: ActivatedRoute
8080
) {
8181
this.navItems = this.navItemDefinitions.map(definition =>
82-
this.navigationService.decorateNavItem(definition, this.activatedRoute)
82+
this.navigationListService.decorateNavItem(definition, this.activatedRoute)
8383
);
8484
this.isCollapsed$ = this.preferenceService.get(NavigationComponent.COLLAPSED_PREFERENCE, false);
8585
}

0 commit comments

Comments
 (0)