From 1a088e9576393fb1a64e7f91e97989f3689f68dc Mon Sep 17 00:00:00 2001 From: Nikita Poltoratsky Date: Fri, 30 Nov 2018 19:29:26 +0300 Subject: [PATCH 1/6] fix(menu): highlight when active anchors --- .../theme/components/menu/menu-item.component.html | 2 +- .../theme/components/menu/menu.service.ts | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/framework/theme/components/menu/menu-item.component.html b/src/framework/theme/components/menu/menu-item.component.html index 2f26629780..de1cec9db1 100644 --- a/src/framework/theme/components/menu/menu-item.component.html +++ b/src/framework/theme/components/menu/menu-item.component.html @@ -10,7 +10,7 @@ [attr.title]="menuItem.title" [class.active]="menuItem.selected" (mouseenter)="onHoverItem(menuItem)" - (click)="onItemClick(menuItem);"> + (click)="onSelectItem(menuItem);"> {{ menuItem.title }} diff --git a/src/framework/theme/components/menu/menu.service.ts b/src/framework/theme/components/menu/menu.service.ts index cf602c49e3..63ea5e31f7 100644 --- a/src/framework/theme/components/menu/menu.service.ts +++ b/src/framework/theme/components/menu/menu.service.ts @@ -362,8 +362,18 @@ export class NbMenuInternalService { private isSelectedInUrl(item: NbMenuItem): boolean { const exact: boolean = item.pathMatch === 'full'; + let link: string = item.link; + + /** + * By default, `item.link` doesn't contain a fragment. But `this.location.path()` does. + * And when we're going to compare them we have to append fragment to the link. + * */ + if (item.fragment) { + link += `#${item.fragment}`; + } + return exact - ? isUrlPathEqual(this.location.path(), item.link) - : isUrlPathContain(this.location.path(), item.link); + ? isUrlPathEqual(this.location.path(), link) + : isUrlPathContain(this.location.path(), link); } } From 0c18bae9adc5bc9e601983e967ff197dbfd7f762 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Thu, 28 Mar 2019 15:08:52 +0300 Subject: [PATCH 2/6] feat(menu): compare url fragment --- .../components/menu/menu-item.component.html | 2 +- .../theme/components/menu/menu.service.ts | 20 +++++++------- .../theme/components/menu/menu.spec.ts | 27 ++++++++++++++++++- .../components/menu/url-matching-helpers.ts | 9 +++++++ 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/framework/theme/components/menu/menu-item.component.html b/src/framework/theme/components/menu/menu-item.component.html index d925d026c6..c3bb5f4081 100644 --- a/src/framework/theme/components/menu/menu-item.component.html +++ b/src/framework/theme/components/menu/menu-item.component.html @@ -11,7 +11,7 @@ [attr.title]="menuItem.title" [class.active]="menuItem.selected" (mouseenter)="onHoverItem(menuItem)" - (click)="onSelectItem(menuItem);"> + (click)="onItemClick(menuItem);"> {{ menuItem.title }} diff --git a/src/framework/theme/components/menu/menu.service.ts b/src/framework/theme/components/menu/menu.service.ts index a49541dcee..ebfb66db52 100644 --- a/src/framework/theme/components/menu/menu.service.ts +++ b/src/framework/theme/components/menu/menu.service.ts @@ -9,7 +9,7 @@ import { Location } from '@angular/common'; import { Params } from '@angular/router'; import { Observable, BehaviorSubject, ReplaySubject, Subject } from 'rxjs'; import { share } from 'rxjs/operators'; -import { isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; +import { isFragmentEqual, isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; export interface NbMenuBag { tag: string; item: NbMenuItem } @@ -376,18 +376,16 @@ export class NbMenuInternalService { private isSelectedInUrl(item: NbMenuItem): boolean { const exact: boolean = item.pathMatch === 'full'; - let link: string = item.link; - - /** - * By default, `item.link` doesn't contain a fragment. But `this.location.path()` does. - * And when we're going to compare them we have to append fragment to the link. - * */ - if (item.fragment) { - link += `#${item.fragment}`; - } + const link: string = item.link; - return exact + const isSelectedInPath = exact ? isUrlPathEqual(this.location.path(), link) : isUrlPathContain(this.location.path(), link); + + if (isSelectedInPath && item.fragment != null) { + return isFragmentEqual(this.location.path(), item.fragment); + } + + return isSelectedInPath; } } diff --git a/src/framework/theme/components/menu/menu.spec.ts b/src/framework/theme/components/menu/menu.spec.ts index e3ee3597d7..5b6c5c535f 100644 --- a/src/framework/theme/components/menu/menu.spec.ts +++ b/src/framework/theme/components/menu/menu.spec.ts @@ -10,7 +10,7 @@ import { TestBed } from '@angular/core/testing'; import { NbMenuModule } from './menu.module'; import { NbMenuBag, NbMenuItem, NbMenuService } from './menu.service'; import { NbThemeModule } from '../../theme.module'; -import { isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; +import { getFragmentPartOfUrl, isFragmentEqual, isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; import { pairwise, take } from 'rxjs/operators'; import { NbMenuComponent } from './menu.component'; @@ -271,4 +271,29 @@ describe('menu URL helpers', () => { expect(isUrlPathEqual('/a/b/c?a=1;b=2&c=3', '/a/b/c')).toBeTruthy(); }); + it('getFragmentPartOfUrl should return empty string for path without fragment', () => { + expect(getFragmentPartOfUrl('/a/b')).toBeFalsy(); + expect(getFragmentPartOfUrl('/a/b/c?a=1;b=2&c=3')).toBeFalsy(); + }); + + it('getFragmentPartOfUrl should return fragment part when it presented', () => { + expect(getFragmentPartOfUrl('/a/b#f')).toEqual('f'); + expect(getFragmentPartOfUrl('/a/b/c?a=1;b=2&c=3#fragment')).toEqual('fragment'); + }); + + it('isFragmentEqual should return false for path without fragments', () => { + expect(isFragmentEqual('/a/b', 'fragment')).toBeFalsy(); + expect(isFragmentEqual('/a/b/c?a=1;b=2&c=3', 'fragment')).toBeFalsy(); + }); + + it('isFragmentEqual should return false for path with different fragments', () => { + expect(isFragmentEqual('/a/b#f', 'fragment')).toBeFalsy(); + expect(isFragmentEqual('/a/b/c?a=1;b=2&c=3#f', 'fragment')).toBeFalsy(); + }); + + it('isFragmentEqual should return true for path with same fragments', () => { + expect(isFragmentEqual('/a/b#fragment', 'fragment')).toBeTruthy(); + expect(isFragmentEqual('/a/b/c?a=1;b=2&c=3#fragment', 'fragment')).toBeTruthy(); + }); + }); diff --git a/src/framework/theme/components/menu/url-matching-helpers.ts b/src/framework/theme/components/menu/url-matching-helpers.ts index 536be91aea..57ac14155a 100644 --- a/src/framework/theme/components/menu/url-matching-helpers.ts +++ b/src/framework/theme/components/menu/url-matching-helpers.ts @@ -19,3 +19,12 @@ export function isUrlPathContain(path, link) { export function getPathPartOfUrl(url): string { return url.match(/.*?(?=[?;#]|$)/)[0]; } + +export function getFragmentPartOfUrl(url: string): string { + const matched = url.match(/#(.+)/); + return matched ? matched[1] : ''; +} + +export function isFragmentEqual(path: string, fragment: string): boolean { + return getFragmentPartOfUrl(path) === fragment; +} From ecd2e02a59449d81017f7864bfc9826dc529e4d8 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Fri, 29 Mar 2019 21:47:06 +0300 Subject: [PATCH 3/6] feat(menu): add fragment contains helper --- src/framework/theme/components/menu/menu.service.ts | 6 ++++-- src/framework/theme/components/menu/url-matching-helpers.ts | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/menu/menu.service.ts b/src/framework/theme/components/menu/menu.service.ts index ebfb66db52..e50babb0a3 100644 --- a/src/framework/theme/components/menu/menu.service.ts +++ b/src/framework/theme/components/menu/menu.service.ts @@ -9,7 +9,7 @@ import { Location } from '@angular/common'; import { Params } from '@angular/router'; import { Observable, BehaviorSubject, ReplaySubject, Subject } from 'rxjs'; import { share } from 'rxjs/operators'; -import { isFragmentEqual, isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; +import { isFragmentContain, isFragmentEqual, isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; export interface NbMenuBag { tag: string; item: NbMenuItem } @@ -383,7 +383,9 @@ export class NbMenuInternalService { : isUrlPathContain(this.location.path(), link); if (isSelectedInPath && item.fragment != null) { - return isFragmentEqual(this.location.path(), item.fragment); + return exact + ? isFragmentEqual(this.location.path(), item.fragment) + : isFragmentContain(this.location.path(), item.fragment); } return isSelectedInPath; diff --git a/src/framework/theme/components/menu/url-matching-helpers.ts b/src/framework/theme/components/menu/url-matching-helpers.ts index 57ac14155a..4d13d498e6 100644 --- a/src/framework/theme/components/menu/url-matching-helpers.ts +++ b/src/framework/theme/components/menu/url-matching-helpers.ts @@ -28,3 +28,7 @@ export function getFragmentPartOfUrl(url: string): string { export function isFragmentEqual(path: string, fragment: string): boolean { return getFragmentPartOfUrl(path) === fragment; } + +export function isFragmentContain(path: string, fragment: string): boolean { + return getFragmentPartOfUrl(path).includes(fragment); +} From 6858ccffe29e101f9ccaba3b6aa1c52d3da064ae Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Fri, 29 Mar 2019 21:59:01 +0300 Subject: [PATCH 4/6] test(menu): add checks for fragment contains helper --- .../theme/components/menu/menu.spec.ts | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/menu/menu.spec.ts b/src/framework/theme/components/menu/menu.spec.ts index 5b6c5c535f..c37a43cd7b 100644 --- a/src/framework/theme/components/menu/menu.spec.ts +++ b/src/framework/theme/components/menu/menu.spec.ts @@ -4,13 +4,19 @@ * Licensed under the MIT License. See License.txt in the project root for license information. */ import { Component, Input, QueryList, ViewChild, ViewChildren } from '@angular/core'; +import { Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { TestBed } from '@angular/core/testing'; import { NbMenuModule } from './menu.module'; -import { NbMenuBag, NbMenuItem, NbMenuService } from './menu.service'; +import { NbMenuBag, NbMenuInternalService, NbMenuItem, NbMenuService } from './menu.service'; import { NbThemeModule } from '../../theme.module'; -import { getFragmentPartOfUrl, isFragmentEqual, isUrlPathContain, isUrlPathEqual } from './url-matching-helpers'; +import { + getFragmentPartOfUrl, isFragmentContain, + isFragmentEqual, + isUrlPathContain, + isUrlPathEqual, +} from './url-matching-helpers'; import { pairwise, take } from 'rxjs/operators'; import { NbMenuComponent } from './menu.component'; @@ -243,6 +249,28 @@ describe('menu services', () => { }); +fdescribe('NbMenuInternalService', () => { + beforeEach(() => createTestBed()); + + it('should select menu item by fragment', (done) => { + const service: NbMenuInternalService = TestBed.get(NbMenuInternalService); + const router: Router = TestBed.get(Router); + const items = [{ title: 'Item 1', link: '/', fragment: '1' }]; + service.prepareItems(items); + const menuItem: NbMenuItem = items[0]; + + expect(menuItem.selected).toEqual(undefined); + + router.navigate(['.'], { fragment: '1' }) + .then(() => { + service.selectFromUrl(items, ''); + expect(menuItem.selected).toEqual(true); + done(); + }) + .catch(() => { throw new Error('Navigation failed') }); + }); +}); + describe('menu URL helpers', () => { it('isUrlPathContain should work by url segments', () => { @@ -296,4 +324,24 @@ describe('menu URL helpers', () => { expect(isFragmentEqual('/a/b/c?a=1;b=2&c=3#fragment', 'fragment')).toBeTruthy(); }); + it('isFragmentContain should return true for url with exact fragment', () => { + expect(isFragmentContain('/a/b#1', '1')).toBeTruthy(); + expect(isFragmentContain('/#2', '2')).toBeTruthy(); + }); + + it('isFragmentContain should return true for url containing fragments', () => { + expect(isFragmentContain('/a/b#12', '1')).toBeTruthy(); + expect(isFragmentContain('/a/b?a=1;b=2&c=3#21', '1')).toBeTruthy(); + }); + + it('isFragmentContain should return false for url without fragment', () => { + expect(isFragmentContain('/a1/b', '1')).toBeFalsy(); + expect(isFragmentContain('/a1/b?a=1;b=2&c=3', '1')).toBeFalsy(); + }); + + it('isFragmentContain should return false for url with different fragment', () => { + expect(isFragmentContain('/a1/b#222', '1')).toBeTruthy(); + expect(isFragmentContain('/a1/b?a=1;b=2&c=3#222', '1')).toBeTruthy(); + }); + }); From aa6bcfea393bb089b588b250cb175de27dbe0e95 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Sat, 30 Mar 2019 12:17:09 +0300 Subject: [PATCH 5/6] test(menu): selectFromUrl test --- .../theme/components/menu/menu.spec.ts | 261 ++++++++++++++++-- 1 file changed, 241 insertions(+), 20 deletions(-) diff --git a/src/framework/theme/components/menu/menu.spec.ts b/src/framework/theme/components/menu/menu.spec.ts index c37a43cd7b..7e4abc4e11 100644 --- a/src/framework/theme/components/menu/menu.spec.ts +++ b/src/framework/theme/components/menu/menu.spec.ts @@ -4,7 +4,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. */ import { Component, Input, QueryList, ViewChild, ViewChildren } from '@angular/core'; -import { Router } from '@angular/router'; +import { Router, Routes } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { TestBed } from '@angular/core/testing'; @@ -20,6 +20,8 @@ import { import { pairwise, take } from 'rxjs/operators'; import { NbMenuComponent } from './menu.component'; +@Component({ template: '' }) +export class NoopComponent {} @Component({ template: ``, @@ -46,15 +48,15 @@ export class DoubleMenusTestComponent { @ViewChildren(NbMenuComponent) menuComponent: QueryList; } -function createTestBed() { +function createTestBed(routes: Routes = []) { TestBed.configureTestingModule({ imports: [ NbThemeModule.forRoot(), NbMenuModule.forRoot(), - RouterTestingModule.withRoutes([]), + RouterTestingModule.withRoutes(routes), NoopAnimationsModule, ], - declarations: [SingleMenuTestComponent, DoubleMenusTestComponent], + declarations: [SingleMenuTestComponent, DoubleMenusTestComponent, NoopComponent], providers: [NbMenuService], }); } @@ -81,6 +83,11 @@ function createDoubleMenuComponent( firstMenuItems, firstMenuTag, secondMenuItem return { fixture, menuService }; } +function createMenuItems(items: Partial[], menuInternaleService: NbMenuInternalService): NbMenuItem[] { + menuInternaleService.prepareItems(items as NbMenuItem[]); + return items as NbMenuItem[]; +} + describe('NbMenuItem', () => { it('should set tag attribute for menu services', () => { @@ -249,25 +256,239 @@ describe('menu services', () => { }); -fdescribe('NbMenuInternalService', () => { - beforeEach(() => createTestBed()); +describe('NbMenuInternalService', () => { + let router: Router; + let menuInternalService: NbMenuInternalService; - it('should select menu item by fragment', (done) => { - const service: NbMenuInternalService = TestBed.get(NbMenuInternalService); - const router: Router = TestBed.get(Router); - const items = [{ title: 'Item 1', link: '/', fragment: '1' }]; - service.prepareItems(items); - const menuItem: NbMenuItem = items[0]; + beforeEach(() => { + const routes = [ + { path: 'menu-1', component: NoopComponent }, + { path: 'menu-1/2', component: NoopComponent }, + { + path: 'menu-2', + component: NoopComponent, + children: [{ path: 'menu-2-level-2', component: NoopComponent }], + }, + ]; + createTestBed(routes); + router = TestBed.get(Router); + menuInternalService = TestBed.get(NbMenuInternalService); + }); - expect(menuItem.selected).toEqual(undefined); + describe('selectFromUrl pathMatch full', () => { + + it('should select menu item with matching path', (done) => { + const items: Partial[] = [{ link: '/menu-1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link]) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toEqual(true); + done(); + }); + }); + + it('should select menu item with matching path and fragment', (done) => { + const items: Partial[] = [{ link: '/menu-1', fragment: '1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link], { fragment: menuItem.fragment }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toEqual(true); + done(); + }); + }); + + it('should select child menu item and its parent', (done) => { + const items: Partial[] = [{ + link: '/menu-2', + children: [{ link: '/menu-2/menu-2-level-2' }] as NbMenuItem[], + }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const parentMenuItem: NbMenuItem = menuItems[0]; + const childMenuItem: NbMenuItem = parentMenuItem.children[0]; + + expect(parentMenuItem.selected).toBeFalsy(); + expect(childMenuItem.selected).toBeFalsy(); + + router.navigate([childMenuItem.link]) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(parentMenuItem.selected).toEqual(true); + expect(childMenuItem.selected).toEqual(true); + done(); + }); + }); + + it('should select child menu item with fragment', (done) => { + const items: Partial[] = [{ + link: '/menu-2', + children: [{ link: '/menu-2/menu-2-level-2', fragment: '22' }] as NbMenuItem[], + }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const parentMenuItem: NbMenuItem = menuItems[0]; + const childMenuItem: NbMenuItem = parentMenuItem.children[0]; + + expect(parentMenuItem.selected).toBeFalsy(); + expect(childMenuItem.selected).toBeFalsy(); + + router.navigate([childMenuItem.link], { fragment: childMenuItem.fragment }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(parentMenuItem.selected).toEqual(true); + expect(childMenuItem.selected).toEqual(true); + done(); + }); + }); + + it('should not select menu item with matching path if fragment doesn\'t match', function(done) { + const items: Partial[] = [{ link: '/menu-1', fragment: '1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link], { fragment: menuItem.fragment + 'random-fragment' }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); + + it('should not select menu item with matching fragment if path doesn\'t match', function(done) { + const items: Partial[] = [{ link: '/menu-1', fragment: '1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + const url = menuItem.link + '/2'; + router.navigate([url], { fragment: menuItem.fragment }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); + + it('should not select menu item with fragment if no fragment in url', (done) => { + const items: Partial[] = [{ link: '/menu-1', fragment: '1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link]) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); + + it('should not select menu item if path not matches fully', (done) => { + const items: Partial[] = [{ link: '/menu-1', fragment: '1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + const url = menuItem.link + '/2'; + router.navigate([url], { fragment: menuItem.fragment }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); + + it('should not select menu item if path and fragment not matches fully', (done) => { + const items: Partial[] = [{ link: '/menu-1', fragment: '1' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link], { fragment: menuItem.fragment + '1' }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); + }); - router.navigate(['.'], { fragment: '1' }) - .then(() => { - service.selectFromUrl(items, ''); - expect(menuItem.selected).toEqual(true); - done(); - }) - .catch(() => { throw new Error('Navigation failed') }); + describe('selectFromUrl pathMatch prefix', () => { + + it('should select menu item if url contains menu link', function(done) { + const items: Partial[] = [{ link: '/menu-1', pathMatch: 'prefix' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + const url = menuItem.link + '/2'; + router.navigate([url]) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toEqual(true); + done(); + }); + }); + + it('should select menu item if url contains menu link and fragment', function(done) { + const items: Partial[] = [{ link: '/menu-1', fragment: '1', pathMatch: 'prefix' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link], { fragment: menuItem.fragment + '1' }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toEqual(true); + done(); + }); + }); + + it('should not select menu item if url contains link without fragment', function(done) { + const items: Partial[] = [{ link: '/menu-1', fragment: '1', pathMatch: 'prefix' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate([menuItem.link]) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); + + it('should not select menu item if url contains fragment without link', function(done) { + const items: Partial[] = [{ link: '/menu-1', fragment: '1', pathMatch: 'prefix' }]; + const menuItems: NbMenuItem[] = createMenuItems(items, menuInternalService); + const menuItem: NbMenuItem = menuItems[0]; + + expect(menuItem.selected).toBeFalsy(); + + router.navigate(['menu-2'], { fragment: menuItem.fragment }) + .then(() => { + menuInternalService.selectFromUrl(menuItems, ''); + expect(menuItem.selected).toBeFalsy(); + done(); + }); + }); }); }); From 4a46ab78e564d591d3161c1beb010f386d5c6ccf Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Sun, 31 Mar 2019 13:52:47 +0300 Subject: [PATCH 6/6] test(menu): fix expected value --- src/framework/theme/components/menu/menu.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/menu/menu.spec.ts b/src/framework/theme/components/menu/menu.spec.ts index 7e4abc4e11..a4bf6f045f 100644 --- a/src/framework/theme/components/menu/menu.spec.ts +++ b/src/framework/theme/components/menu/menu.spec.ts @@ -561,8 +561,8 @@ describe('menu URL helpers', () => { }); it('isFragmentContain should return false for url with different fragment', () => { - expect(isFragmentContain('/a1/b#222', '1')).toBeTruthy(); - expect(isFragmentContain('/a1/b?a=1;b=2&c=3#222', '1')).toBeTruthy(); + expect(isFragmentContain('/a1/b#222', '1')).toBeFalsy(); + expect(isFragmentContain('/a1/b?a=1;b=2&c=3#222', '1')).toBeFalsy(); }); });