11import { TestBed } from '@angular/core/testing' ;
2+ import { ActivatedRoute } from '@angular/router' ;
23import { TranslateModule } from '@ngx-translate/core' ;
34import { of } from 'rxjs' ;
45
56import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service' ;
6- import { Item } from '../../../core/shared/item.model' ;
7- import { ITEM } from '../../../core/shared/item.resource-type' ;
8- import { createSuccessfulRemoteDataObject } from '../../remote-data.utils' ;
9- import { MenuItemType } from '../menu-item-type.model' ;
10- import { PartialMenuSection } from '../menu-provider.model' ;
7+ import { ActivatedRouteStub } from '../../testing/active-router.stub' ;
118import { StatisticsMenuProvider } from './statistics.menu' ;
129
1310describe ( 'StatisticsMenuProvider' , ( ) => {
14-
15- const expectedSectionsNoDSO : PartialMenuSection [ ] = [
16- {
17- visible : true ,
18- model : {
19- type : MenuItemType . LINK ,
20- text : 'menu.section.statistics' ,
21- link : `statistics` ,
22- } ,
23- icon : 'chart-line' ,
24- } ,
25- ] ;
26-
27- const expectedSectionsForItem : PartialMenuSection [ ] = [
28- {
29- visible : true ,
30- model : {
31- type : MenuItemType . LINK ,
32- text : 'menu.section.statistics' ,
33- link : `statistics/items/test-item-uuid` ,
34- } ,
35- icon : 'chart-line' ,
36- } ,
37- ] ;
38-
39- const expectedSectionsForItemInvisible : PartialMenuSection [ ] = [
40- {
41- visible : false ,
42- model : {
43- type : MenuItemType . LINK ,
44- text : 'menu.section.statistics' ,
45- link : `statistics/items/test-item-uuid` ,
46- } ,
47- icon : 'chart-line' ,
48- } ,
49- ] ;
50-
5111 let provider : StatisticsMenuProvider ;
52-
53- const item : Item = Object . assign ( new Item ( ) , {
54- uuid : 'test-item-uuid' ,
55- type : ITEM . value ,
56- _links : { self : { href : 'self-link' } } ,
57- metadata : {
58- 'dc.title' : [ {
59- 'value' : 'Untyped Item' ,
60- } ] ,
61- } ,
62- } ) ;
63-
64- const item2 : Item = Object . assign ( new Item ( ) , {
65- uuid : 'test-item2-uuid' ,
66- type : ITEM . value ,
67- _links : { self : { href : 'self-link' } } ,
68- metadata : {
69- 'dc.title' : [ {
70- 'value' : 'Untyped Item 2' ,
71- } ] ,
72- } ,
73- } ) ;
7412 let authorizationService : AuthorizationDataService ;
7513
7614 beforeEach ( ( ) => {
@@ -83,6 +21,7 @@ describe('StatisticsMenuProvider', () => {
8321 providers : [
8422 StatisticsMenuProvider ,
8523 { provide : AuthorizationDataService , useValue : authorizationService } ,
24+ { provide : ActivatedRoute , useValue : new ActivatedRouteStub ( { } , { } ) } ,
8625 ] ,
8726 } ) ;
8827 provider = TestBed . inject ( StatisticsMenuProvider ) ;
@@ -93,62 +32,21 @@ describe('StatisticsMenuProvider', () => {
9332 } ) ;
9433
9534 describe ( 'getSectionsForContext' , ( ) => {
96- it ( 'should return the general statistics link when no DSO is provided ' , ( done ) => {
35+ it ( 'should return menu entries when at least one authorization is granted ' , ( done ) => {
9736 provider . getSectionsForContext ( undefined ) . subscribe ( ( sections ) => {
98- expect ( sections ) . toEqual ( expectedSectionsNoDSO ) ;
99- done ( ) ;
100- } ) ;
101- } ) ;
102- it ( 'should return a statistics link to the DSO when a DSO is provided' , ( done ) => {
103- provider . getSectionsForContext ( item ) . subscribe ( ( sections ) => {
104- expect ( sections ) . toEqual ( expectedSectionsForItem ) ;
105- done ( ) ;
106- } ) ;
107- } ) ;
108- it ( 'should not return anything if not authorized to view statistics' , ( done ) => {
109- ( TestBed . inject ( AuthorizationDataService ) as any ) . isAuthorized . and . returnValue ( of ( false ) ) ;
110- provider . getSectionsForContext ( item ) . subscribe ( ( sections ) => {
111- expect ( sections ) . toEqual ( expectedSectionsForItemInvisible ) ;
112- done ( ) ;
113- } ) ;
114- } ) ;
115- } ) ;
116-
117- describe ( 'getRouteContext' , ( ) => {
118- it ( 'should get the dso from the route' , ( done ) => {
119- const route = { data : { dso : createSuccessfulRemoteDataObject ( item ) } } as any ;
120-
121- provider . getRouteContext ( route , undefined ) . subscribe ( ( dso ) => {
122- expect ( dso ) . toEqual ( item ) ;
37+ expect ( Array . isArray ( sections ) ) . toBeTrue ( ) ;
38+ expect ( sections . length ) . toBeGreaterThan ( 0 ) ;
39+ expect ( sections . some ( ( s ) => s . id === 'statistics' ) ) . toBeTrue ( ) ;
12340 done ( ) ;
12441 } ) ;
12542 } ) ;
126- it ( 'should get the dso from first parent route with a dso when the route itself has none' , ( done ) => {
127- const route = {
128- data : { } ,
129- parent : {
130- data : { } ,
131- parent : {
132- data : { dso : createSuccessfulRemoteDataObject ( item ) } ,
133- parent : { data : { dso : createSuccessfulRemoteDataObject ( item2 ) } } ,
134- } ,
135- } ,
136- } as any ;
13743
138- provider . getRouteContext ( route , undefined ) . subscribe ( ( dso ) => {
139- expect ( dso ) . toEqual ( item ) ;
140- expect ( dso ) . not . toEqual ( item2 ) ;
141- done ( ) ;
142- } ) ;
143- } ) ;
144- it ( 'should return undefined when no dso is found in the route' , ( done ) => {
145- const route = { data : { } , parent : { data : { } , parent : { data : { } , parent : { data : { } } } } } as any ;
146-
147- provider . getRouteContext ( route , undefined ) . subscribe ( ( dso ) => {
148- expect ( dso ) . toBeUndefined ( ) ;
44+ it ( 'should return an empty array when no authorizations are granted' , ( done ) => {
45+ ( TestBed . inject ( AuthorizationDataService ) as any ) . isAuthorized . and . returnValue ( of ( false ) ) ;
46+ provider . getSectionsForContext ( undefined ) . subscribe ( ( sections ) => {
47+ expect ( sections ) . toEqual ( [ ] ) ;
14948 done ( ) ;
15049 } ) ;
15150 } ) ;
15251 } ) ;
153-
15452} ) ;
0 commit comments