Skip to content

Commit

Permalink
fix(menu): path fragment comparison (#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
yggg authored Jul 12, 2019
1 parent 7df24fa commit abbd659
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/framework/theme/components/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 33 additions & 2 deletions src/framework/theme/components/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -26,6 +37,7 @@ import {
NbLayoutDirection,
NbLayoutDirectionService,
} from '@nebular/theme';
import { SpyLocation } from '@angular/common/testing';

@Component({ template: '' })
export class NoopComponent {}
Expand Down Expand Up @@ -55,6 +67,23 @@ export class DoubleMenusTestComponent {
@ViewChildren(NbMenuComponent) menuComponent: QueryList<NbMenuComponent>;
}


// 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: [
Expand All @@ -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': '<svg>some-icon</svg>' });
iconLibs.setDefaultPack('test')
Expand Down

0 comments on commit abbd659

Please sign in to comment.