@@ -24,6 +24,7 @@ import { createPaginatedList } from '../../shared/testing/utils.test';
2424import { Item } from '../../core/shared/item.model' ;
2525import { MetadataValueFilter } from '../../core/shared/metadata.models' ;
2626import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
27+ import { PrimaryBitstreamService } from '../../core/data/primary-bitstream.service' ;
2728
2829const infoNotification : INotification = new Notification ( 'id' , NotificationType . Info , 'info' ) ;
2930const warningNotification : INotification = new Notification ( 'id' , NotificationType . Warning , 'warning' ) ;
@@ -32,19 +33,27 @@ const successNotification: INotification = new Notification('id', NotificationTy
3233let notificationsService : NotificationsService ;
3334let formService : DynamicFormService ;
3435let bitstreamService : BitstreamDataService ;
36+ let primaryBitstreamService : PrimaryBitstreamService ;
3537let bitstreamFormatService : BitstreamFormatDataService ;
3638let dsoNameService : DSONameService ;
3739let bitstream : Bitstream ;
40+ let bitstreamID : string ;
3841let selectedFormat : BitstreamFormat ;
3942let allFormats : BitstreamFormat [ ] ;
4043let router : Router ;
41-
44+ let currentPrimary : string ;
45+ let differentPrimary : string ;
46+ let bundle ;
4247let comp : EditBitstreamPageComponent ;
4348let fixture : ComponentFixture < EditBitstreamPageComponent > ;
4449
4550describe ( 'EditBitstreamPageComponent' , ( ) => {
4651
4752 beforeEach ( ( ) => {
53+ bitstreamID = 'current-bitstream-id' ;
54+ currentPrimary = bitstreamID ;
55+ differentPrimary = '12345-abcde-54321-edcba' ;
56+
4857 allFormats = [
4958 Object . assign ( {
5059 id : '1' ,
@@ -53,7 +62,7 @@ describe('EditBitstreamPageComponent', () => {
5362 supportLevel : BitstreamFormatSupportLevel . Unknown ,
5463 mimetype : 'application/octet-stream' ,
5564 _links : {
56- self : { href : 'format-selflink-1' }
65+ self : { href : 'format-selflink-1' }
5766 }
5867 } ) ,
5968 Object . assign ( {
@@ -63,7 +72,7 @@ describe('EditBitstreamPageComponent', () => {
6372 supportLevel : BitstreamFormatSupportLevel . Known ,
6473 mimetype : 'image/png' ,
6574 _links : {
66- self : { href : 'format-selflink-2' }
75+ self : { href : 'format-selflink-2' }
6776 }
6877 } ) ,
6978 Object . assign ( {
@@ -73,7 +82,7 @@ describe('EditBitstreamPageComponent', () => {
7382 supportLevel : BitstreamFormatSupportLevel . Known ,
7483 mimetype : 'image/gif' ,
7584 _links : {
76- self : { href : 'format-selflink-3' }
85+ self : { href : 'format-selflink-3' }
7786 }
7887 } )
7988 ] as BitstreamFormat [ ] ;
@@ -103,15 +112,52 @@ describe('EditBitstreamPageComponent', () => {
103112 success : successNotification
104113 }
105114 ) ;
115+
116+ bundle = {
117+ _links : {
118+ primaryBitstream : {
119+ href : 'bundle-selflink'
120+ }
121+ } ,
122+ item : createSuccessfulRemoteDataObject$ ( Object . assign ( new Item ( ) , {
123+ uuid : 'some-uuid' ,
124+ firstMetadataValue ( keyOrKeys : string | string [ ] , valueFilter ?: MetadataValueFilter ) : string {
125+ return undefined ;
126+ } ,
127+ } ) )
128+ } ;
129+
130+ const result = createSuccessfulRemoteDataObject$ ( bundle ) ;
131+ primaryBitstreamService = jasmine . createSpyObj ( 'PrimaryBitstreamService' ,
132+ {
133+ put : result ,
134+ create : result ,
135+ delete : result ,
136+ } ) ;
137+
106138 } ) ;
107139
108140 describe ( 'EditBitstreamPageComponent no IIIF fields' , ( ) => {
109141
110142 beforeEach ( waitForAsync ( ( ) => {
111-
143+ bundle = {
144+ _links : {
145+ primaryBitstream : {
146+ href : 'bundle-selflink'
147+ }
148+ } ,
149+ item : createSuccessfulRemoteDataObject$ ( Object . assign ( new Item ( ) , {
150+ uuid : 'some-uuid' ,
151+ firstMetadataValue ( keyOrKeys : string | string [ ] , valueFilter ?: MetadataValueFilter ) : string {
152+ return undefined ;
153+ } ,
154+ } ) )
155+ } ;
112156 const bundleName = 'ORIGINAL' ;
113157
114158 bitstream = Object . assign ( new Bitstream ( ) , {
159+ uuid : bitstreamID ,
160+ id : bitstreamID ,
115161 metadata : {
116162 'dc.description' : [
117163 {
@@ -128,17 +174,11 @@ describe('EditBitstreamPageComponent', () => {
128174 _links : {
129175 self : 'bitstream-selflink'
130176 } ,
131- bundle : createSuccessfulRemoteDataObject$ ( {
132- item : createSuccessfulRemoteDataObject$ ( Object . assign ( new Item ( ) , {
133- uuid : 'some-uuid' ,
134- firstMetadataValue ( keyOrKeys : string | string [ ] , valueFilter ?: MetadataValueFilter ) : string {
135- return undefined ;
136- } ,
137- } ) )
138- } )
177+ bundle : createSuccessfulRemoteDataObject$ ( bundle )
139178 } ) ;
140179 bitstreamService = jasmine . createSpyObj ( 'bitstreamService' , {
141180 findById : createSuccessfulRemoteDataObject$ ( bitstream ) ,
181+ findByHref : createSuccessfulRemoteDataObject$ ( bitstream ) ,
142182 update : createSuccessfulRemoteDataObject$ ( bitstream ) ,
143183 updateFormat : createSuccessfulRemoteDataObject$ ( bitstream ) ,
144184 commitUpdates : { } ,
@@ -155,17 +195,19 @@ describe('EditBitstreamPageComponent', () => {
155195 imports : [ TranslateModule . forRoot ( ) , RouterTestingModule ] ,
156196 declarations : [ EditBitstreamPageComponent , FileSizePipe , VarDirective ] ,
157197 providers : [
158- { provide : NotificationsService , useValue : notificationsService } ,
159- { provide : DynamicFormService , useValue : formService } ,
160- { provide : ActivatedRoute ,
198+ { provide : NotificationsService , useValue : notificationsService } ,
199+ { provide : DynamicFormService , useValue : formService } ,
200+ {
201+ provide : ActivatedRoute ,
161202 useValue : {
162- data : observableOf ( { bitstream : createSuccessfulRemoteDataObject ( bitstream ) } ) ,
163- snapshot : { queryParams : { } }
203+ data : observableOf ( { bitstream : createSuccessfulRemoteDataObject ( bitstream ) } ) ,
204+ snapshot : { queryParams : { } }
164205 }
165206 } ,
166- { provide : BitstreamDataService , useValue : bitstreamService } ,
167- { provide : DSONameService , useValue : dsoNameService } ,
168- { provide : BitstreamFormatDataService , useValue : bitstreamFormatService } ,
207+ { provide : BitstreamDataService , useValue : bitstreamService } ,
208+ { provide : DSONameService , useValue : dsoNameService } ,
209+ { provide : BitstreamFormatDataService , useValue : bitstreamFormatService } ,
210+ { provide : PrimaryBitstreamService , useValue : primaryBitstreamService } ,
169211 ChangeDetectorRef
170212 ] ,
171213 schemas : [ NO_ERRORS_SCHEMA ]
@@ -203,6 +245,27 @@ describe('EditBitstreamPageComponent', () => {
203245 it ( 'should put the \"New Format\" input on invisible' , ( ) => {
204246 expect ( comp . formLayout . newFormat . grid . host ) . toContain ( 'invisible' ) ;
205247 } ) ;
248+ describe ( 'when the bitstream is the primary bitstream on the bundle' , ( ) => {
249+ beforeEach ( ( ) => {
250+ ( comp as any ) . primaryBitstreamUUID = currentPrimary ;
251+ comp . setForm ( ) ;
252+ rawForm = comp . formGroup . getRawValue ( ) ;
253+
254+ } ) ;
255+ it ( 'should enable the primary bitstream toggle' , ( ) => {
256+ expect ( rawForm . fileNamePrimaryContainer . primaryBitstream ) . toEqual ( true ) ;
257+ } ) ;
258+ } ) ;
259+ describe ( 'when the bitstream is not the primary bitstream on the bundle' , ( ) => {
260+ beforeEach ( ( ) => {
261+ ( comp as any ) . primaryBitstreamUUID = differentPrimary ;
262+ comp . setForm ( ) ;
263+ rawForm = comp . formGroup . getRawValue ( ) ;
264+ } ) ;
265+ it ( 'should disable the primary bitstream toggle' , ( ) => {
266+ expect ( rawForm . fileNamePrimaryContainer . primaryBitstream ) . toEqual ( false ) ;
267+ } ) ;
268+ } ) ;
206269 } ) ;
207270
208271 describe ( 'when an unknown format is selected' , ( ) => {
@@ -216,6 +279,83 @@ describe('EditBitstreamPageComponent', () => {
216279 } ) ;
217280
218281 describe ( 'onSubmit' , ( ) => {
282+ describe ( 'when the primaryBitstream changed' , ( ) => {
283+ describe ( 'to the current bitstream' , ( ) => {
284+ beforeEach ( ( ) => {
285+ const rawValue = Object . assign ( comp . formGroup . getRawValue ( ) , { fileNamePrimaryContainer : { primaryBitstream : true } } ) ;
286+ spyOn ( comp . formGroup , 'getRawValue' ) . and . returnValue ( rawValue ) ;
287+ } ) ;
288+
289+ describe ( 'from a different primary bitstream' , ( ) => {
290+ beforeEach ( ( ) => {
291+ ( comp as any ) . primaryBitstreamUUID = differentPrimary ;
292+ comp . onSubmit ( ) ;
293+ } ) ;
294+
295+ it ( 'should call put with the correct bitstream on the PrimaryBitstreamService' , ( ) => {
296+ expect ( primaryBitstreamService . put ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { uuid : currentPrimary } ) , bundle ) ;
297+ } ) ;
298+ } ) ;
299+
300+ describe ( 'from no primary bitstream' , ( ) => {
301+ beforeEach ( ( ) => {
302+ ( comp as any ) . primaryBitstreamUUID = null ;
303+ comp . onSubmit ( ) ;
304+ } ) ;
305+
306+ it ( 'should call create with the correct bitstream on the PrimaryBitstreamService' , ( ) => {
307+ expect ( primaryBitstreamService . create ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { uuid : currentPrimary } ) , bundle ) ;
308+ } ) ;
309+ } ) ;
310+ } ) ;
311+ describe ( 'to no primary bitstream' , ( ) => {
312+ beforeEach ( ( ) => {
313+ const rawValue = Object . assign ( comp . formGroup . getRawValue ( ) , { fileNamePrimaryContainer : { primaryBitstream : false } } ) ;
314+ spyOn ( comp . formGroup , 'getRawValue' ) . and . returnValue ( rawValue ) ;
315+ } ) ;
316+
317+ describe ( 'from the current bitstream' , ( ) => {
318+ beforeEach ( ( ) => {
319+ ( comp as any ) . primaryBitstreamUUID = currentPrimary ;
320+ comp . onSubmit ( ) ;
321+ } ) ;
322+
323+ it ( 'should call delete on the PrimaryBitstreamService' , ( ) => {
324+ expect ( primaryBitstreamService . delete ) . toHaveBeenCalledWith ( jasmine . objectContaining ( bundle ) ) ;
325+ } ) ;
326+ } ) ;
327+ } ) ;
328+ } ) ;
329+ describe ( 'when the primaryBitstream did not change' , ( ) => {
330+ describe ( 'the current bitstream stayed the primary bitstream' , ( ) => {
331+ beforeEach ( ( ) => {
332+ const rawValue = Object . assign ( comp . formGroup . getRawValue ( ) , { fileNamePrimaryContainer : { primaryBitstream : true } } ) ;
333+ spyOn ( comp . formGroup , 'getRawValue' ) . and . returnValue ( rawValue ) ;
334+ ( comp as any ) . primaryBitstreamUUID = currentPrimary ;
335+ comp . onSubmit ( ) ;
336+ } ) ;
337+ it ( 'should not call anything on the PrimaryBitstreamService' , ( ) => {
338+ expect ( primaryBitstreamService . put ) . not . toHaveBeenCalled ( ) ;
339+ expect ( primaryBitstreamService . delete ) . not . toHaveBeenCalled ( ) ;
340+ expect ( primaryBitstreamService . create ) . not . toHaveBeenCalled ( ) ;
341+ } ) ;
342+ } ) ;
343+
344+ describe ( 'the bitstream was not and did not become the primary bitstream' , ( ) => {
345+ beforeEach ( ( ) => {
346+ const rawValue = Object . assign ( comp . formGroup . getRawValue ( ) , { fileNamePrimaryContainer : { primaryBitstream : false } } ) ;
347+ spyOn ( comp . formGroup , 'getRawValue' ) . and . returnValue ( rawValue ) ;
348+ ( comp as any ) . primaryBitstreamUUID = differentPrimary ;
349+ comp . onSubmit ( ) ;
350+ } ) ;
351+ it ( 'should not call anything on the PrimaryBitstreamService' , ( ) => {
352+ expect ( primaryBitstreamService . put ) . not . toHaveBeenCalled ( ) ;
353+ expect ( primaryBitstreamService . delete ) . not . toHaveBeenCalled ( ) ;
354+ expect ( primaryBitstreamService . create ) . not . toHaveBeenCalled ( ) ;
355+ } ) ;
356+ } ) ;
357+ } ) ;
358+
219359 describe ( 'when selected format hasn\'t changed' , ( ) => {
220360 beforeEach ( ( ) => {
221361 comp . onSubmit ( ) ;
@@ -261,20 +401,13 @@ describe('EditBitstreamPageComponent', () => {
261401 expect ( comp . navigateToItemEditBitstreams ) . toHaveBeenCalled ( ) ;
262402 } ) ;
263403 } ) ;
264- describe ( 'when navigateToItemEditBitstreams is called, and the component has an itemId ' , ( ) => {
404+ describe ( 'when navigateToItemEditBitstreams is called' , ( ) => {
265405 it ( 'should redirect to the item edit page on the bitstreams tab with the itemId from the component' , ( ) => {
266406 comp . itemId = 'some-uuid1' ;
267407 comp . navigateToItemEditBitstreams ( ) ;
268408 expect ( router . navigate ) . toHaveBeenCalledWith ( [ getEntityEditRoute ( null , 'some-uuid1' ) , 'bitstreams' ] ) ;
269409 } ) ;
270410 } ) ;
271- describe ( 'when navigateToItemEditBitstreams is called, and the component does not have an itemId' , ( ) => {
272- it ( 'should redirect to the item edit page on the bitstreams tab with the itemId from the bundle links ' , ( ) => {
273- comp . itemId = undefined ;
274- comp . navigateToItemEditBitstreams ( ) ;
275- expect ( router . navigate ) . toHaveBeenCalledWith ( [ getEntityEditRoute ( null , 'some-uuid' ) , 'bitstreams' ] ) ;
276- } ) ;
277- } ) ;
278411 } ) ;
279412
280413 describe ( 'EditBitstreamPageComponent with IIIF fields' , ( ) => {
@@ -321,16 +454,22 @@ describe('EditBitstreamPageComponent', () => {
321454 self : 'bitstream-selflink'
322455 } ,
323456 bundle : createSuccessfulRemoteDataObject$ ( {
457+ _links : {
458+ primaryBitstream : {
459+ href : 'bundle-selflink'
460+ }
461+ } ,
324462 item : createSuccessfulRemoteDataObject$ ( Object . assign ( new Item ( ) , {
325463 uuid : 'some-uuid' ,
326464 firstMetadataValue ( keyOrKeys : string | string [ ] , valueFilter ?: MetadataValueFilter ) : string {
327465 return 'True' ;
328466 }
329467 } ) )
330- } )
468+ } ) ,
331469 } ) ;
332470 bitstreamService = jasmine . createSpyObj ( 'bitstreamService' , {
333471 findById : createSuccessfulRemoteDataObject$ ( bitstream ) ,
472+ findByHref : createSuccessfulRemoteDataObject$ ( bitstream ) ,
334473 update : createSuccessfulRemoteDataObject$ ( bitstream ) ,
335474 updateFormat : createSuccessfulRemoteDataObject$ ( bitstream ) ,
336475 commitUpdates : { } ,
@@ -357,6 +496,7 @@ describe('EditBitstreamPageComponent', () => {
357496 { provide : BitstreamDataService , useValue : bitstreamService } ,
358497 { provide : DSONameService , useValue : dsoNameService } ,
359498 { provide : BitstreamFormatDataService , useValue : bitstreamFormatService } ,
499+ { provide : PrimaryBitstreamService , useValue : primaryBitstreamService } ,
360500 ChangeDetectorRef
361501 ] ,
362502 schemas : [ NO_ERRORS_SCHEMA ]
@@ -371,7 +511,6 @@ describe('EditBitstreamPageComponent', () => {
371511 spyOn ( router , 'navigate' ) ;
372512 } ) ;
373513
374-
375514 describe ( 'on startup' , ( ) => {
376515 let rawForm ;
377516
@@ -440,16 +579,22 @@ describe('EditBitstreamPageComponent', () => {
440579 self : 'bitstream-selflink'
441580 } ,
442581 bundle : createSuccessfulRemoteDataObject$ ( {
582+ _links : {
583+ primaryBitstream : {
584+ href : 'bundle-selflink'
585+ }
586+ } ,
443587 item : createSuccessfulRemoteDataObject$ ( Object . assign ( new Item ( ) , {
444588 uuid : 'some-uuid' ,
445589 firstMetadataValue ( keyOrKeys : string | string [ ] , valueFilter ?: MetadataValueFilter ) : string {
446590 return 'True' ;
447591 }
448592 } ) )
449- } )
593+ } ) ,
450594 } ) ;
451595 bitstreamService = jasmine . createSpyObj ( 'bitstreamService' , {
452596 findById : createSuccessfulRemoteDataObject$ ( bitstream ) ,
597+ findByHref : createSuccessfulRemoteDataObject$ ( bitstream ) ,
453598 update : createSuccessfulRemoteDataObject$ ( bitstream ) ,
454599 updateFormat : createSuccessfulRemoteDataObject$ ( bitstream ) ,
455600 commitUpdates : { } ,
@@ -475,6 +620,7 @@ describe('EditBitstreamPageComponent', () => {
475620 { provide : BitstreamDataService , useValue : bitstreamService } ,
476621 { provide : DSONameService , useValue : dsoNameService } ,
477622 { provide : BitstreamFormatDataService , useValue : bitstreamFormatService } ,
623+ { provide : PrimaryBitstreamService , useValue : primaryBitstreamService } ,
478624 ChangeDetectorRef
479625 ] ,
480626 schemas : [ NO_ERRORS_SCHEMA ]
@@ -496,7 +642,7 @@ describe('EditBitstreamPageComponent', () => {
496642 rawForm = comp . formGroup . getRawValue ( ) ;
497643 } ) ;
498644
499- it ( 'should NOT set isIIIF to true' , ( ) => {
645+ it ( 'should NOT set is IIIF to true' , ( ) => {
500646 expect ( comp . isIIIF ) . toBeFalse ( ) ;
501647 } ) ;
502648 it ( 'should put the \"IIIF Label\" input not to be shown' , ( ) => {
0 commit comments