Skip to content

Commit 3a5cde0

Browse files
committed
test: cover RouterHistoryStore
1 parent 114bd80 commit 3a5cde0

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { AsyncPipe, Location, NgIf } from '@angular/common';
2+
import { Component, inject, NgZone } from '@angular/core';
3+
import { TestBed } from '@angular/core/testing';
4+
import { By } from '@angular/platform-browser';
5+
import { Router, RouterLink, RouterOutlet } from '@angular/router';
6+
import { RouterTestingModule } from '@angular/router/testing';
7+
import {
8+
provideRouterHistoryStore,
9+
RouterHistoryStore,
10+
} from './router-history.store';
11+
12+
function createTestComponent(name: string, selector: string) {
13+
@Component({ standalone: true, selector, template: name })
14+
class TestComponent {}
15+
16+
return TestComponent;
17+
}
18+
19+
@Component({
20+
standalone: true,
21+
selector: 'ngw-test-app',
22+
imports: [AsyncPipe, NgIf, RouterLink, RouterOutlet],
23+
template: `
24+
<a
25+
id="back-link"
26+
*ngIf="routerHistory.previousUrl$ | async as previousUrl"
27+
(click)="onBack()"
28+
>Back</a
29+
>
30+
31+
<a id="home-link" routerLink="/">Home</a>
32+
<a id="about-link" routerLink="about">About</a>
33+
<a id="company-link" routerLink="company">Company</a>
34+
<a id="products-link" routerLink="products">Products</a>
35+
36+
<router-outlet></router-outlet>
37+
`,
38+
})
39+
class TestAppComponent {
40+
#location = inject(Location);
41+
42+
protected routerHistory = inject(RouterHistoryStore);
43+
44+
onBack() {
45+
this.#location.back();
46+
}
47+
}
48+
49+
describe(RouterHistoryStore.name, () => {
50+
async function setup() {
51+
TestBed.configureTestingModule({
52+
imports: [
53+
TestAppComponent,
54+
RouterTestingModule.withRoutes([
55+
{ path: '', pathMatch: 'full', redirectTo: 'home' },
56+
{
57+
path: 'home',
58+
component: createTestComponent('HomeComponent', 'test-home'),
59+
},
60+
{
61+
path: 'about',
62+
component: createTestComponent('AboutComponent', 'test-about'),
63+
},
64+
{
65+
path: 'company',
66+
component: createTestComponent('CompanyComponent', 'test-company'),
67+
},
68+
{
69+
path: 'products',
70+
component: createTestComponent(
71+
'ProductsComponent',
72+
'test-products'
73+
),
74+
},
75+
]),
76+
],
77+
providers: [provideRouterHistoryStore()],
78+
});
79+
80+
const rootFixture = TestBed.createComponent(TestAppComponent);
81+
const router = TestBed.inject(Router);
82+
const ngZone = TestBed.inject(NgZone);
83+
const routerHistory = TestBed.inject(RouterHistoryStore);
84+
85+
rootFixture.autoDetectChanges();
86+
ngZone.run(() => router.initialNavigation());
87+
88+
return {
89+
async click(selector: string) {
90+
const link = rootFixture.debugElement.query(By.css(selector))
91+
.nativeElement as HTMLElement;
92+
ngZone.run(() => link.click());
93+
await rootFixture.whenStable();
94+
},
95+
routerHistory,
96+
};
97+
}
98+
99+
it('the URLs behave like the History API when navigating back', async () => {
100+
const { click, routerHistory } = await setup();
101+
let currentUrl: string | undefined;
102+
routerHistory.currentUrl$.subscribe((url) => {
103+
currentUrl = url;
104+
});
105+
let previousUrl: string | null | undefined;
106+
routerHistory.previousUrl$.subscribe((url) => {
107+
previousUrl = url;
108+
});
109+
110+
// At Home
111+
await click('#about-link');
112+
// At About
113+
await click('#company-link');
114+
// At Company
115+
await click('#back-link');
116+
// At About
117+
118+
expect(currentUrl).toBe('/about');
119+
expect(previousUrl).toBe('/home');
120+
});
121+
});

0 commit comments

Comments
 (0)