add tests for dsoPageSomeFeatureGuard

This commit is contained in:
Art Lowel
2024-05-02 11:22:58 +02:00
parent 0ec1d28bd1
commit aecf2c7b61

View File

@@ -4,12 +4,17 @@ import { RemoteData } from '../../remote-data';
import { Observable, of as observableOf } from 'rxjs'; import { Observable, of as observableOf } from 'rxjs';
import { DSpaceObject } from '../../../shared/dspace-object.model'; import { DSpaceObject } from '../../../shared/dspace-object.model';
import { AuthService } from '../../../auth/auth.service'; import { AuthService } from '../../../auth/auth.service';
import { dsoPageSomeFeatureGuard } from './dso-page-some-feature.guard'; import {
dsoPageSomeFeatureGuard,
defaultDSOGetObjectUrl,
getRouteWithDSOId
} from './dso-page-some-feature.guard';
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { FeatureID } from '../feature-id'; import { FeatureID } from '../feature-id';
import { createSuccessfulRemoteDataObject$ } from '../../../../shared/remote-data.utils';
describe('DsoPageSomeFeatureGuard', () => { describe('dsoPageSomeFeatureGuard and its functions', () => {
let authorizationService: AuthorizationDataService; let authorizationService: AuthorizationDataService;
let router: Router; let router: Router;
let authService: AuthService; let authService: AuthService;
@@ -25,16 +30,14 @@ describe('DsoPageSomeFeatureGuard', () => {
object = { object = {
self: 'test-selflink' self: 'test-selflink'
} as DSpaceObject; } as DSpaceObject;
featureIds = [FeatureID.LoginOnBehalfOf, FeatureID.CanDelete];
authorizationService = jasmine.createSpyObj('authorizationService', { authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true) isAuthorized: observableOf(true)
}); });
router = jasmine.createSpyObj('router', { router = jasmine.createSpyObj('router', {
parseUrl: {} parseUrl: {}
}); });
// resolver = jasmine.createSpyObj('resolver', { resolver = () => createSuccessfulRemoteDataObject$(object);
// resolve: createSuccessfulRemoteDataObject$(object)
// });
authService = jasmine.createSpyObj('authService', { authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true) isAuthenticated: observableOf(true)
}); });
@@ -63,33 +66,38 @@ describe('DsoPageSomeFeatureGuard', () => {
init(); init();
}); });
describe('getObjectUrl', () => {
it('should return the resolved object\'s selflink', (done) => {
describe('defaultDSOGetObjectUrl', () => {
it('should return the resolved object\'s selflink', (done) => {
defaultDSOGetObjectUrl(resolver)(route, undefined).subscribe((selflink) => {
expect(selflink).toEqual(object.self);
done();
});
});
})
describe('getRouteWithDSOId', () => {
it('should return the route that has the UUID of the DSO', () => {
const foundRoute = getRouteWithDSOId(route);
expect(foundRoute).toBe(parentRoute);
});
})
describe('dsoPageSomeFeatureGuard', () => {
it('should call authorizationService.isAuthenticated with the appropriate arguments', (done) => {
const result$ = TestBed.runInInjectionContext(() => { const result$ = TestBed.runInInjectionContext(() => {
return dsoPageSomeFeatureGuard( return dsoPageSomeFeatureGuard(
() => resolver, () => observableOf(featureIds) () => resolver, () => observableOf(featureIds)
)(route, { url: 'current-url' } as any); )(route, { url: 'current-url' } as any);
}) as Observable<boolean | UrlTree>; }) as Observable<boolean | UrlTree>;
console.log('result$', result$);
result$.subscribe(() => { result$.subscribe(() => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(featureIds[0]); featureIds.forEach((featureId: FeatureID) => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(featureId, object.self, undefined);
});
done(); done();
}); });
// guard.getObjectUrl(route, undefined).subscribe((selflink) => {
// expect(selflink).toEqual(object.self);
// done();
// });
}); });
}); });
// describe('getRouteWithDSOId', () => {
// it('should return the route that has the UUID of the DSO', () => {
// const foundRoute = (guard as any).getRouteWithDSOId(route);
// expect(foundRoute).toBe(parentRoute);
// });
// });
}); });