Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions src/app/handle-page/handle-table/handle-table.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ import {
import { ReactiveFormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import {
of,
Subject,
} from 'rxjs';

import { HandleDataService } from '../../core/data/handle-data.service';
import { buildPaginatedList } from '../../core/data/paginated-list.model';
import {
buildPaginatedList,
PaginatedList,
} from '../../core/data/paginated-list.model';
import { RemoteData } from '../../core/data/remote-data';
import { RequestService } from '../../core/data/request.service';
import { Handle } from '../../core/handle/handle.model';
import { PaginationService } from '../../core/pagination/pagination.service';
import { PageInfo } from '../../core/shared/page-info.model';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import {
createSuccessfulRemoteDataObject,
createSuccessfulRemoteDataObject$,
} from '../../shared/remote-data.utils';
import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub';
import { PaginationServiceStub } from '../../shared/testing/pagination-service.stub';
import { RouterStub } from '../../shared/testing/router.stub';
Expand All @@ -24,7 +34,10 @@ import {
HANDLE_TABLE_EDIT_HANDLE_PATH,
} from '../handle-page-routing-paths';
import { HandleTableComponent } from './handle-table.component';
import { defaultPagination } from './handle-table-pagination';
import {
defaultPagination,
paginationID,
} from './handle-table-pagination';

const selectedHandleId = 1;
const otherHandleId = 47;
Expand Down Expand Up @@ -75,9 +88,11 @@ describe('HandleTableComponent', () => {
let handleDataService: HandleDataService;
let requestService: RequestService;
let notificationService: NotificationsServiceStub;
let paginationService: PaginationServiceStub;

beforeEach(async () => {
notificationService = new NotificationsServiceStub();
paginationService = new PaginationServiceStub();
handleDataService = jasmine.createSpyObj('handleDataService', {
findAll: mockHandleRD$,
getLinkPath: of(''),
Expand All @@ -99,7 +114,7 @@ describe('HandleTableComponent', () => {
{ provide: RequestService, useValue: requestService },
{ provide: HandleDataService, useValue: handleDataService },
{ provide: Router, useValue: new RouterStub() },
{ provide: PaginationService, useValue: new PaginationServiceStub() },
{ provide: PaginationService, useValue: paginationService },
{ provide: NotificationsService, useValue: notificationService },
],
})
Expand Down Expand Up @@ -142,6 +157,31 @@ describe('HandleTableComponent', () => {
expect((component as HandleTableComponent).handlesRD$).not.toBeNull();
});

it('should move the paginator back to page 1 without a new history entry when a search starts', () => {
(component as HandleTableComponent).ngOnInit();
(component as HandleTableComponent).searchOption = (component as HandleTableComponent).handleOption;
(component as HandleTableComponent).searchQuery = mockHandle.handle;
(component as HandleTableComponent).searchHandles();

expect(paginationService.updateRoute).toHaveBeenCalledWith(paginationID, { page: 1 }, {}, false, { replaceUrl: true });
expect((component as any).handleDataService.findAll).toHaveBeenCalledWith(
jasmine.objectContaining({ currentPage: 1 }), false);
});

it('should not let a load that is still running overwrite the rows of a newer search', () => {
const firstLoad$ = new Subject<RemoteData<PaginatedList<Handle>>>();
const searchResult = createSuccessfulRemoteDataObject(buildPaginatedList(new PageInfo(), [otherMockHandle]));
(handleDataService.findAll as jasmine.Spy).and.returnValues(firstLoad$, of(searchResult));

(component as HandleTableComponent).ngOnInit();
(component as HandleTableComponent).searchOption = (component as HandleTableComponent).handleOption;
(component as HandleTableComponent).searchQuery = otherMockHandle.handle;
(component as HandleTableComponent).searchHandles();
firstLoad$.next(createSuccessfulRemoteDataObject(buildPaginatedList(new PageInfo(), [mockHandle])));

expect((component as HandleTableComponent).handlesRD$.value.payload.page).toEqual([otherMockHandle]);
});

it('should not allow to have two or more selected handles', () => {
const firstId = 1;
const secondId = 2;
Expand Down
19 changes: 17 additions & 2 deletions src/app/handle-page/handle-table/handle-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CommonModule } from '@angular/common';
import {
ChangeDetectorRef,
Component,
OnDestroy,
OnInit,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
Expand All @@ -14,6 +15,7 @@ import {
import {
BehaviorSubject,
combineLatest,
Subscription,
} from 'rxjs';
import {
scan,
Expand Down Expand Up @@ -88,7 +90,7 @@ export const RESOURCE_TYPE_SEARCH_OPTION = 'resourceTypeId';
templateUrl: './handle-table.component.html',
styleUrls: ['./handle-table.component.scss'],
})
export class HandleTableComponent implements OnInit {
export class HandleTableComponent implements OnInit, OnDestroy {

constructor(private handleDataService: HandleDataService,
private paginationService: PaginationService,
Expand Down Expand Up @@ -170,6 +172,11 @@ export class HandleTableComponent implements OnInit {
*/
selectedHandle = null;

/**
* The running load of the table rows. A new load cancels it, so an older response cannot overwrite newer rows.
*/
private handlesSub: Subscription;

ngOnInit(): void {
this.handleRoute = getHandleTableModulePath();
this.initializePaginationOptions();
Expand All @@ -181,6 +188,10 @@ export class HandleTableComponent implements OnInit {
this.resourceTypeOption = this.translateService.instant('handle-table.table.resource-type');
}

ngOnDestroy(): void {
this.handlesSub?.unsubscribe();
}

/**
* Load all handles based on the pagination and sorting options.
*/
Expand All @@ -193,7 +204,8 @@ export class HandleTableComponent implements OnInit {
const currentSort$ = this.getCurrentSort();
const searchTerm$ = new BehaviorSubject<string>(this.searchQuery);

combineLatest([currentPagination$, currentSort$, searchTerm$]).pipe(
this.handlesSub?.unsubscribe();
this.handlesSub = combineLatest([currentPagination$, currentSort$, searchTerm$]).pipe(
scan((prevState, [currentPagination, currentSort, searchTerm]) => {
// If search term has changed, reset to page 1; otherwise, keep current page
const currentPage = prevState.searchTerm !== searchTerm ? 1 : currentPagination.currentPage;
Expand Down Expand Up @@ -474,6 +486,9 @@ export class HandleTableComponent implements OnInit {
}

this.sortConfiguration.field = parsedSearchOption + ':' + parsedSearchQuery;
// The results start on page 1. Replace the URL instead of adding a history entry,
// because Back cannot restore the search filter, which is not in the URL.
this.paginationService.updateRoute(this.options.id, { page: 1 }, {}, false, { replaceUrl: true });
this.getAllHandles();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
</ds-claimed-task-actions-loader>
}

<button class="btn btn-primary workflow-curate" ngbTooltip="{{'submission.workflow.generic.curate-help' | translate}}"
[routerLink]="[getWorkflowItemCurationRoute()]" [queryParams]="{item_id: item?.handle}">
<i class="fa fa-filter"></i> {{"submission.workflow.generic.curate" | translate}}
</button>
@if (canCurate$ | async) {
<button class="btn btn-primary workflow-curate" ngbTooltip="{{'submission.workflow.generic.curate-help' | translate}}"
[routerLink]="[getWorkflowItemCurationRoute()]" [queryParams]="{item_id: item?.handle}">
<i class="fa fa-filter"></i> {{"submission.workflow.generic.curate" | translate}}
</button>
}
@if (showViewButton) {
<button class="btn btn-primary workflow-view" ngbTooltip="{{'submission.workflow.generic.view-help' | translate}}"
[routerLink]="[getWorkflowItemViewRoute(workflowitem)]">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
} from '@ngx-translate/core';
import { of } from 'rxjs';

import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { RequestService } from '../../../core/data/request.service';
import { WorkflowActionDataService } from '../../../core/data/workflow-action-data.service';
import { Item } from '../../../core/shared/item.model';
Expand Down Expand Up @@ -54,6 +56,7 @@ let mockDataService;
let searchService;
let requestServce;
let workflowActionService: WorkflowActionDataService;
let authorizationService;

let item;
let rdItem;
Expand Down Expand Up @@ -108,6 +111,9 @@ function init() {
workflowActionService = jasmine.createSpyObj('workflowActionService', {
findById: createSuccessfulRemoteDataObject$(workflowAction),
});
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: of(true),
});
}

describe('ClaimedTaskActionsComponent', () => {
Expand All @@ -132,6 +138,7 @@ describe('ClaimedTaskActionsComponent', () => {
{ provide: SearchService, useValue: searchService },
{ provide: RequestService, useValue: requestServce },
{ provide: WorkflowActionDataService, useValue: workflowActionService },
{ provide: AuthorizationDataService, useValue: authorizationService },
],
schemas: [NO_ERRORS_SCHEMA],
}).overrideComponent(ClaimedTaskActionsComponent, {
Expand Down Expand Up @@ -196,19 +203,31 @@ describe('ClaimedTaskActionsComponent', () => {

}));

it('should display a curate button', waitForAsync(() => {
it('should display a curate button to a site administrator', waitForAsync(() => {
component.object = null;
component.initObjects(mockObject);
fixture.detectChanges();

fixture.whenStable().then(() => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.AdministratorOf);
const debugElement = fixture.debugElement.query(By.css('.workflow-curate'));
expect(debugElement).toBeTruthy();
expect(debugElement.nativeElement.innerText.trim()).toBe('submission.workflow.generic.curate');
});

}));

it('should not display a curate button to a reviewer who is not a site administrator', waitForAsync(() => {
authorizationService.isAuthorized.and.returnValue(of(false));
component.ngOnInit();
fixture.detectChanges();

fixture.whenStable().then(() => {
expect(fixture.debugElement.query(By.css('.workflow-curate'))).toBeNull();
});

}));

it('getWorkflowItemViewRoute should return the combined uri to show a workspaceitem', waitForAsync(() => {
const href = component.getWorkflowItemViewRoute(workflowitem);
expect(href).toEqual('/workflowitems/333/view');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
} from '@ngx-translate/core';
import { Observable } from 'rxjs';

import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { RemoteData } from '../../../core/data/remote-data';
import { RequestService } from '../../../core/data/request.service';
import { WorkflowActionDataService } from '../../../core/data/workflow-action-data.service';
Expand Down Expand Up @@ -75,6 +77,11 @@ export class ClaimedTaskActionsComponent extends MyDSpaceActionsComponent<Claime
*/
public actionRD$: Observable<RemoteData<WorkflowAction>>;

/**
* Whether the current user may open the curation page, which only site administrators can
*/
public canCurate$: Observable<boolean>;

/**
* Initialize instance variables
*
Expand All @@ -85,6 +92,7 @@ export class ClaimedTaskActionsComponent extends MyDSpaceActionsComponent<Claime
* @param {SearchService} searchService
* @param {RequestService} requestService
* @param workflowActionService
* @param authorizationService
*/
constructor(protected injector: Injector,
protected router: Router,
Expand All @@ -93,6 +101,7 @@ export class ClaimedTaskActionsComponent extends MyDSpaceActionsComponent<Claime
protected searchService: SearchService,
protected requestService: RequestService,
protected workflowActionService: WorkflowActionDataService,
protected authorizationService: AuthorizationDataService,
) {
super(ClaimedTask.type, injector, router, notificationsService, translate, searchService, requestService);
}
Expand All @@ -103,6 +112,7 @@ export class ClaimedTaskActionsComponent extends MyDSpaceActionsComponent<Claime
ngOnInit() {
this.initObjects(this.object);
this.initAction(this.object);
this.canCurate$ = this.authorizationService.isAuthorized(FeatureID.AdministratorOf);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { first } from 'rxjs/operators';

import { SUBMISSION_LINKS_TO_FOLLOW } from '../core/submission/resolver/submission-links-to-follow';
import { WorkflowItemDataService } from '../core/submission/workflowitem-data.service';
import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils';
import { workflowItemPageResolver } from './workflow-item-page.resolver';
Expand All @@ -12,7 +13,7 @@ describe('workflowItemPageResolver', () => {

beforeEach(() => {
wfiService = {
findById: (id: string) => createSuccessfulRemoteDataObject$({ id }),
findById: jasmine.createSpy('findById').and.callFake((id: string) => createSuccessfulRemoteDataObject$({ id })),
} as any;
resolver = workflowItemPageResolver;
});
Expand All @@ -27,5 +28,14 @@ describe('workflowItemPageResolver', () => {
},
);
});

it('should not resolve from a cached copy of the workflow item', (done) => {
resolver({ params: { id: uuid } } as any, undefined, wfiService)
.pipe(first())
.subscribe(() => {
expect(wfiService.findById).toHaveBeenCalledWith(uuid, false, false, ...SUBMISSION_LINKS_TO_FOLLOW);
done();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export const workflowItemPageResolver: ResolveFn<RemoteData<WorkflowItem>> = (
state: RouterStateSnapshot,
workflowItemService: WorkflowItemDataService = inject(WorkflowItemDataService),
): Observable<RemoteData<WorkflowItem>> => {
// No cached copy: the edit form must not start while a GET for this item is still running,
// or the form takes that response, which has no embedded collection, and stays on "Loading".
return workflowItemService.findById(
route.params.id,
true,
false,
false,
...SUBMISSION_LINKS_TO_FOLLOW,
).pipe(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { first } from 'rxjs/operators';

import { SUBMISSION_LINKS_TO_FOLLOW } from '../core/submission/resolver/submission-links-to-follow';
import { WorkspaceitemDataService } from '../core/submission/workspaceitem-data.service';
import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils';
import { workspaceItemPageResolver } from './workspace-item-page.resolver';
Expand All @@ -12,7 +13,7 @@ describe('workspaceItemPageResolver', () => {

beforeEach(() => {
wsiService = {
findById: (id: string) => createSuccessfulRemoteDataObject$({ id }),
findById: jasmine.createSpy('findById').and.callFake((id: string) => createSuccessfulRemoteDataObject$({ id })),
} as any;
resolver = workspaceItemPageResolver;
});
Expand All @@ -27,5 +28,14 @@ describe('workspaceItemPageResolver', () => {
},
);
});

it('should not resolve from a cached copy of the workspace item', (done) => {
resolver({ params: { id: uuid } } as any, undefined, wsiService)
.pipe(first())
.subscribe(() => {
expect(wsiService.findById).toHaveBeenCalledWith(uuid, false, false, ...SUBMISSION_LINKS_TO_FOLLOW);
done();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export const workspaceItemPageResolver: ResolveFn<RemoteData<WorkspaceItem>> = (
state: RouterStateSnapshot,
workspaceItemService: WorkspaceitemDataService = inject(WorkspaceitemDataService),
): Observable<RemoteData<WorkspaceItem>> => {
// No cached copy: the edit form must not start while a GET for this item is still running,
// or the form takes that response, which has no embedded collection, and stays on "Loading".
return workspaceItemService.findById(route.params.id,
true,
false,
false,
...SUBMISSION_LINKS_TO_FOLLOW,
).pipe(
Expand Down
Loading