@@ -10,6 +10,8 @@ describe('TopMenuComponent', () => {
1010 const list : HTMLUListElement = fixture . debugElement . nativeElement . querySelector ( 'ul' ) ;
1111 return Array . from ( list . querySelectorAll ( 'li' ) ) ;
1212 } ;
13+ const getSelected = ( items : HTMLLIElement [ ] ) =>
14+ items . filter ( item => item . classList . contains ( 'selected' ) ) ;
1315
1416 beforeEach ( ( ) => {
1517 TestBed . configureTestingModule ( {
@@ -38,4 +40,47 @@ describe('TopMenuComponent', () => {
3840 expect ( links . map ( link => link . textContent ) ) . toEqual ( [ 'API' , 'Features' ] ) ;
3941 expect ( links . map ( link => link . title ) ) . toEqual ( [ 'API docs' , 'Angular features overview' ] ) ;
4042 } ) ;
43+
44+ it ( 'should mark the currently selected node with `.selected`' , ( ) => {
45+ const items = getListItems ( ) ;
46+ expect ( getSelected ( items ) ) . toEqual ( [ ] ) ;
47+
48+ component . currentNode = { url : 'api' , view : 'foo' , nodes : [ ] } ;
49+ fixture . detectChanges ( ) ;
50+ expect ( getSelected ( items ) ) . toEqual ( [ items [ 0 ] ] ) ;
51+
52+ component . currentNode = { url : 'features' , view : 'foo' , nodes : [ ] } ;
53+ fixture . detectChanges ( ) ;
54+ expect ( getSelected ( items ) ) . toEqual ( [ items [ 1 ] ] ) ;
55+
56+ component . currentNode = { url : 'something/else' , view : 'foo' , nodes : [ ] } ;
57+ fixture . detectChanges ( ) ;
58+ expect ( getSelected ( items ) ) . toEqual ( [ ] ) ;
59+ } ) ;
60+
61+ it ( 'should not mark any node with `.selected` if the current URL is undefined' , ( ) => {
62+ component . nodes = [
63+ { url : '' , title : 'API' , tooltip : 'API docs' } ,
64+ { url : undefined , title : 'Features' , tooltip : 'Angular features overview' } ,
65+ ] ;
66+ fixture . detectChanges ( ) ;
67+ const items = getListItems ( ) ;
68+
69+ component . currentNode = undefined ;
70+ fixture . detectChanges ( ) ;
71+ expect ( getSelected ( items ) ) . toEqual ( [ ] ) ;
72+ } ) ;
73+
74+ it ( 'should correctly mark a node with `.selected` even if its URL is empty' , ( ) => {
75+ component . nodes = [
76+ { url : '' , title : 'API' , tooltip : 'API docs' } ,
77+ { url : undefined , title : 'Features' , tooltip : 'Angular features overview' } ,
78+ ] ;
79+ fixture . detectChanges ( ) ;
80+ const items = getListItems ( ) ;
81+
82+ component . currentNode = { url : '' , view : 'Empty url' , nodes : [ ] } ;
83+ fixture . detectChanges ( ) ;
84+ expect ( getSelected ( items ) ) . toEqual ( [ items [ 0 ] ] ) ;
85+ } ) ;
4186} ) ;
0 commit comments