import {HighlightOnNavigationDirective} from './highlight-on-navigation.directive'; import {ActivatedRoute, NavigationExtras, Router} from '@angular/router'; import {fakeAsync, tick} from '@angular/core/testing'; import {of} from 'rxjs'; import {ElementRef} from '@angular/core'; describe('HighlightOnNavigationDirective', () => { it('should create an instance', () => { const directive = new HighlightOnNavigationDirective({} as any, {} as any, {} as any); expect(directive).toBeTruthy(); }); it('scrolls to element if id matches url parameter', fakeAsync(() => { const nativeElement = {scrollIntoView : () => undefined, classList: {add: () => undefined, remove: () => undefined}}; const scrollSpy = spyOn(nativeElement, 'scrollIntoView'); const directive = new HighlightOnNavigationDirective( {nativeElement} as any as ElementRef, { queryParamMap: of({has: () => true, get: () => 'id'})} as any as ActivatedRoute, {navigate: () => Promise.resolve()} as any as Router ); directive.id = 'id'; directive.ngAfterViewInit(); tick(2000); // Wait for animations and such to finish expect(scrollSpy).toHaveBeenCalled(); })); it('does not scroll if id does not match', fakeAsync(() => { const nativeElement = {scrollIntoView : () => undefined, classList: {add: () => undefined, remove: () => undefined}}; const scrollSpy = spyOn(nativeElement, 'scrollIntoView'); const directive = new HighlightOnNavigationDirective( {nativeElement} as any as ElementRef, { queryParamMap: of({has: () => true, get: () => 'id'})} as any as ActivatedRoute, {navigate: () => Promise.resolve()} as any as Router ); directive.id = 'not_id'; directive.ngAfterViewInit(); tick(2000); // Wait for animations and such to finish expect(scrollSpy).not.toHaveBeenCalled(); })); it('does not scroll if no highlight parameter is present', fakeAsync(() => { const nativeElement = {scrollIntoView : () => undefined, classList: {add: () => undefined, remove: () => undefined}}; const scrollSpy = spyOn(nativeElement, 'scrollIntoView'); const directive = new HighlightOnNavigationDirective( {nativeElement} as any as ElementRef, { queryParamMap: of({has: () => false, get: () => undefined})} as any as ActivatedRoute, {navigate: () => Promise.resolve()} as any as Router ); directive.id = 'id'; directive.ngAfterViewInit(); tick(2000); // Wait for animations and such to finish expect(scrollSpy).not.toHaveBeenCalled(); })); it('removes highlightParam when scrolling', fakeAsync(() => { let queryParams: any = null; const directive = new HighlightOnNavigationDirective( { nativeElement: {scrollIntoView : () => undefined, classList: {add: () => undefined, remove: () => undefined}} } as any, { queryParamMap: of({has: () => true, get: () => 'id'}) } as any, {navigate: (commands: any[], extras: NavigationExtras) => { queryParams = extras?.queryParams; return Promise.resolve(); }} as any ); directive.id = 'id'; directive.ngAfterViewInit(); tick(2000); // Wait for animations and such to finish expect(queryParams).toBeDefined(); expect(queryParams?.highlight).toBeUndefined(); })); });