From 6760826f5f1ac6106ab3f8e35565cdd916dc7f78 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Thu, 11 Jul 2019 16:51:08 +0300 Subject: [PATCH 1/2] fix(menu): add fragment to path passed to fragment comparision functions --- src/framework/theme/components/menu/menu.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/menu/menu.service.ts b/src/framework/theme/components/menu/menu.service.ts index e50babb0a3..421513ec6c 100644 --- a/src/framework/theme/components/menu/menu.service.ts +++ b/src/framework/theme/components/menu/menu.service.ts @@ -384,8 +384,8 @@ export class NbMenuInternalService { if (isSelectedInPath && item.fragment != null) { return exact - ? isFragmentEqual(this.location.path(), item.fragment) - : isFragmentContain(this.location.path(), item.fragment); + ? isFragmentEqual(this.location.path(true), item.fragment) + : isFragmentContain(this.location.path(true), item.fragment); } return isSelectedInPath; From 39f7e8f919923f89c12468f5493a2bc269bd4ad4 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Fri, 12 Jul 2019 13:24:40 +0300 Subject: [PATCH 2/2] test(menu): make SpyLocation.path method work same as Location.path --- .../theme/components/menu/menu.spec.ts | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/menu/menu.spec.ts b/src/framework/theme/components/menu/menu.spec.ts index d3175a8661..888d6141ae 100644 --- a/src/framework/theme/components/menu/menu.spec.ts +++ b/src/framework/theme/components/menu/menu.spec.ts @@ -3,7 +3,16 @@ * Copyright Akveo. All Rights Reserved. * Licensed under the MIT License. See License.txt in the project root for license information. */ -import { Component, DebugElement, Input, QueryList, ViewChild, ViewChildren } from '@angular/core'; +import { + Component, + DebugElement, + Input, + QueryList, + ViewChild, + ViewChildren, + Injectable, +} from '@angular/core'; +import { Location } from '@angular/common'; import { Router, Routes } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { By } from '@angular/platform-browser'; @@ -13,10 +22,12 @@ import { NbMenuModule } from './menu.module'; import { NbMenuBag, NbMenuInternalService, NbMenuItem, NbMenuService } from './menu.service'; import { NbThemeModule } from '../../theme.module'; import { - getFragmentPartOfUrl, isFragmentContain, + getFragmentPartOfUrl, + isFragmentContain, isFragmentEqual, isUrlPathContain, isUrlPathEqual, + getPathPartOfUrl, } from './url-matching-helpers'; import { pairwise, take } from 'rxjs/operators'; import { NbMenuComponent } from './menu.component'; @@ -26,6 +37,7 @@ import { NbLayoutDirection, NbLayoutDirectionService, } from '@nebular/theme'; +import { SpyLocation } from '@angular/common/testing'; @Component({ template: '' }) export class NoopComponent {} @@ -55,6 +67,23 @@ export class DoubleMenusTestComponent { @ViewChildren(NbMenuComponent) menuComponent: QueryList; } + +// Overrides SpyLocation path method to take into account `includeHash` parameter. +// Original SpyLocation ignores parameters and always returns path with hash which is different +// from Location. +@Injectable() +export class SpyLocationPathParameter extends SpyLocation { + path(includeHash: boolean = false): string { + const path = super.path(); + + if (includeHash) { + return path; + } + + return getPathPartOfUrl(path); + } +} + function createTestBed(routes: Routes = []) { TestBed.configureTestingModule({ imports: [ @@ -67,6 +96,8 @@ function createTestBed(routes: Routes = []) { providers: [NbMenuService], }); + TestBed.overrideProvider(Location, { useValue: new SpyLocationPathParameter() }); + const iconLibs: NbIconLibraries = TestBed.get(NbIconLibraries); iconLibs.registerSvgPack('test', { 'some-icon': 'some-icon' }); iconLibs.setDefaultPack('test')