Skip to content

Commit 3520af9

Browse files
fix #4195: update spec and menu provider for subscribe button
1 parent e953e4d commit 3520af9

2 files changed

Lines changed: 50 additions & 25 deletions

File tree

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
1+
import { PLATFORM_ID } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
3+
import { AuthService } from '@dspace/core/auth/auth.service';
24
import { AuthorizationDataService } from '@dspace/core/data/feature-authorization/authorization-data.service';
5+
import { SubscriptionsDataService } from '@dspace/core/data/subscriptions-data.service';
36
import { Collection } from '@dspace/core/shared/collection.model';
47
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
8+
import { TranslateService } from '@ngx-translate/core';
59
import { of } from 'rxjs';
610

711
import { MenuItemType } from '../menu-item-type.model';
8-
import { PartialMenuSection } from '../menu-provider.model';
912
import { SubscribeMenuProvider } from './comcol-subscribe.menu';
1013

1114
describe('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
});

src/app/shared/menu/providers/comcol-subscribe.menu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import {
1111
Injectable,
1212
PLATFORM_ID,
1313
} from '@angular/core';
14+
import { AuthService } from '@dspace/core/auth/auth.service';
1415
import { AuthorizationDataService } from '@dspace/core/data/feature-authorization/authorization-data.service';
1516
import { FeatureID } from '@dspace/core/data/feature-authorization/feature-id';
17+
import { SubscriptionsDataService } from '@dspace/core/data/subscriptions-data.service';
1618
import { DSpaceObject } from '@dspace/core/shared/dspace-object.model';
1719
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
1820
import { TranslateService } from '@ngx-translate/core';
@@ -29,10 +31,8 @@ import {
2931
startWith,
3032
switchMap,
3133
} from 'rxjs/operators';
32-
import { AuthService } from '@dspace/core/auth/auth.service';
3334

3435
import { SubscriptionModalComponent } from '../../subscriptions/subscription-modal/subscription-modal.component';
35-
import { SubscriptionsDataService } from '@dspace/core/data/subscriptions-data.service';
3636
import { OnClickMenuItemModel } from '../menu-item/models/onclick.model';
3737
import { MenuItemType } from '../menu-item-type.model';
3838
import { PartialMenuSection } from '../menu-provider.model';

0 commit comments

Comments
 (0)