From 4d83af1fe4a5428a798d5f370e0b8b032503cb8a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 21 Oct 2026 11:43:11 +0000 Subject: [PATCH 1/3] fix: scroll to top when changing routes (closes #15) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The standalone router never applied RoutingModule's scroll restoration, so SPA navigations kept the previous viewport offset. Restore to top on NavigationEnd and align router config with that behavior. Co-authored-by: ✅ Daniel Danielecki --- e2e/scroll.spec.ts | 45 ++++++++++++++++++++++ src/app/app.component.spec.ts | 40 +++++++++++++++++++ src/app/app.component.ts | 15 +++++++- src/app/app.config.spec.ts | 16 ++++++++ src/app/app.config.ts | 7 +++- src/app/routing/app-routing.module.spec.ts | 14 +++++++ src/app/routing/app-routing.module.ts | 2 +- 7 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 e2e/scroll.spec.ts create mode 100644 src/app/app.config.spec.ts diff --git a/e2e/scroll.spec.ts b/e2e/scroll.spec.ts new file mode 100644 index 0000000..b666248 --- /dev/null +++ b/e2e/scroll.spec.ts @@ -0,0 +1,45 @@ +import { expect, test } from '@playwright/test'; + +test('scrolls to top when changing routes', async ({ page }) => { + await page.goto('/'); + await expect(page.locator('app-root')).toBeVisible({ timeout: 15000 }); + + await page.addStyleTag({ + content: 'html, body, main { min-height: 4000px !important; }', + }); + await page.evaluate(() => { + const y = Math.max( + document.body.scrollHeight, + document.documentElement.scrollHeight + ); + window.scrollTo(0, y); + document.documentElement.scrollTop = y; + document.body.scrollTop = y; + }); + + const scrolled = await page.evaluate( + () => + window.scrollY || + document.documentElement.scrollTop || + document.body.scrollTop + ); + expect(scrolled).toBeGreaterThan(100); + + await page.locator('.desktop-navigation a[title="About us"]').click(); + await expect(page).toHaveURL(/\/about-us$/); + await expect(page.locator('mat-card-title[title="About us"]')).toBeVisible({ + timeout: 15000, + }); + + await expect + .poll( + async () => + page.evaluate( + () => + window.scrollY || + document.documentElement.scrollTop || + document.body.scrollTop + ) + ) + .toBe(0); +}); diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index aef8314..d9cbe04 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -5,7 +5,9 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { Meta, Title } from '@angular/platform-browser'; import { HomeModule } from './home/home.module'; import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { NavigationEnd, Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; +import { Subject } from 'rxjs'; describe('AppComponent', () => { let app: AppComponent; @@ -58,3 +60,41 @@ describe('AppComponent', () => { expect(compiled.querySelector('app-footer')).not.toBeNull(); }); }); + +describe('AppComponent scroll on route change', () => { + const routerEvents = new Subject(); + let fixture: ComponentFixture; + + beforeEach(waitForAsync(() => { + TestBed.configureTestingModule({ + imports: [ + AppComponent, + BrowserAnimationsModule, + HomeModule, + HttpClientTestingModule, + ], + providers: [ + Title, + Meta, + { provide: Router, useValue: { events: routerEvents } }, + ], + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + }); + + it('should scroll the viewport to the top after navigation', () => { + const scrollToSpy = spyOn(window, 'scrollTo'); + document.documentElement.scrollTop = 800; + document.body.scrollTop = 800; + + routerEvents.next(new NavigationEnd(1, '/about-us', '/about-us')); + + expect(scrollToSpy).toHaveBeenCalledWith(0, 0); + expect(document.documentElement.scrollTop).toBe(0); + expect(document.body.scrollTop).toBe(0); + }); +}); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 40ee4a0..9dca92b 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,6 @@ import { animate, style, transition, trigger } from '@angular/animations'; -import { Component, inject } from '@angular/core'; +import { isPlatformBrowser } from '@angular/common'; +import { Component, PLATFORM_ID, inject } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { NavigationEnd, Router, RouterOutlet } from '@angular/router'; import { filter } from 'rxjs/operators'; @@ -31,6 +32,7 @@ const FADE_ANIMATION = export class AppComponent { private readonly router = inject(Router); private readonly seoService = inject(SeoService); + private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID)); constructor() { this.router.events @@ -40,9 +42,20 @@ export class AppComponent { ) .subscribe((event) => { this.seoService.updateForUrl(event.urlAfterRedirects); + this.scrollToTop(); }); } + /** html/body height:100% + overflow-x:hidden can make body the scroller, not window. */ + private scrollToTop(): void { + if (!this.isBrowser) { + return; + } + window.scrollTo(0, 0); + document.documentElement.scrollTop = 0; + document.body.scrollTop = 0; + } + public getRouterOutletState(routerOutlet: RouterOutlet): string { const routeData = routerOutlet.activatedRouteData['animation']; return routeData ? routeData : 'rootPage'; diff --git a/src/app/app.config.spec.ts b/src/app/app.config.spec.ts new file mode 100644 index 0000000..dec753a --- /dev/null +++ b/src/app/app.config.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; +import { ROUTER_CONFIGURATION } from '@angular/router'; +import { appConfig } from './app.config'; + +describe('appConfig', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [...appConfig.providers], + }); + }); + + it('scrolls to the top on navigation', () => { + const config = TestBed.inject(ROUTER_CONFIGURATION); + expect(config.scrollPositionRestoration).toBe('top'); + }); +}); diff --git a/src/app/app.config.ts b/src/app/app.config.ts index a0321fc..b2eab4d 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,5 +1,5 @@ import { ApplicationConfig, importProvidersFrom } from '@angular/core'; -import { provideRouter } from '@angular/router'; +import { provideRouter, withInMemoryScrolling } from '@angular/router'; import { provideAnimations } from '@angular/platform-browser/animations'; import { provideHttpClient, withJsonpSupport } from '@angular/common/http'; import { AngularFireModule } from '@angular/fire/compat'; @@ -23,7 +23,10 @@ const isFirebaseConfigured = !!firebaseConfig.appId; const baseProviders = [ - provideRouter(routes), + provideRouter( + routes, + withInMemoryScrolling({ scrollPositionRestoration: 'top' }) + ), provideAnimations(), provideHttpClient(withJsonpSupport()), { diff --git a/src/app/routing/app-routing.module.spec.ts b/src/app/routing/app-routing.module.spec.ts index e0373fc..0a5c5c3 100644 --- a/src/app/routing/app-routing.module.spec.ts +++ b/src/app/routing/app-routing.module.spec.ts @@ -1,5 +1,6 @@ import { TestBed } from "@angular/core/testing"; import { HttpClientTestingModule } from "@angular/common/http/testing"; +import { ROUTER_CONFIGURATION } from "@angular/router"; import { RoutingModule } from "./app-routing.module"; import { RouterTestingModule } from "@angular/router/testing"; @@ -14,3 +15,16 @@ describe("RoutingModule", () => { expect(RoutingModule).toBeDefined(); }); }); + +describe("RoutingModule scrollPositionRestoration", () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [RoutingModule], + }); + }); + + it("scrolls to the top on navigation", () => { + const config = TestBed.inject(ROUTER_CONFIGURATION); + expect(config.scrollPositionRestoration).toBe("top"); + }); +}); diff --git a/src/app/routing/app-routing.module.ts b/src/app/routing/app-routing.module.ts index 5875828..457a369 100644 --- a/src/app/routing/app-routing.module.ts +++ b/src/app/routing/app-routing.module.ts @@ -5,7 +5,7 @@ import { routes } from '../app.routes'; @NgModule({ exports: [RouterModule], imports: [ - RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' }), + RouterModule.forRoot(routes, { scrollPositionRestoration: 'top' }), ], }) export class RoutingModule {} From a7f5a90d3437c088f82a8e044c8465d64edcc495 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 21 Oct 2026 11:47:34 +0000 Subject: [PATCH 2/3] test: navigate with RouterTestingModule for scroll-to-top spec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Standalone withInMemoryScrolling does not populate ROUTER_CONFIGURATION, so drop that assertion and drive the AppComponent test through a real navigation. Co-authored-by: ✅ Daniel Danielecki --- src/app/app.component.spec.ts | 44 +++++++++++------------------------ src/app/app.config.spec.ts | 16 ------------- 2 files changed, 13 insertions(+), 47 deletions(-) delete mode 100644 src/app/app.config.spec.ts diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index d9cbe04..9eadc88 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -1,13 +1,16 @@ import 'hammerjs'; +import { Component } from '@angular/core'; import { AppComponent } from './app.component'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { Meta, Title } from '@angular/platform-browser'; import { HomeModule } from './home/home.module'; import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { NavigationEnd, Router } from '@angular/router'; +import { Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; -import { Subject } from 'rxjs'; + +@Component({ standalone: true, template: '' }) +class BlankStubComponent {} describe('AppComponent', () => { let app: AppComponent; @@ -20,7 +23,10 @@ describe('AppComponent', () => { BrowserAnimationsModule, HomeModule, HttpClientTestingModule, - RouterTestingModule, + RouterTestingModule.withRoutes([ + { path: '', component: BlankStubComponent }, + { path: 'about-us', component: BlankStubComponent }, + ]), ], providers: [Title, Meta] }).compileComponents(); @@ -59,39 +65,15 @@ describe('AppComponent', () => { const compiled = fixture.debugElement.nativeElement; expect(compiled.querySelector('app-footer')).not.toBeNull(); }); -}); - -describe('AppComponent scroll on route change', () => { - const routerEvents = new Subject(); - let fixture: ComponentFixture; - - beforeEach(waitForAsync(() => { - TestBed.configureTestingModule({ - imports: [ - AppComponent, - BrowserAnimationsModule, - HomeModule, - HttpClientTestingModule, - ], - providers: [ - Title, - Meta, - { provide: Router, useValue: { events: routerEvents } }, - ], - }).compileComponents(); - })); - - beforeEach(() => { - fixture = TestBed.createComponent(AppComponent); - fixture.detectChanges(); - }); - it('should scroll the viewport to the top after navigation', () => { + it('should scroll the viewport to the top after navigation', async () => { + const router = TestBed.inject(Router); const scrollToSpy = spyOn(window, 'scrollTo'); document.documentElement.scrollTop = 800; document.body.scrollTop = 800; - routerEvents.next(new NavigationEnd(1, '/about-us', '/about-us')); + await fixture.ngZone!.run(() => router.navigateByUrl('/about-us')); + await fixture.whenStable(); expect(scrollToSpy).toHaveBeenCalledWith(0, 0); expect(document.documentElement.scrollTop).toBe(0); diff --git a/src/app/app.config.spec.ts b/src/app/app.config.spec.ts deleted file mode 100644 index dec753a..0000000 --- a/src/app/app.config.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { ROUTER_CONFIGURATION } from '@angular/router'; -import { appConfig } from './app.config'; - -describe('appConfig', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - providers: [...appConfig.providers], - }); - }); - - it('scrolls to the top on navigation', () => { - const config = TestBed.inject(ROUTER_CONFIGURATION); - expect(config.scrollPositionRestoration).toBe('top'); - }); -}); From d275c340ec362896d35ac659f6d713e5447a0321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=85=20Daniel=20Danielecki?= Date: Tue, 17 Nov 2026 09:27:03 +0100 Subject: [PATCH 3/3] bump(release): bump version to 2.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 140199f..1db72dc 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "homepage": "https://github.com/Ditectrev/ditectrev.com", "name": "ditectrev", "url": "https://ditectrev.com", - "version": "2.1.0", + "version": "2.1.1", "packageManager": "pnpm@9.15.0", "scripts": { "prepare:browser-env": "node ./generate-browser-env.mjs",