1+ import { PLATFORM_ID } from '@angular/core' ;
12import { TestBed } from '@angular/core/testing' ;
3+ import { AuthService } from '@dspace/core/auth/auth.service' ;
24import { AuthorizationDataService } from '@dspace/core/data/feature-authorization/authorization-data.service' ;
5+ import { SubscriptionsDataService } from '@dspace/core/data/subscriptions-data.service' ;
36import { Collection } from '@dspace/core/shared/collection.model' ;
47import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
8+ import { TranslateService } from '@ngx-translate/core' ;
59import { of } from 'rxjs' ;
610
711import { MenuItemType } from '../menu-item-type.model' ;
8- import { PartialMenuSection } from '../menu-provider.model' ;
912import { SubscribeMenuProvider } from './comcol-subscribe.menu' ;
1013
1114describe ( 'SubscribeMenuProvider' , ( ) => {
1215
13- const expectedSections : PartialMenuSection [ ] = [
14- {
15- visible : true ,
16- model : {
17- type : MenuItemType . ONCLICK ,
18- text : 'subscriptions.tooltip' ,
19- function : jasmine . any ( Function ) as any ,
20- } ,
21- icon : 'bell' ,
22- } ,
23- ] ;
24-
2516 let provider : SubscribeMenuProvider ;
2617
27- const dso : Collection = Object . assign ( new Collection ( ) , { _links : { self : { href : 'self-link' } } } ) ;
28-
18+ const dso : Collection = Object . assign ( new Collection ( ) , {
19+ uuid : 'mock-uuid' ,
20+ _links : { self : { href : 'self-link' } } ,
21+ } ) ;
2922
3023 let authorizationService ;
3124 let modalService ;
25+ let authService ;
26+ let subscriptionsDataService ;
3227
3328 beforeEach ( ( ) => {
34-
3529 authorizationService = jasmine . createSpyObj ( 'authorizationService' , {
36- ' isAuthorized' : of ( true ) ,
30+ isAuthorized : of ( true ) ,
3731 } ) ;
3832
3933 modalService = jasmine . createSpyObj ( 'modalService' , [ 'open' ] ) ;
4034
35+ authService = jasmine . createSpyObj ( 'authService' , {
36+ getAuthenticatedUserFromStore : of ( { id : 'mock-user-id' } ) ,
37+ } ) ;
38+
39+ subscriptionsDataService = jasmine . createSpyObj ( 'subscriptionsDataService' , {
40+ getSubscriptionsByPersonDSO : of ( { payload : { page : [ ] } } ) ,
41+ } ) ;
42+
4143 TestBed . configureTestingModule ( {
4244 providers : [
4345 SubscribeMenuProvider ,
4446 { provide : AuthorizationDataService , useValue : authorizationService } ,
4547 { provide : NgbModal , useValue : modalService } ,
48+ { provide : AuthService , useValue : authService } ,
49+ { provide : SubscriptionsDataService , useValue : subscriptionsDataService } ,
50+ { provide : TranslateService , useValue : { } } ,
51+ { provide : PLATFORM_ID , useValue : 'browser' } ,
4652 ] ,
4753 } ) ;
4854 provider = TestBed . inject ( SubscribeMenuProvider ) ;
@@ -53,13 +59,32 @@ describe('SubscribeMenuProvider', () => {
5359 } ) ;
5460
5561 describe ( 'getSectionsForContext' , ( ) => {
56- it ( 'should return the expected sections' , ( done ) => {
57- provider . getSectionsForContext ( dso ) . subscribe ( ( sections ) => {
58- expect ( sections ) . toEqual ( expectedSections ) ;
59- done ( ) ;
60- } ) ;
62+ it ( 'should return subscribe section when user has no subscription' , ( done ) => {
63+ provider . getSectionsForContext ( dso )
64+ . pipe ( )
65+ . subscribe ( ( sections ) => {
66+ if ( sections . length > 0 && sections [ 0 ] . visible ) {
67+ expect ( sections [ 0 ] . model . type ) . toBe ( MenuItemType . ONCLICK ) ;
68+ expect ( ( sections [ 0 ] . model as any ) . text ) . toBe ( 'subscriptions.tooltip' ) ;
69+ expect ( sections [ 0 ] . icon ) . toBe ( 'bell' ) ;
70+ done ( ) ;
71+ }
72+ } ) ;
6173 } ) ;
62- } ) ;
6374
75+ it ( 'should return manage subscription section when user is already subscribed' , ( done ) => {
76+ subscriptionsDataService . getSubscriptionsByPersonDSO . and . returnValue (
77+ of ( { payload : { page : [ { id : 'sub-1' } ] } } ) ,
78+ ) ;
6479
80+ provider . getSectionsForContext ( dso )
81+ . pipe ( )
82+ . subscribe ( ( sections ) => {
83+ if ( sections . length > 0 && sections [ 0 ] . visible ) {
84+ expect ( ( sections [ 0 ] . model as any ) . text ) . toBe ( 'subscriptions.manage' ) ;
85+ done ( ) ;
86+ }
87+ } ) ;
88+ } ) ;
89+ } ) ;
6590} ) ;
0 commit comments