Skip to content

Commit 7fb4755

Browse files
committed
118223: Add item-edit-bitstream-bundle component tests
1 parent e8379db commit 7fb4755

2 files changed

Lines changed: 285 additions & 20 deletions

File tree

src/app/item-page/edit-item-page/item-bitstreams/item-edit-bitstream-bundle/item-edit-bitstream-bundle.component.spec.ts

Lines changed: 284 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import { Bundle } from '../../../../core/shared/bundle.model';
77
import { ResponsiveTableSizes } from '../../../../shared/responsive-table-sizes/responsive-table-sizes';
88
import { ResponsiveColumnSizes } from '../../../../shared/responsive-table-sizes/responsive-column-sizes';
99
import { BundleDataService } from '../../../../core/data/bundle-data.service';
10-
import { of as observableOf } from 'rxjs';
10+
import { of as observableOf, of, Subject } from 'rxjs';
1111
import { ObjectUpdatesService } from '../../../../core/data/object-updates/object-updates.service';
1212
import { PaginationServiceStub } from '../../../../shared/testing/pagination-service.stub';
1313
import { RequestService } from '../../../../core/data/request.service';
1414
import { getMockRequestService } from '../../../../shared/mocks/request.service.mock';
15-
import { ItemBitstreamsService } from '../item-bitstreams.service';
15+
import { ItemBitstreamsService, BitstreamTableEntry, SelectedBitstreamTableEntry } from '../item-bitstreams.service';
1616
import { PaginationService } from '../../../../core/pagination/pagination.service';
17-
import { PaginationComponentOptions } from '../../../../shared/pagination/pagination-component-options.model';
17+
import { getItemBitstreamsServiceStub, ItemBitstreamsServiceStub } from '../item-bitstreams.service.stub';
18+
import { FieldUpdate } from '../../../../core/data/object-updates/field-update.model';
19+
import { FieldChangeType } from '../../../../core/data/object-updates/field-change-type.model';
20+
import { createSuccessfulRemoteDataObject$ } from '../../../../shared/remote-data.utils';
21+
import { createPaginatedList } from '../../../../shared/testing/utils.test';
22+
import { CdkDragDrop } from '@angular/cdk/drag-drop';
1823

1924
describe('ItemEditBitstreamBundleComponent', () => {
2025
let comp: ItemEditBitstreamBundleComponent;
@@ -43,25 +48,20 @@ describe('ItemEditBitstreamBundleComponent', () => {
4348
const restEndpoint = 'fake-rest-endpoint';
4449
const bundleService = jasmine.createSpyObj('bundleService', {
4550
getBitstreamsEndpoint: observableOf(restEndpoint),
46-
getBitstreams: null,
51+
getBitstreams: createSuccessfulRemoteDataObject$(createPaginatedList([])),
4752
});
4853

49-
const objectUpdatesService = {
50-
initialize: () => {
51-
// do nothing
52-
},
53-
};
54-
55-
const itemBitstreamsService = jasmine.createSpyObj('itemBitstreamsService', {
56-
getInitialBitstreamsPaginationOptions: Object.assign(new PaginationComponentOptions(), {
57-
id: 'bundles-pagination-options',
58-
currentPage: 1,
59-
pageSize: 9999
60-
}),
61-
getSelectedBitstream$: observableOf({}),
62-
});
54+
let objectUpdatesService: any;
55+
let itemBitstreamsService: ItemBitstreamsServiceStub;
6356

6457
beforeEach(waitForAsync(() => {
58+
objectUpdatesService = jasmine.createSpyObj('objectUpdatesService', {
59+
initialize: undefined,
60+
getFieldUpdatesExclusive: of(null),
61+
});
62+
63+
itemBitstreamsService = getItemBitstreamsServiceStub();
64+
6565
TestBed.configureTestingModule({
6666
imports: [TranslateModule.forRoot()],
6767
declarations: [ItemEditBitstreamBundleComponent],
@@ -92,4 +92,270 @@ describe('ItemEditBitstreamBundleComponent', () => {
9292
it('should create an embedded view of the component', () => {
9393
expect(viewContainerRef.createEmbeddedView).toHaveBeenCalled();
9494
});
95+
96+
describe('on selected entry change', () => {
97+
let paginationComponent: any;
98+
let testSubject: Subject<SelectedBitstreamTableEntry> = new Subject();
99+
100+
beforeEach(() => {
101+
paginationComponent = jasmine.createSpyObj('paginationComponent', {
102+
doPageChange: undefined,
103+
});
104+
comp.paginationComponent = paginationComponent;
105+
106+
spyOn<any>(comp, 'getCurrentPageSize').and.returnValue(2);
107+
});
108+
109+
it('should move to the page the selected entry is on if were not on that page', () => {
110+
const selectedA: SelectedBitstreamTableEntry = {
111+
bitstream: null,
112+
bundle: bundle,
113+
bundleSize: 5,
114+
originalPosition: 1,
115+
currentPosition: 1,
116+
};
117+
118+
const selectedB: SelectedBitstreamTableEntry = {
119+
bitstream: null,
120+
bundle: bundle,
121+
bundleSize: 5,
122+
originalPosition: 1,
123+
currentPosition: 2,
124+
};
125+
126+
comp.handleSelectedEntryChange(selectedA, selectedB);
127+
expect(paginationComponent.doPageChange).toHaveBeenCalledWith(2);
128+
});
129+
130+
it('should not change page when we are already on the correct page', () => {
131+
const selectedA: SelectedBitstreamTableEntry = {
132+
bitstream: null,
133+
bundle: bundle,
134+
bundleSize: 5,
135+
originalPosition: 0,
136+
currentPosition: 0,
137+
};
138+
139+
const selectedB: SelectedBitstreamTableEntry = {
140+
bitstream: null,
141+
bundle: bundle,
142+
bundleSize: 5,
143+
originalPosition: 0,
144+
currentPosition: 1,
145+
};
146+
147+
comp.handleSelectedEntryChange(selectedA, selectedB);
148+
expect(paginationComponent.doPageChange).not.toHaveBeenCalled();
149+
});
150+
151+
it('should change to the original page when cancelling', () => {
152+
const selectedA: SelectedBitstreamTableEntry = {
153+
bitstream: null,
154+
bundle: bundle,
155+
bundleSize: 5,
156+
originalPosition: 3,
157+
currentPosition: 0,
158+
};
159+
160+
const selectedB = null;
161+
162+
comp.handleSelectedEntryChange(selectedA, selectedB);
163+
expect(paginationComponent.doPageChange).toHaveBeenCalledWith(2);
164+
});
165+
166+
it('should not change page when we are already on the correct page when cancelling', () => {
167+
const selectedA: SelectedBitstreamTableEntry = {
168+
bitstream: null,
169+
bundle: bundle,
170+
bundleSize: 5,
171+
originalPosition: 0,
172+
currentPosition: 3,
173+
};
174+
175+
const selectedB = null;
176+
177+
comp.handleSelectedEntryChange(selectedA, selectedB);
178+
expect(paginationComponent.doPageChange).not.toHaveBeenCalled();
179+
});
180+
});
181+
182+
describe('getRowClass', () => {
183+
it('should return \'table-info\' when the bitstream is the selected bitstream', () => {
184+
itemBitstreamsService.getSelectedBitstream.and.returnValue({
185+
bitstream: { id: 'bitstream-id'}
186+
});
187+
188+
const bitstreamEntry = {
189+
id: 'bitstream-id',
190+
} as BitstreamTableEntry;
191+
192+
expect(comp.getRowClass(undefined, bitstreamEntry)).toEqual('table-info');
193+
});
194+
195+
it('should return \'table-warning\' when the update is of type \'UPDATE\'', () => {
196+
const update = {
197+
changeType: FieldChangeType.UPDATE,
198+
} as FieldUpdate;
199+
200+
expect(comp.getRowClass(update, undefined)).toEqual('table-warning');
201+
});
202+
203+
it('should return \'table-success\' when the update is of type \'ADD\'', () => {
204+
const update = {
205+
changeType: FieldChangeType.ADD,
206+
} as FieldUpdate;
207+
208+
expect(comp.getRowClass(update, undefined)).toEqual('table-success');
209+
});
210+
211+
it('should return \'table-danger\' when the update is of type \'REMOVE\'', () => {
212+
const update = {
213+
changeType: FieldChangeType.REMOVE,
214+
} as FieldUpdate;
215+
216+
expect(comp.getRowClass(update, undefined)).toEqual('table-danger');
217+
});
218+
219+
it('should return \'bg-white\' in any other case', () => {
220+
const update = {
221+
changeType: undefined,
222+
} as FieldUpdate;
223+
224+
expect(comp.getRowClass(update, undefined)).toEqual('bg-white');
225+
});
226+
});
227+
228+
describe('drag', () => {
229+
let dragTooltip;
230+
let paginationComponent;
231+
232+
beforeEach(() => {
233+
dragTooltip = jasmine.createSpyObj('dragTooltip', {
234+
open: undefined,
235+
close: undefined,
236+
});
237+
comp.dragTooltip = dragTooltip;
238+
});
239+
240+
describe('Start', () => {
241+
it('should open the tooltip when there are multiple pages', () => {
242+
paginationComponent = jasmine.createSpyObj('paginationComponent', {
243+
doPageChange: undefined,
244+
}, {
245+
shouldShowBottomPager: of(true),
246+
});
247+
comp.paginationComponent = paginationComponent;
248+
249+
comp.dragStart();
250+
expect(dragTooltip.open).toHaveBeenCalled();
251+
});
252+
253+
it('should not open the tooltip when there is only a single page', () => {
254+
paginationComponent = jasmine.createSpyObj('paginationComponent', {
255+
doPageChange: undefined,
256+
}, {
257+
shouldShowBottomPager: of(false),
258+
});
259+
comp.paginationComponent = paginationComponent;
260+
261+
comp.dragStart();
262+
expect(dragTooltip.open).not.toHaveBeenCalled();
263+
});
264+
});
265+
266+
describe('end', () => {
267+
it('should always close the tooltip', () => {
268+
paginationComponent = jasmine.createSpyObj('paginationComponent', {
269+
doPageChange: undefined,
270+
}, {
271+
shouldShowBottomPager: of(false),
272+
});
273+
comp.paginationComponent = paginationComponent;
274+
275+
comp.dragEnd();
276+
expect(dragTooltip.close).toHaveBeenCalled();
277+
});
278+
});
279+
});
280+
281+
describe('drop', () => {
282+
it('should correctly move the bitstream on drop', () => {
283+
const event = {
284+
previousIndex: 1,
285+
currentIndex: 8,
286+
dropPoint: { x: 100, y: 200 },
287+
} as CdkDragDrop<any>;
288+
289+
comp.drop(event);
290+
expect(itemBitstreamsService.performBitstreamMoveRequest).toHaveBeenCalledWith(jasmine.any(Bundle), 1, 8, jasmine.any(Function));
291+
});
292+
293+
it('should not move the bitstream if dropped in the same place', () => {
294+
const event = {
295+
previousIndex: 1,
296+
currentIndex: 1,
297+
dropPoint: { x: 100, y: 200 },
298+
} as CdkDragDrop<any>;
299+
300+
comp.drop(event);
301+
expect(itemBitstreamsService.performBitstreamMoveRequest).not.toHaveBeenCalled();
302+
});
303+
304+
it('should move to a different page if dropped on a page number', () => {
305+
spyOn(document, 'elementFromPoint').and.returnValue({
306+
textContent: '2',
307+
classList: { contains: (token: string) => true },
308+
} as Element);
309+
310+
const event = {
311+
previousIndex: 1,
312+
currentIndex: 1,
313+
dropPoint: { x: 100, y: 200 },
314+
} as CdkDragDrop<any>;
315+
316+
comp.drop(event);
317+
expect(itemBitstreamsService.performBitstreamMoveRequest).toHaveBeenCalledWith(jasmine.any(Bundle), 1, 20, jasmine.any(Function));
318+
});
319+
});
320+
321+
describe('select', () => {
322+
it('should select the bitstream', () => {
323+
const event = new KeyboardEvent('keydown');
324+
spyOnProperty(event, 'repeat', 'get').and.returnValue(false);
325+
326+
const entry = { } as BitstreamTableEntry;
327+
comp.tableEntries$.next([entry]);
328+
329+
comp.select(event, entry);
330+
expect(itemBitstreamsService.selectBitstreamEntry).toHaveBeenCalledWith(jasmine.objectContaining({ bitstream: entry }));
331+
});
332+
333+
it('should cancel the selection if the bitstream already is selected', () => {
334+
const event = new KeyboardEvent('keydown');
335+
spyOnProperty(event, 'repeat', 'get').and.returnValue(false);
336+
337+
const entry = { } as BitstreamTableEntry;
338+
comp.tableEntries$.next([entry]);
339+
340+
itemBitstreamsService.getSelectedBitstream.and.returnValue({ bitstream: entry });
341+
342+
comp.select(event, entry);
343+
expect(itemBitstreamsService.selectBitstreamEntry).not.toHaveBeenCalled();
344+
expect(itemBitstreamsService.cancelSelection).toHaveBeenCalled();
345+
});
346+
347+
it('should not do anything if the user is holding down the select key', () => {
348+
const event = new KeyboardEvent('keydown');
349+
spyOnProperty(event, 'repeat', 'get').and.returnValue(true);
350+
351+
const entry = { } as BitstreamTableEntry;
352+
comp.tableEntries$.next([entry]);
353+
354+
itemBitstreamsService.getSelectedBitstream.and.returnValue({ bitstream: entry });
355+
356+
comp.select(event, entry);
357+
expect(itemBitstreamsService.selectBitstreamEntry).not.toHaveBeenCalled();
358+
expect(itemBitstreamsService.cancelSelection).not.toHaveBeenCalled();
359+
});
360+
});
95361
});

src/app/item-page/edit-item-page/item-bitstreams/item-edit-bitstream-bundle/item-edit-bitstream-bundle.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,8 @@ export class ItemEditBitstreamBundleComponent implements OnInit, OnDestroy {
243243
* Handles a change in selected bitstream by changing the pagination if the change happened on a different page
244244
* @param previousSelectedEntry The previously selected entry
245245
* @param currentSelectedEntry The currently selected entry
246-
* @protected
247246
*/
248-
protected handleSelectedEntryChange(
247+
handleSelectedEntryChange(
249248
previousSelectedEntry: SelectedBitstreamTableEntry,
250249
currentSelectedEntry: SelectedBitstreamTableEntry
251250
) {

0 commit comments

Comments
 (0)