diff --git a/src/app/core/breadcrumbs/community-breadcrumb.resolver.spec.ts b/src/app/core/breadcrumbs/community-breadcrumb.resolver.spec.ts new file mode 100644 index 00000000000..b763bc11c99 --- /dev/null +++ b/src/app/core/breadcrumbs/community-breadcrumb.resolver.spec.ts @@ -0,0 +1,91 @@ +import { Community } from '../shared/community.model'; +import { createSuccessfulRemoteDataObject$ } from '../utilities/remote-data.utils'; +import { communityBreadcrumbResolver } from './community-breadcrumb.resolver'; + +describe('communityBreadcrumbResolver', () => { + let breadcrumbService: any; + let communityDataService: any; + let community: Community; + let parentCommunity: Community; + let state: any; + + beforeEach(() => { + community = Object.assign(new Community(), { + uuid: '1234-65487-12354-1235', + type: 'community', + }); + parentCommunity = Object.assign(new Community(), { + uuid: '4321-76548-45321-5321', + type: 'community', + }); + breadcrumbService = {}; + state = {}; + communityDataService = jasmine.createSpyObj('communityDataService', ['findById']); + communityDataService.findById.and.callFake((uuid: string) => + createSuccessfulRemoteDataObject$(uuid === parentCommunity.uuid ? parentCommunity : community), + ); + }); + + it('should resolve the community from the route id', () => { + const resolvedConfig = (communityBreadcrumbResolver as any)( + { data: {}, params: { id: community.uuid }, queryParams: {} } as any, + state, + breadcrumbService, + communityDataService, + ); + + let emitted = false; + resolvedConfig.subscribe((config) => { + emitted = true; + expect(config).toEqual({ + provider: breadcrumbService, + key: community, + url: `/communities/${community.uuid}`, + }); + }); + expect(emitted).toBeTrue(); + expect(communityDataService.findById.calls.mostRecent().args[0]).toEqual(community.uuid); + }); + + it('should resolve the parent community from the configured query parameter', () => { + const resolvedConfig = (communityBreadcrumbResolver as any)( + { + data: { breadcrumbQueryParam: 'parent' }, + params: {}, + queryParams: { parent: parentCommunity.uuid }, + } as any, + state, + breadcrumbService, + communityDataService, + ); + + let emitted = false; + resolvedConfig.subscribe((config) => { + emitted = true; + expect(config).toEqual({ + provider: breadcrumbService, + key: parentCommunity, + url: `/communities/${parentCommunity.uuid}`, + }); + }); + expect(emitted).toBeTrue(); + expect(communityDataService.findById.calls.mostRecent().args[0]).toEqual(parentCommunity.uuid); + }); + + it('should not request a community when the configured query parameter is absent', () => { + const resolvedConfig = (communityBreadcrumbResolver as any)( + { data: { breadcrumbQueryParam: 'parent' }, params: {}, queryParams: {} } as any, + state, + breadcrumbService, + communityDataService, + ); + + let emitted = false; + resolvedConfig.subscribe((config) => { + emitted = true; + expect(config).toBeUndefined(); + }); + expect(emitted).toBeTrue(); + expect(communityDataService.findById).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/core/breadcrumbs/community-breadcrumb.resolver.ts b/src/app/core/breadcrumbs/community-breadcrumb.resolver.ts index c92b3fe0e5a..9b148d0ccaf 100644 --- a/src/app/core/breadcrumbs/community-breadcrumb.resolver.ts +++ b/src/app/core/breadcrumbs/community-breadcrumb.resolver.ts @@ -5,7 +5,10 @@ import { RouterStateSnapshot, } from '@angular/router'; import { hasValue } from '@dspace/shared/utils/empty.util'; -import { Observable } from 'rxjs'; +import { + Observable, + of, +} from 'rxjs'; import { CommunityDataService } from '../data/community-data.service'; import { @@ -31,11 +34,15 @@ export const communityBreadcrumbResolver: ResolveFn> dataService: CommunityDataService = inject(CommunityDataService), ): Observable> => { const linksToFollow: FollowLinkConfig[] = COMMUNITY_PAGE_LINKS_TO_FOLLOW as FollowLinkConfig[]; - if (hasValue(route.data.breadcrumbQueryParam) && hasValue(route.queryParams[route.data.breadcrumbQueryParam])) { + if (hasValue(route.data.breadcrumbQueryParam)) { + const queryParam = route.queryParams[route.data.breadcrumbQueryParam]; + if (!hasValue(queryParam)) { + return of(undefined); + } return DSOBreadcrumbResolverByUuid( route, state, - route.queryParams[route.data.breadcrumbQueryParam], + queryParam, breadcrumbService, dataService, ...linksToFollow,