diff --git a/lib/core/shell/src/lib/components/shell/shell.component.spec.ts b/lib/core/shell/src/lib/components/shell/shell.component.spec.ts index 85e8e59d0e..6278b90ef5 100644 --- a/lib/core/shell/src/lib/components/shell/shell.component.spec.ts +++ b/lib/core/shell/src/lib/components/shell/shell.component.spec.ts @@ -18,29 +18,19 @@ import { TestBed, ComponentFixture } from '@angular/core/testing'; import { AppConfigService } from '@alfresco/adf-core'; import { ShellLayoutComponent } from './shell.component'; -import { Router, NavigationStart, RouterModule } from '@angular/router'; -import { of, Subject } from 'rxjs'; +import { provideRouter, RouterModule } from '@angular/router'; +import { of } from 'rxjs'; import { ShellAppService, SHELL_APP_SERVICE } from '../../services/shell-app.service'; - -class MockRouter { - private url = 'some-url'; - private subject = new Subject(); - events = this.subject.asObservable(); - routerState = { snapshot: { url: this.url } }; - - navigateByUrl(url: string) { - const navigationStart = new NavigationStart(0, url); - this.subject.next(navigationStart); - } -} +import { RouterTestingHarness } from '@angular/router/testing'; describe('AppLayoutComponent', () => { let fixture: ComponentFixture; let component: ShellLayoutComponent; let appConfig: AppConfigService; let shellAppService: ShellAppService; + let routerHarness: RouterTestingHarness; - beforeEach(() => { + beforeEach(async () => { const shellService: ShellAppService = { pageHeading$: of('Title'), hideSidenavConditions: [], @@ -54,14 +44,11 @@ describe('AppLayoutComponent', () => { TestBed.configureTestingModule({ imports: [RouterModule.forChild([]), ShellLayoutComponent], providers: [ - { - provide: Router, - useClass: MockRouter - }, { provide: SHELL_APP_SERVICE, useValue: shellService - } + }, + provideRouter([{ path: 'minimize', component: ShellLayoutComponent }]) ] }); @@ -69,6 +56,7 @@ describe('AppLayoutComponent', () => { component = fixture.componentInstance; appConfig = TestBed.inject(AppConfigService); shellAppService = TestBed.inject(SHELL_APP_SERVICE); + routerHarness = await RouterTestingHarness.create(); }); beforeEach(() => { @@ -163,4 +151,43 @@ describe('AppLayoutComponent', () => { expect(component.layout.container.toggleMenu).toHaveBeenCalled(); }); + + it('should minimize menu when navigates to minimize url', async () => { + shellAppService.minimizeSidenavConditions = ['/minimize']; + component.layout.container = { + isMobileScreenSize: false, + isMenuMinimized: false, + toggleMenu: () => {} + }; + + spyOn(component.layout.container, 'toggleMenu'); + + fixture.detectChanges(); + await routerHarness.navigateByUrl('/minimize'); + fixture.detectChanges(); + + expect(component.minimizeSidenav).toBeTrue(); + expect(component.layout.isMenuMinimized).toBeTrue(); + expect(component.layout.container.toggleMenu).toHaveBeenCalled(); + }); + + it('should not minimize menu again when previous and current route contain minimize url', async () => { + shellAppService.minimizeSidenavConditions = ['/minimize']; + component.layout.container = { + isMobileScreenSize: false, + isMenuMinimized: false, + toggleMenu: () => {} + }; + + fixture.detectChanges(); + await routerHarness.navigateByUrl('/minimize?query=123'); + fixture.detectChanges(); + + expect(component.minimizeSidenav).toBeTrue(); + + await routerHarness.navigateByUrl('/minimize?query=456'); + fixture.detectChanges(); + + expect(component.minimizeSidenav).toBeFalse(); + }); }); diff --git a/lib/core/shell/src/lib/components/shell/shell.component.ts b/lib/core/shell/src/lib/components/shell/shell.component.ts index 94998d5288..4ad1d913b4 100644 --- a/lib/core/shell/src/lib/components/shell/shell.component.ts +++ b/lib/core/shell/src/lib/components/shell/shell.component.ts @@ -102,7 +102,11 @@ export class ShellLayoutComponent implements OnInit { takeUntilDestroyed(this.destroyRef) ) .subscribe((event: NavigationEnd) => { - this.minimizeSidenav = this.shellService.minimizeSidenavConditions.some((el) => event.urlAfterRedirects.includes(el)); + this.minimizeSidenav = this.shellService.minimizeSidenavConditions.some((el) => { + const previousNavigation = this.router.getCurrentNavigation()?.previousNavigation?.finalUrl; + const previousPath = previousNavigation ? this.router.serializeUrl(previousNavigation) : ''; + return event.urlAfterRedirects.includes(el) && !previousPath.includes(el); + }); this.hideSidenav = this.shellService.hideSidenavConditions.some((el) => event.urlAfterRedirects.includes(el)); this.updateState();