Skip to content

Commit d9f55cc

Browse files
committed
docs: Add new feature for read random article in documentazione
Add random route: 'angular.dev/random' for read random article Fixed angular#68038
1 parent 4a174b8 commit d9f55cc

5 files changed

Lines changed: 95 additions & 3 deletions

File tree

adev/src/app/core/constants/pages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const DEFAULT_PAGES = {
1414
TUTORIALS: 'tutorials',
1515
PLAYGROUND: 'playground',
1616
UPDATE: 'update-guide',
17+
RANDOM: 'random',
1718
} as const;
1819

1920
export const PAGE_PREFIX = {
@@ -25,4 +26,5 @@ export const PAGE_PREFIX = {
2526
REFERENCE: 'reference',
2627
TUTORIALS: 'tutorials',
2728
UPDATE: 'update-guide',
29+
RANDOM: 'random',
2830
} as const;

adev/src/app/features/random/random.scss

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {Router} from '@angular/router';
3+
import Random from './random';
4+
import {SUB_NAVIGATION_DATA} from '../../routing/sub-navigation-data';
5+
6+
describe('Random', () => {
7+
let component: Random;
8+
let fixture: ComponentFixture<Random>;
9+
let router: Router;
10+
11+
beforeEach(async () => {
12+
const routerSpy = jasmine.createSpyObj('Router', ['navigateByUrl']);
13+
14+
await TestBed.configureTestingModule({
15+
imports: [Random],
16+
providers: [{provide: Router, useValue: routerSpy}],
17+
}).compileComponents();
18+
19+
router = TestBed.inject(Router);
20+
fixture = TestBed.createComponent(Random);
21+
component = fixture.componentInstance;
22+
});
23+
24+
it('should create', () => {
25+
expect(component).toBeTruthy();
26+
});
27+
28+
it('should redirect to a random path on init', () => {
29+
fixture.detectChanges(); // Triggers ngOnInit
30+
31+
expect(router.navigateByUrl).toHaveBeenCalled();
32+
const call = (router.navigateByUrl as jasmine.Spy).calls.mostRecent();
33+
const navigatedUrl = call.args[0];
34+
const options = call.args[1];
35+
36+
expect(options.replaceUrl).toBeTrue();
37+
38+
// Check if navigatedUrl is one of the possible paths
39+
const allPaths = [
40+
...SUB_NAVIGATION_DATA.docs,
41+
...SUB_NAVIGATION_DATA.reference,
42+
...SUB_NAVIGATION_DATA.tutorials,
43+
].flatMap((item) => {
44+
const paths: string[] = [];
45+
const traverse = (node: any) => {
46+
if (node.path && !node.path.startsWith('http')) paths.push(`/${node.path}`);
47+
if (node.children) node.children.forEach(traverse);
48+
};
49+
traverse(item);
50+
return paths;
51+
});
52+
53+
expect(allPaths).toContain(navigatedUrl);
54+
});
55+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Component, inject, OnInit} from '@angular/core';
2+
import {Router} from '@angular/router';
3+
import {SUB_NAVIGATION_DATA} from '../../routing/sub-navigation-data';
4+
import {flatNavigationData} from '@angular/docs';
5+
6+
@Component({
7+
selector: 'adev-random',
8+
standalone: true,
9+
template: ``,
10+
})
11+
export default class Random implements OnInit {
12+
private readonly router = inject(Router);
13+
14+
ngOnInit(): void {
15+
const allItems = [
16+
...flatNavigationData(SUB_NAVIGATION_DATA.docs),
17+
...flatNavigationData(SUB_NAVIGATION_DATA.reference),
18+
...flatNavigationData(SUB_NAVIGATION_DATA.tutorials),
19+
];
20+
21+
const paths = allItems
22+
.map((item) => item.path)
23+
.filter((path): path is string => !!path && !path.startsWith('http'));
24+
25+
if (paths.length > 0) {
26+
const randomPath = paths[Math.floor(Math.random() * paths.length)];
27+
this.router.navigateByUrl(`/${randomPath}`, {replaceUrl: true});
28+
} else {
29+
this.router.navigateByUrl('/', {replaceUrl: true});
30+
}
31+
}
32+
}

adev/src/app/routing/routes.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ const cliReferencePageRoutes = mapNavigationItemsToRoutes(
5555
referenceNavigationItems.filter((r) => r.path?.startsWith(`${PAGE_PREFIX.CLI}/`)),
5656
{
5757
loadComponent: () =>
58-
import(
59-
'../features/references/cli-reference-details-page/cli-reference-details-page.component'
60-
),
58+
import('../features/references/cli-reference-details-page/cli-reference-details-page.component'),
6159
data: commonReferenceRouteData,
6260
},
6361
).map((route) => ({
@@ -148,6 +146,11 @@ export const routes: Route[] = [
148146
loadComponent: () => import('../features/playground/playground.component'),
149147
data: {...commonTutorialRouteData, label: 'Playground'},
150148
},
149+
{
150+
path: PAGE_PREFIX.RANDOM,
151+
loadComponent: () => import('../features/random/random'),
152+
data: {label: 'Random topic'},
153+
},
151154
...SUB_NAVIGATION_ROUTES,
152155
...API_REFERENCE_ROUTES,
153156
...FOOTER_ROUTES,

0 commit comments

Comments
 (0)