1
0

118223: Add item-bitstreams component tests

This commit is contained in:
Andreas Awouters
2024-10-07 11:49:54 +02:00
parent 0bdb5742e0
commit e8379db987
3 changed files with 192 additions and 17 deletions

View File

@@ -26,9 +26,7 @@ import { createPaginatedList } from '../../../shared/testing/utils.test';
import { FieldChangeType } from '../../../core/data/object-updates/field-change-type.model'; import { FieldChangeType } from '../../../core/data/object-updates/field-change-type.model';
import { BitstreamDataServiceStub } from '../../../shared/testing/bitstream-data-service.stub'; import { BitstreamDataServiceStub } from '../../../shared/testing/bitstream-data-service.stub';
import { ItemBitstreamsService } from './item-bitstreams.service'; import { ItemBitstreamsService } from './item-bitstreams.service';
import { ResponsiveTableSizes } from '../../../shared/responsive-table-sizes/responsive-table-sizes'; import { getItemBitstreamsServiceStub, ItemBitstreamsServiceStub } from './item-bitstreams.service.stub';
import { ResponsiveColumnSizes } from '../../../shared/responsive-table-sizes/responsive-column-sizes';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
let comp: ItemBitstreamsComponent; let comp: ItemBitstreamsComponent;
let fixture: ComponentFixture<ItemBitstreamsComponent>; let fixture: ComponentFixture<ItemBitstreamsComponent>;
@@ -80,7 +78,7 @@ let objectCache: ObjectCacheService;
let requestService: RequestService; let requestService: RequestService;
let searchConfig: SearchConfigurationService; let searchConfig: SearchConfigurationService;
let bundleService: BundleDataService; let bundleService: BundleDataService;
let itemBitstreamsService: ItemBitstreamsService; let itemBitstreamsService: ItemBitstreamsServiceStub;
describe('ItemBitstreamsComponent', () => { describe('ItemBitstreamsComponent', () => {
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
@@ -152,18 +150,7 @@ describe('ItemBitstreamsComponent', () => {
patch: createSuccessfulRemoteDataObject$({}), patch: createSuccessfulRemoteDataObject$({}),
}); });
itemBitstreamsService = jasmine.createSpyObj('itemBitstreamsService', { itemBitstreamsService = getItemBitstreamsServiceStub();
getColumnSizes: new ResponsiveTableSizes([
new ResponsiveColumnSizes(2, 2, 3, 4, 4),
new ResponsiveColumnSizes(2, 3, 3, 3, 3),
new ResponsiveColumnSizes(2, 2, 2, 2, 2),
new ResponsiveColumnSizes(6, 5, 4, 3, 3)
]),
getSelectedBitstream$: observableOf({}),
getInitialBundlesPaginationOptions: new PaginationComponentOptions(),
removeMarkedBitstreams: createSuccessfulRemoteDataObject$({}),
displayNotifications: undefined,
});
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()], imports: [TranslateModule.forRoot()],
@@ -218,4 +205,114 @@ describe('ItemBitstreamsComponent', () => {
expect(objectUpdatesService.reinstateFieldUpdates).toHaveBeenCalledWith(bundle.self); expect(objectUpdatesService.reinstateFieldUpdates).toHaveBeenCalledWith(bundle.self);
}); });
}); });
describe('moveUp', () => {
it('should move the selected bitstream up', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(true);
const event = {
preventDefault: () => {/* Intentionally empty */},
} as KeyboardEvent;
comp.moveUp(event);
expect(itemBitstreamsService.moveSelectedBitstreamUp).toHaveBeenCalled();
});
it('should not do anything if no bitstream is selected', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(false);
const event = {
preventDefault: () => {/* Intentionally empty */},
} as KeyboardEvent;
comp.moveUp(event);
expect(itemBitstreamsService.moveSelectedBitstreamUp).not.toHaveBeenCalled();
});
});
describe('moveDown', () => {
it('should move the selected bitstream down', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(true);
const event = {
preventDefault: () => {/* Intentionally empty */},
} as KeyboardEvent;
comp.moveDown(event);
expect(itemBitstreamsService.moveSelectedBitstreamDown).toHaveBeenCalled();
});
it('should not do anything if no bitstream is selected', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(false);
const event = {
preventDefault: () => {/* Intentionally empty */},
} as KeyboardEvent;
comp.moveDown(event);
expect(itemBitstreamsService.moveSelectedBitstreamDown).not.toHaveBeenCalled();
});
});
describe('cancelSelection', () => {
it('should cancel the selection', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(true);
const event = {
preventDefault: () => {/* Intentionally empty */},
} as KeyboardEvent;
comp.cancelSelection(event);
expect(itemBitstreamsService.cancelSelection).toHaveBeenCalled();
});
it('should not do anything if no bitstream is selected', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(false);
const event = {
preventDefault: () => {/* Intentionally empty */},
} as KeyboardEvent;
comp.cancelSelection(event);
expect(itemBitstreamsService.cancelSelection).not.toHaveBeenCalled();
});
});
describe('clearSelection', () => {
it('should clear the selection', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(true);
const event = {
target: document.createElement('BODY'),
preventDefault: () => {/* Intentionally empty */},
} as unknown as KeyboardEvent;
comp.clearSelection(event);
expect(itemBitstreamsService.clearSelection).toHaveBeenCalled();
});
it('should not do anything if no bitstream is selected', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(false);
const event = {
target: document.createElement('BODY'),
preventDefault: () => {/* Intentionally empty */},
} as unknown as KeyboardEvent;
comp.clearSelection(event);
expect(itemBitstreamsService.clearSelection).not.toHaveBeenCalled();
});
it('should not do anything if the event target is not \'BODY\'', () => {
itemBitstreamsService.hasSelectedBitstream.and.returnValue(true);
const event = {
target: document.createElement('NOT-BODY'),
preventDefault: () => {/* Intentionally empty */},
} as unknown as KeyboardEvent;
comp.clearSelection(event);
expect(itemBitstreamsService.clearSelection).not.toHaveBeenCalled();
});
});
}); });

View File

@@ -137,7 +137,11 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
// Only when no specific element is in focus do we want to clear the currently selected bitstream // Only when no specific element is in focus do we want to clear the currently selected bitstream
// Otherwise we might clear the selection when a different action was intended, e.g. clicking a button or selecting // Otherwise we might clear the selection when a different action was intended, e.g. clicking a button or selecting
// a different bitstream. // a different bitstream.
if (event.target instanceof Element && event.target.tagName === 'BODY') { if (
this.itemBitstreamsService.hasSelectedBitstream() &&
event.target instanceof Element &&
event.target.tagName === 'BODY'
) {
event.preventDefault(); event.preventDefault();
this.itemBitstreamsService.clearSelection(); this.itemBitstreamsService.clearSelection();
} }

View File

@@ -0,0 +1,74 @@
import { of } from 'rxjs';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import { ResponsiveTableSizes } from '../../../shared/responsive-table-sizes/responsive-table-sizes';
import { ResponsiveColumnSizes } from '../../../shared/responsive-table-sizes/responsive-column-sizes';
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
export function getItemBitstreamsServiceStub(): ItemBitstreamsServiceStub {
return new ItemBitstreamsServiceStub();
}
export class ItemBitstreamsServiceStub {
getSelectedBitstream$ = jasmine.createSpy('getSelectedBitstream$').and
.returnValue(of(null));
getSelectedBitstream = jasmine.createSpy('getSelectedBitstream').and
.returnValue(null);
hasSelectedBitstream = jasmine.createSpy('hasSelectedBitstream').and
.returnValue(false);
selectBitstreamEntry = jasmine.createSpy('selectBitstreamEntry');
clearSelection = jasmine.createSpy('clearSelection');
cancelSelection = jasmine.createSpy('cancelSelection');
moveSelectedBitstreamUp = jasmine.createSpy('moveSelectedBitstreamUp');
moveSelectedBitstreamDown = jasmine.createSpy('moveSelectedBitstreamDown');
performBitstreamMoveRequest = jasmine.createSpy('performBitstreamMoveRequest');
getInitialBundlesPaginationOptions = jasmine.createSpy('getInitialBundlesPaginationOptions').and
.returnValue(new PaginationComponentOptions());
getInitialBitstreamsPaginationOptions = jasmine.createSpy('getInitialBitstreamsPaginationOptions').and
.returnValue(new PaginationComponentOptions());
getColumnSizes = jasmine.createSpy('getColumnSizes').and
.returnValue(
new ResponsiveTableSizes([
new ResponsiveColumnSizes(2, 2, 3, 4, 4),
new ResponsiveColumnSizes(2, 3, 3, 3, 3),
new ResponsiveColumnSizes(2, 2, 2, 2, 2),
new ResponsiveColumnSizes(6, 5, 4, 3, 3)
])
);
displayNotifications = jasmine.createSpy('displayNotifications');
displayFailedResponseNotifications = jasmine.createSpy('displayFailedResponseNotifications');
displayErrorNotification = jasmine.createSpy('displayErrorNotification');
displaySuccessFulResponseNotifications = jasmine.createSpy('displaySuccessFulResponseNotifications');
displaySuccessNotification = jasmine.createSpy('displaySuccessNotification');
removeMarkedBitstreams = jasmine.createSpy('removeMarkedBitstreams').and
.returnValue(createSuccessfulRemoteDataObject$({}));
mapBitstreamsToTableEntries = jasmine.createSpy('mapBitstreamsToTableEntries').and
.returnValue([]);
nameToHeader = jasmine.createSpy('nameToHeader').and.returnValue('header');
stripWhiteSpace = jasmine.createSpy('stripWhiteSpace').and.returnValue('string');
announceSelect = jasmine.createSpy('announceSelect');
announceMove = jasmine.createSpy('announceMove');
announceCancel = jasmine.createSpy('announceCancel');
}