diff --git a/adev/shared-docs/utils/BUILD.bazel b/adev/shared-docs/utils/BUILD.bazel index 2e3c29dafb1..744090010e3 100644 --- a/adev/shared-docs/utils/BUILD.bazel +++ b/adev/shared-docs/utils/BUILD.bazel @@ -1,4 +1,4 @@ -load("//adev/shared-docs:defaults.bzl", "ts_project") +load("//adev/shared-docs:defaults.bzl", "ts_project", "zoneless_web_test_suite") package(default_visibility = ["//visibility:private"]) @@ -21,3 +21,20 @@ ts_project( "//adev/shared-docs/providers", ], ) + +ts_project( + name = "test_lib", + testonly = True, + srcs = glob( + ["*.spec.ts"], + ), + deps = [ + ":utils", + "//adev/shared-docs/interfaces", + ], +) + +zoneless_web_test_suite( + name = "test", + deps = [":test_lib"], +) diff --git a/adev/shared-docs/utils/navigation.utils.spec.ts b/adev/shared-docs/utils/navigation.utils.spec.ts new file mode 100644 index 00000000000..773ab4b585c --- /dev/null +++ b/adev/shared-docs/utils/navigation.utils.spec.ts @@ -0,0 +1,54 @@ +/*! + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import {NavigationItem} from '../interfaces/index'; +import {isExternalLink, mapNavigationItemsToRoutes} from './navigation.utils'; + +describe('navigation.utils', () => { + describe('isExternalLink', () => { + it('should return true for http and https links', () => { + expect(isExternalLink('http://example.com')).toBeTrue(); + expect(isExternalLink('https://example.com')).toBeTrue(); + expect(isExternalLink('https://github.com/angular/angularfire#readme')).toBeTrue(); + }); + + it('should return false for internal relative or absolute paths', () => { + expect(isExternalLink('guide/signals')).toBeFalse(); + expect(isExternalLink('/guide/signals')).toBeFalse(); + expect(isExternalLink('overview')).toBeFalse(); + }); + }); + + describe('mapNavigationItemsToRoutes', () => { + it('should map internal navigation items to routes and ignore external links', () => { + const items: NavigationItem[] = [ + { + label: 'Overview', + path: 'overview', + }, + { + label: 'AngularFire', + path: 'https://github.com/angular/angularfire#readme', + }, + { + label: 'Google Maps', + path: 'https://github.com/angular/components/tree/main/src/google-maps#readme', + }, + { + label: 'Guide', + path: 'guide/components', + }, + ]; + + const routes = mapNavigationItemsToRoutes(items, {}); + + expect(routes.length).toBe(2); + expect(routes.map((r) => r.path)).toEqual(['overview', 'guide/components']); + }); + }); +}); diff --git a/adev/shared-docs/utils/navigation.utils.ts b/adev/shared-docs/utils/navigation.utils.ts index 17eb62f5c5a..72ba93fb2eb 100644 --- a/adev/shared-docs/utils/navigation.utils.ts +++ b/adev/shared-docs/utils/navigation.utils.ts @@ -96,7 +96,10 @@ export const mapNavigationItemsToRoutes = ( additionalRouteProperties: Partial, ): Route[] => navigationItems - .filter((route): route is NavigationItem & {path: string} => Boolean(route.path)) + .filter( + (route): route is NavigationItem & {path: string} => + Boolean(route.path) && !isExternalLink(route.path!), + ) .map((navigationItem) => { const route = { path: navigationItem.path,