Skip to content

Commit 3fecbc2

Browse files
committed
Fix tests pt. 1
1 parent fbffcca commit 3fecbc2

8 files changed

Lines changed: 77 additions & 105 deletions

File tree

src/app/admin/admin-sidebar/expandable-admin-sidebar-section/expandable-admin-sidebar-section.component.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,14 @@ describe('ExpandableAdminSidebarSectionComponent', () => {
7979
describe('when there are no subsections', () => {
8080
beforeEach(waitForAsync(() => {
8181
TestBed.configureTestingModule({
82-
imports: [NoopAnimationsModule, TranslateModule.forRoot()],
83-
declarations: [ExpandableAdminSidebarSectionComponent, TestComponent],
82+
imports: [NoopAnimationsModule, TranslateModule.forRoot(), ExpandableAdminSidebarSectionComponent, TestComponent],
8483
providers: [
8584
{ provide: 'sectionDataProvider', useValue: { icon: iconString, model: {} } },
8685
{ provide: MenuService, useValue: menuService },
8786
{ provide: CSSVariableService, useClass: CSSVariableServiceStub },
8887
{ provide: Router, useValue: new RouterStub() },
8988
],
90-
}).overrideComponent(ExpandableAdminSidebarSectionComponent, {
91-
})
92-
.compileComponents();
89+
}).compileComponents();
9390
}));
9491

9592
beforeEach(() => {

src/app/shared/dso-page/dso-edit-menu/dso-edit-expandable-menu-section/dso-edit-menu-expandable-section.component.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ describe('DsoEditMenuExpandableSectionComponent', () => {
7474
describe('when there are no subsections', () => {
7575
beforeEach(waitForAsync(() => {
7676
TestBed.configureTestingModule({
77-
imports: [TranslateModule.forRoot()],
78-
declarations: [DsoEditMenuExpandableSectionComponent, TestComponent],
77+
imports: [TranslateModule.forRoot(), DsoEditMenuExpandableSectionComponent, TestComponent],
7978
providers: [
8079
{ provide: 'sectionDataProvider', useValue: dummySection },
8180
{ provide: MenuService, useValue: menuService },

src/app/shared/menu/menu-section/menu-section.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { AbstractMenuSectionComponent } from './abstract-menu-section.component'
2323
@Component({
2424
selector: 'ds-some-menu-section',
2525
template: '',
26+
standalone: true,
2627
})
2728
class SomeMenuSectionComponent extends AbstractMenuSectionComponent {
2829
constructor(
@@ -47,8 +48,7 @@ describe('MenuSectionComponent', () => {
4748
active: false,
4849
} as any;
4950
TestBed.configureTestingModule({
50-
imports: [TranslateModule.forRoot(), NoopAnimationsModule, SomeMenuSectionComponent],
51-
declarations: [AbstractMenuSectionComponent],
51+
imports: [TranslateModule.forRoot(), NoopAnimationsModule, SomeMenuSectionComponent, AbstractMenuSectionComponent],
5252
providers: [
5353
{ provide: Injector, useValue: {} },
5454
{ provide: MenuService, useClass: MenuServiceStub },

src/app/shared/menu/menu.component.spec.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const mockMenuID = 'mock-menuID' as MenuID;
5454
// eslint-disable-next-line @angular-eslint/component-selector
5555
selector: '',
5656
template: '',
57+
standalone: true,
5758
})
5859
@rendersSectionForMenu(mockMenuID, true)
5960
class TestExpandableMenuComponent {
@@ -63,6 +64,7 @@ class TestExpandableMenuComponent {
6364
// eslint-disable-next-line @angular-eslint/component-selector
6465
selector: '',
6566
template: '',
67+
standalone: true,
6668
})
6769
@rendersSectionForMenu(mockMenuID, false)
6870
class TestMenuComponent {
@@ -85,8 +87,6 @@ describe('MenuComponent', () => {
8587
visible: true,
8688
};
8789

88-
const mockMenuID = 'mock-menuID' as MenuID;
89-
9090
const mockStatisticSection = { 'id': 'statistics_site', 'active': true, 'visible': true, 'index': 2, 'type': 'statistics', 'model': { 'type': 1, 'text': 'menu.section.statistics', 'link': 'statistics' } };
9191

9292
let authorizationService: AuthorizationDataService;
@@ -144,16 +144,14 @@ describe('MenuComponent', () => {
144144
});
145145

146146
TestBed.configureTestingModule({
147-
imports: [TranslateModule.forRoot(), NoopAnimationsModule, RouterTestingModule, MenuComponent, StoreModule.forRoot(authReducer, storeModuleConfig)],
147+
imports: [TranslateModule.forRoot(), NoopAnimationsModule, RouterTestingModule, MenuComponent, StoreModule.forRoot(authReducer, storeModuleConfig), TestExpandableMenuComponent, TestMenuComponent],
148148
providers: [
149149
Injector,
150150
{ provide: ThemeService, useValue: getMockThemeService() },
151151
MenuService,
152152
provideMockStore({ initialState }),
153153
{ provide: AuthorizationDataService, useValue: authorizationService },
154154
{ provide: ActivatedRoute, useValue: routeStub },
155-
TestExpandableMenuComponent,
156-
TestMenuComponent,
157155
],
158156
schemas: [NO_ERRORS_SCHEMA],
159157
}).overrideComponent(MenuComponent, {
@@ -272,35 +270,4 @@ describe('MenuComponent', () => {
272270
expect(menuService.collapseMenuPreview).toHaveBeenCalledWith(comp.menuID);
273271
}));
274272
});
275-
276-
describe('when unauthorized statistics', () => {
277-
278-
beforeEach(() => {
279-
(authorizationService as any).isAuthorized.and.returnValue(observableOf(false));
280-
fixture.detectChanges();
281-
});
282-
283-
it('should return observable of empty object', done => {
284-
comp.getAuthorizedStatistics(mockStatisticSection).subscribe((res) => {
285-
expect(res).toEqual({});
286-
done();
287-
});
288-
});
289-
});
290-
291-
describe('get authorized statistics', () => {
292-
293-
beforeEach(() => {
294-
(authorizationService as any).isAuthorized.and.returnValue(observableOf(true));
295-
fixture.detectChanges();
296-
});
297-
298-
it('should return observable of statistics section menu', done => {
299-
comp.getAuthorizedStatistics(mockStatisticSection).subscribe((res) => {
300-
expect(res).toEqual(mockStatisticSection);
301-
done();
302-
});
303-
});
304-
});
305-
306273
});

src/app/shared/menu/menu.component.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,26 @@ import { ActivatedRoute } from '@angular/router';
99
import {
1010
BehaviorSubject,
1111
Observable,
12-
of as observableOf,
1312
Subscription,
1413
} from 'rxjs';
1514
import {
1615
distinctUntilChanged,
1716
map,
18-
mergeMap,
1917
switchMap,
2018
} from 'rxjs/operators';
2119

2220
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
23-
import { FeatureID } from '../../core/data/feature-authorization/feature-id';
2421
import { GenericConstructor } from '../../core/shared/generic-constructor';
2522
import {
2623
hasValue,
2724
isNotEmptyOperator,
2825
} from '../empty.util';
2926
import { ThemeService } from '../theme-support/theme.service';
30-
import { MenuService } from './menu.service';
3127
import { MenuID } from './menu-id.model';
3228
import { getComponentForMenu } from './menu-section.decorator';
3329
import { MenuSection } from './menu-section.model';
3430
import { AbstractMenuSectionComponent } from './menu-section/abstract-menu-section.component';
31+
import { MenuService } from './menu.service';
3532

3633
/**
3734
* A basic implementation of a MenuComponent
@@ -93,8 +90,12 @@ export class MenuComponent implements OnInit, OnDestroy {
9390

9491
private activatedRouteLastChild: ActivatedRoute;
9592

96-
constructor(protected menuService: MenuService, protected injector: Injector, public authorizationService: AuthorizationDataService,
97-
public route: ActivatedRoute, protected themeService: ThemeService,
93+
constructor(
94+
protected menuService: MenuService,
95+
protected injector: Injector,
96+
public authorizationService: AuthorizationDataService,
97+
public route: ActivatedRoute,
98+
protected themeService: ThemeService,
9899
) {
99100
}
100101

@@ -113,12 +114,6 @@ export class MenuComponent implements OnInit, OnDestroy {
113114
// if you return an array from a switchMap it will emit each element as a separate event.
114115
// So this switchMap is equivalent to a subscribe with a forEach inside
115116
switchMap((sections: MenuSection[]) => sections),
116-
mergeMap((section: MenuSection) => {
117-
if (section.id.includes('statistics')) {
118-
return this.getAuthorizedStatistics(section);
119-
}
120-
return observableOf(section);
121-
}),
122117
isNotEmptyOperator(),
123118
switchMap((section: MenuSection) => this.getSectionComponent(section).pipe(
124119
map((component: GenericConstructor<AbstractMenuSectionComponent>) => ({ section, component })),
@@ -146,32 +141,6 @@ export class MenuComponent implements OnInit, OnDestroy {
146141
}
147142
}
148143

149-
/**
150-
* Get section of statistics after checking authorization
151-
*/
152-
getAuthorizedStatistics(section) {
153-
return this.activatedRouteLastChild.data.pipe(
154-
switchMap((data) => {
155-
return this.authorizationService.isAuthorized(FeatureID.CanViewUsageStatistics, this.getObjectUrl(data)).pipe(
156-
map((canViewUsageStatistics: boolean) => {
157-
if (!canViewUsageStatistics) {
158-
return {};
159-
} else {
160-
return section;
161-
}
162-
}));
163-
}),
164-
);
165-
}
166-
167-
/**
168-
* Get statistics route dso data
169-
*/
170-
getObjectUrl(data) {
171-
const object = data.site ? data.site : data.dso?.payload;
172-
return object?._links?.self?.href;
173-
}
174-
175144
/**
176145
* Collapse this menu when it's currently expanded, expand it when its currently collapsed
177146
* @param {Event} event The user event that triggered this method

src/app/shared/menu/providers/browse.menu.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('BrowseMenuProvider', () => {
5151

5252

5353
let provider: BrowseMenuProvider;
54-
let browseServiceStub = new BrowseServiceStub();
54+
let browseServiceStub = BrowseServiceStub;
5555

5656
beforeEach(() => {
5757
spyOn(browseServiceStub, 'getBrowseDefinitions').and.returnValue(

src/app/shared/menu/providers/statistics.menu.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { inject } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
23
import { TranslateModule } from '@ngx-translate/core';
4+
import { of as observableOf } from 'rxjs';
5+
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
36

47
import { Item } from '../../../core/shared/item.model';
58
import { ITEM } from '../../../core/shared/item.resource-type';
@@ -34,6 +37,18 @@ describe('StatisticsMenuProvider', () => {
3437
},
3538
];
3639

40+
const expectedSectionsForItemInvisible: PartialMenuSection[] = [
41+
{
42+
visible: false,
43+
model: {
44+
type: MenuItemType.LINK,
45+
text: 'menu.section.statistics',
46+
link: `statistics/items/test-item-uuid`,
47+
},
48+
icon: 'chart-line',
49+
},
50+
];
51+
3752
let provider: StatisticsMenuProvider;
3853

3954
const item: Item = Object.assign(new Item(), {
@@ -57,13 +72,18 @@ describe('StatisticsMenuProvider', () => {
5772
}],
5873
},
5974
});
75+
let authorizationService: AuthorizationDataService;
6076

6177
beforeEach(() => {
78+
authorizationService = jasmine.createSpyObj('authorizationService', {
79+
isAuthorized: observableOf(true),
80+
});
6281

6382
TestBed.configureTestingModule({
6483
imports: [TranslateModule.forRoot()],
6584
providers: [
6685
StatisticsMenuProvider,
86+
{ provide: AuthorizationDataService, useValue: authorizationService },
6787
],
6888
});
6989
provider = TestBed.inject(StatisticsMenuProvider);
@@ -86,6 +106,13 @@ describe('StatisticsMenuProvider', () => {
86106
done();
87107
});
88108
});
109+
it('should not return anything if not authorized to view statistics', (done) => {
110+
(TestBed.inject(AuthorizationDataService) as any).isAuthorized.and.returnValue(observableOf(false));
111+
provider.getSectionsForContext(item).subscribe((sections) => {
112+
expect(sections).toEqual(expectedSectionsForItemInvisible);
113+
done();
114+
});
115+
});
89116
});
90117

91118
describe('getRouteContext', () => {

src/app/shared/menu/providers/statistics.menu.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ import {
1212
RouterStateSnapshot,
1313
} from '@angular/router';
1414
import {
15+
combineLatest,
16+
map,
1517
Observable,
1618
of,
1719
} from 'rxjs';
1820

1921
import { getDSORoute } from '../../../app-routing-paths';
22+
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
23+
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
2024
import { RemoteData } from '../../../core/data/remote-data';
2125
import { DSpaceObject } from '../../../core/shared/dspace-object.model';
2226
import {
@@ -34,6 +38,11 @@ import { AbstractRouteContextMenuProvider } from './helper-providers/route-conte
3438
*/
3539
@Injectable()
3640
export class StatisticsMenuProvider extends AbstractRouteContextMenuProvider<DSpaceObject> {
41+
constructor(
42+
protected authorizationService: AuthorizationDataService,
43+
) {
44+
super();
45+
}
3746

3847
public getRouteContext(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<DSpaceObject> {
3948
let dsoRD: RemoteData<DSpaceObject> = route.data.dso;
@@ -51,28 +60,32 @@ export class StatisticsMenuProvider extends AbstractRouteContextMenuProvider<DSp
5160
}
5261

5362
public getSectionsForContext(dso: DSpaceObject): Observable<PartialMenuSection[]> {
63+
return combineLatest([
64+
this.authorizationService.isAuthorized(FeatureID.CanViewUsageStatistics, dso?._links.self.href),
65+
]).pipe(
66+
map(([authorized]) => {
67+
let link = `statistics`;
5468

55-
let link = `statistics`;
56-
57-
let dsoRoute;
58-
if (hasValue(dso)) {
59-
dsoRoute = getDSORoute(dso);
60-
if (hasValue(dsoRoute)) {
61-
link = `statistics${dsoRoute}`;
62-
}
63-
}
69+
let dsoRoute;
70+
if (hasValue(dso)) {
71+
dsoRoute = getDSORoute(dso);
72+
if (hasValue(dsoRoute)) {
73+
link = `statistics${dsoRoute}`;
74+
}
75+
}
6476

65-
return of([
66-
{
67-
visible: true,
68-
model: {
69-
type: MenuItemType.LINK,
70-
text: 'menu.section.statistics',
71-
link,
72-
},
73-
icon: 'chart-line',
74-
},
75-
] as PartialMenuSection[]);
77+
return [
78+
{
79+
visible: authorized,
80+
model: {
81+
type: MenuItemType.LINK,
82+
text: 'menu.section.statistics',
83+
link,
84+
},
85+
icon: 'chart-line',
86+
},
87+
];
88+
}),
89+
);
7690
}
77-
7891
}

0 commit comments

Comments
 (0)