11import { TestBed } from '@angular/core/testing' ;
2+ import { Router } from '@angular/router' ;
23import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed' ;
3- import { SidebarMenuComponent } from '../sidebar-menu.component' ;
4-
5- import { MenuHarness } from './menu.harness' ;
6- import { MenuItemComponent } from '../menu-item.component' ;
7- import { MenuItemNodeComponent } from '../menu-item-node.component' ;
8- import { MenuItemAnchorComponent } from '../menu-item-anchor.component' ;
9- import { MenuItemNodeService } from '../menu-item-node.service' ;
10- import { MenuItemAnchorService } from '../menu-item-anchor.service' ;
11- import { MenuItemRoleService } from '../menu-item-role.service' ;
12- import { RouterTestingModule } from '@angular/router/testing' ;
13- import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
14- import { Component } from '@angular/core' ;
15- import { Menu , MenuItemBadge , MenuItemLeafRoute } from '../sidebar-menu.interface' ;
16- import { Route , Router } from '@angular/router' ;
17- import { customMatchers } from './custom.matchers.spec' ;
184
19- @Component ( { } )
20- class RoutedStubComponent { }
5+ import { MenuHarness } from '../testing/menu.harness' ;
216
22- const routes : Route [ ] = [
23- {
24- path : '' ,
25- component : RoutedStubComponent ,
26- } ,
27- {
28- path : 'second' ,
29- component : RoutedStubComponent ,
30- } ,
31- {
32- path : 'third' ,
33- component : RoutedStubComponent ,
34- } ,
35- ] ;
7+ import { Menu , MenuItemBadge , MenuItemHeader , MenuItemLeafRoute } from '../sidebar-menu.interface' ;
8+
9+ import { customMatchers } from './custom.matchers.spec' ;
10+ import { sharedTestingModuleFactory , WrapperStubComponent } from './shared.spec' ;
3611
3712const menu : Menu = [
3813 {
@@ -63,35 +38,21 @@ const menu: Menu = [
6338 } ,
6439] ;
6540
66- @Component ( { template : '<asm-angular-sidebar-menu [menu]="menu"></asm-angular-sidebar-menu>' } )
67- class WrapperStubComponent {
68- menu : Menu = menu ;
69- }
70-
71- describe ( 'menu role' , ( ) => {
41+ describe ( 'first level' , ( ) => {
7242 let harness : MenuHarness ;
7343 let router : Router ;
7444
7545 beforeEach ( async ( ) => {
7646 jasmine . addMatchers ( customMatchers ) ;
7747
78- await TestBed . configureTestingModule ( {
79- declarations : [
80- WrapperStubComponent ,
81- SidebarMenuComponent ,
82- MenuItemComponent ,
83- MenuItemNodeComponent ,
84- MenuItemAnchorComponent ,
85- ] ,
86- providers : [ MenuItemNodeService , MenuItemAnchorService , MenuItemRoleService ] ,
87- imports : [ RouterTestingModule . withRoutes ( routes ) , NoopAnimationsModule ] ,
88- } ) . compileComponents ( ) ;
48+ await TestBed . configureTestingModule ( sharedTestingModuleFactory ( ) ) . compileComponents ( ) ;
8949
9050 const fixture = TestBed . createComponent ( WrapperStubComponent ) ;
9151 const loader = TestbedHarnessEnvironment . loader ( fixture ) ;
9252 harness = await loader . getHarness ( MenuHarness ) ;
9353 router = TestBed . inject ( Router ) ;
9454
55+ fixture . componentInstance . menu = menu ;
9556 router . initialNavigation ( ) ;
9657 fixture . detectChanges ( ) ;
9758 } ) ;
@@ -110,55 +71,62 @@ describe('menu role', () => {
11071 } ) ;
11172
11273 it ( 'should activate second menu item on navigation' , async ( ) => {
113- await router . navigateByUrl ( ( menu [ 1 ] as MenuItemLeafRoute ) . route ) ;
74+ const itemConf = menu [ 1 ] as MenuItemLeafRoute ;
75+ await router . navigateByUrl ( itemConf . route ) ;
11476
11577 const items = await harness . getActivatedAnchors ( ) ;
11678 expect ( items . length ) . toEqual ( 1 ) ;
11779
118- const label = await harness . getActivatedAnchorLabel ( ) ;
119- expect ( await label . text ( ) ) . toEqual ( menu [ 1 ] . label as string ) ;
80+ const label = await harness . getActivatedAnchorsLabels ( ) ;
81+ expect ( label . length ) . toEqual ( 1 ) ;
82+ expect ( await label [ 0 ] . text ( ) ) . toEqual ( itemConf . label ) ;
12083 } ) ;
12184
12285 it ( 'should navigate to item route on menu item click' , async ( ) => {
123- const item = await harness . getItemsWith ( { label : menu [ 2 ] . label } ) ;
86+ const itemConf = menu [ 2 ] as MenuItemLeafRoute ;
87+ const item = await harness . getItemsWith ( { label : itemConf . label } ) ;
12488 const link = await item [ 0 ] . getAnchorElement ( ) ;
12589
12690 await link . click ( ) ;
12791
12892 const items = await harness . getActivatedAnchors ( ) ;
12993 expect ( items . length ) . toEqual ( 1 ) ;
13094
131- const label = await harness . getActivatedAnchorLabel ( ) ;
132- expect ( await label . text ( ) ) . toEqual ( menu [ 2 ] . label as string ) ;
95+ const label = await harness . getActivatedAnchorsLabels ( ) ;
96+ expect ( label . length ) . toEqual ( 1 ) ;
97+ expect ( await label [ 0 ] . text ( ) ) . toEqual ( itemConf . label ) ;
13398
134- expect ( router . url ) . toEqual ( ( menu [ 2 ] as MenuItemLeafRoute ) . route ) ;
99+ expect ( router . url ) . toEqual ( itemConf . route ) ;
135100 } ) ;
136101
137102 it ( 'should have one element with icon and two icon classes' , async ( ) => {
103+ const itemConf = menu [ 2 ] as MenuItemLeafRoute ;
138104 const elements = await harness . getItemsWithIcons ( ) ;
139105
140106 expect ( elements . length ) . toEqual ( 1 ) ;
141- expect ( elements [ 0 ] ) . toHaveClassesHarness ( menu [ 2 ] . iconClasses ) ;
107+ expect ( elements [ 0 ] ) . toHaveClasses ( itemConf . iconClasses ) ;
142108 } ) ;
143109
144110 it ( 'should have one element with two badges' , async ( ) => {
111+ const itemConf = menu [ 1 ] as MenuItemLeafRoute ;
145112 const badgesConf = menu [ 1 ] . badges as MenuItemBadge [ ] ;
146- const elements = await harness . getItemsWith ( { label : menu [ 1 ] . label } ) ;
113+ const elements = await harness . getItemsWith ( { label : itemConf . label } ) ;
147114 const badges = await elements [ 0 ] . getBadgesElement ( ) ;
148115
149116 expect ( elements . length ) . toEqual ( 1 ) ;
150117
151118 expect ( badges . length ) . toEqual ( 2 ) ;
152119 expect ( await badges [ 0 ] . text ( ) ) . toEqual ( badgesConf [ 0 ] . label ) ;
153120 expect ( await badges [ 1 ] . text ( ) ) . toEqual ( badgesConf [ 1 ] . label ) ;
154- expect ( badges [ 0 ] ) . toHaveClassesHarness ( badgesConf [ 0 ] . classes ) ;
155- expect ( badges [ 1 ] ) . toHaveClassesHarness ( badgesConf [ 1 ] . classes ) ;
121+ expect ( badges [ 0 ] ) . toHaveClasses ( badgesConf [ 0 ] . classes ) ;
122+ expect ( badges [ 1 ] ) . toHaveClasses ( badgesConf [ 1 ] . classes ) ;
156123 } ) ;
157124
158125 it ( 'should create 1 header' , async ( ) => {
126+ const itemConf = menu [ 3 ] as MenuItemHeader ;
159127 const elements = await harness . getItemsHeaders ( ) ;
160128
161129 expect ( elements . length ) . toEqual ( 1 ) ;
162- expect ( await elements [ 0 ] . text ( ) ) . toEqual ( menu [ 3 ] . header as string ) ;
130+ expect ( await elements [ 0 ] . text ( ) ) . toEqual ( itemConf . header ) ;
163131 } ) ;
164132} ) ;
0 commit comments