@@ -127,19 +127,18 @@ describe('ThumbnailComponent', () => {
127127 } ) ;
128128
129129 const errorHandler = ( ) => {
130- let fallbackSpy ;
130+ let setSrcSpy ;
131131
132132 beforeEach ( ( ) => {
133- fallbackSpy = spyOn ( comp , 'showFallback' ) . and . callThrough ( ) ;
133+ // disconnect error handler to be sure it's only called once
134+ const img = fixture . debugElement . query ( By . css ( 'img.thumbnail-content' ) ) ;
135+ img . nativeNode . onerror = null ;
136+
137+ comp . ngOnChanges ( ) ;
138+ setSrcSpy = spyOn ( comp , 'setSrc' ) . and . callThrough ( ) ;
134139 } ) ;
135140
136141 describe ( 'retry with authentication token' , ( ) => {
137- beforeEach ( ( ) => {
138- // disconnect error handler to be sure it's only called once
139- const img = fixture . debugElement . query ( By . css ( 'img.thumbnail-content' ) ) ;
140- img . nativeNode . onerror = null ;
141- } ) ;
142-
143142 it ( 'should remember that it already retried once' , ( ) => {
144143 expect ( comp . retriedWithToken ) . toBeFalse ( ) ;
145144 comp . errorHandler ( ) ;
@@ -153,7 +152,7 @@ describe('ThumbnailComponent', () => {
153152
154153 it ( 'should fall back to default' , ( ) => {
155154 comp . errorHandler ( ) ;
156- expect ( fallbackSpy ) . toHaveBeenCalled ( ) ;
155+ expect ( setSrcSpy ) . toHaveBeenCalledWith ( comp . defaultImage ) ;
157156 } ) ;
158157 } ) ;
159158
@@ -172,11 +171,9 @@ describe('ThumbnailComponent', () => {
172171
173172 if ( ( comp . thumbnail as RemoteData < Bitstream > ) ?. hasFailed ) {
174173 // If we failed to retrieve the Bitstream in the first place, fall back to the default
175- expect ( comp . src$ . getValue ( ) ) . toBe ( null ) ;
176- expect ( fallbackSpy ) . toHaveBeenCalled ( ) ;
174+ expect ( setSrcSpy ) . toHaveBeenCalledWith ( comp . defaultImage ) ;
177175 } else {
178- expect ( comp . src$ . getValue ( ) ) . toBe ( CONTENT + '?authentication-token=fake' ) ;
179- expect ( fallbackSpy ) . not . toHaveBeenCalled ( ) ;
176+ expect ( setSrcSpy ) . toHaveBeenCalledWith ( CONTENT + '?authentication-token=fake' ) ;
180177 }
181178 } ) ;
182179 } ) ;
@@ -189,8 +186,7 @@ describe('ThumbnailComponent', () => {
189186 it ( 'should fall back to default' , ( ) => {
190187 comp . errorHandler ( ) ;
191188
192- expect ( comp . src$ . getValue ( ) ) . toBe ( null ) ;
193- expect ( fallbackSpy ) . toHaveBeenCalled ( ) ;
189+ expect ( setSrcSpy ) . toHaveBeenCalledWith ( comp . defaultImage ) ;
194190
195191 // We don't need to check authorization if we failed to retrieve the Bitstreamin the first place
196192 if ( ! ( comp . thumbnail as RemoteData < Bitstream > ) ?. hasFailed ) {
@@ -210,7 +206,7 @@ describe('ThumbnailComponent', () => {
210206 comp . errorHandler ( ) ;
211207 expect ( authService . isAuthenticated ) . not . toHaveBeenCalled ( ) ;
212208 expect ( fileService . retrieveFileDownloadLink ) . not . toHaveBeenCalled ( ) ;
213- expect ( fallbackSpy ) . toHaveBeenCalled ( ) ;
209+ expect ( setSrcSpy ) . toHaveBeenCalledWith ( comp . defaultImage ) ;
214210 } ) ;
215211 } ) ;
216212 } ;
@@ -263,21 +259,23 @@ describe('ThumbnailComponent', () => {
263259 comp . thumbnail = thumbnail ;
264260 } ) ;
265261
266- it ( 'should display an image' , ( ) => {
267- comp . ngOnChanges ( ) ;
268- fixture . detectChanges ( ) ;
269- const image : HTMLElement = fixture . debugElement . query ( By . css ( 'img' ) ) . nativeElement ;
270- expect ( image . getAttribute ( 'src' ) ) . toBe ( thumbnail . _links . content . href ) ;
271- } ) ;
262+ describe ( 'if content can be loaded' , ( ) => {
263+ it ( 'should display an image' , ( ) => {
264+ comp . ngOnChanges ( ) ;
265+ fixture . detectChanges ( ) ;
266+ const image : HTMLElement = fixture . debugElement . query ( By . css ( 'img' ) ) . nativeElement ;
267+ expect ( image . getAttribute ( 'src' ) ) . toBe ( thumbnail . _links . content . href ) ;
268+ } ) ;
272269
273- it ( 'should include the alt text' , ( ) => {
274- comp . ngOnChanges ( ) ;
275- fixture . detectChanges ( ) ;
276- const image : HTMLElement = fixture . debugElement . query ( By . css ( 'img' ) ) . nativeElement ;
277- expect ( image . getAttribute ( 'alt' ) ) . toBe ( 'TRANSLATED ' + comp . alt ) ;
270+ it ( 'should include the alt text' , ( ) => {
271+ comp . ngOnChanges ( ) ;
272+ fixture . detectChanges ( ) ;
273+ const image : HTMLElement = fixture . debugElement . query ( By . css ( 'img' ) ) . nativeElement ;
274+ expect ( image . getAttribute ( 'alt' ) ) . toBe ( 'TRANSLATED ' + comp . alt ) ;
275+ } ) ;
278276 } ) ;
279277
280- describe ( 'when there is no thumbnail ' , ( ) => {
278+ describe ( 'if content can\'t be loaded ' , ( ) => {
281279 errorHandler ( ) ;
282280 } ) ;
283281 } ) ;
@@ -296,36 +294,42 @@ describe('ThumbnailComponent', () => {
296294 } ;
297295 } ) ;
298296
299- describe ( 'when there is a thumbnail ' , ( ) => {
297+ describe ( 'if RemoteData succeeded ' , ( ) => {
300298 beforeEach ( ( ) => {
301299 comp . thumbnail = createSuccessfulRemoteDataObject ( thumbnail ) ;
302300 } ) ;
303301
304- it ( 'should display an image' , ( ) => {
305- comp . ngOnChanges ( ) ;
306- fixture . detectChanges ( ) ;
307- const image : HTMLElement = de . query ( By . css ( 'img' ) ) . nativeElement ;
308- expect ( image . getAttribute ( 'src' ) ) . toBe ( thumbnail . _links . content . href ) ;
309- } ) ;
302+ describe ( 'if content can be loaded' , ( ) => {
303+ it ( 'should display an image' , ( ) => {
304+ comp . ngOnChanges ( ) ;
305+ fixture . detectChanges ( ) ;
306+ const image : HTMLElement = de . query ( By . css ( 'img' ) ) . nativeElement ;
307+ expect ( image . getAttribute ( 'src' ) ) . toBe ( thumbnail . _links . content . href ) ;
308+ } ) ;
310309
311- it ( 'should display the alt text' , ( ) => {
312- comp . ngOnChanges ( ) ;
313- fixture . detectChanges ( ) ;
314- const image : HTMLElement = de . query ( By . css ( 'img' ) ) . nativeElement ;
315- expect ( image . getAttribute ( 'alt' ) ) . toBe ( 'TRANSLATED ' + comp . alt ) ;
310+ it ( 'should display the alt text' , ( ) => {
311+ comp . ngOnChanges ( ) ;
312+ fixture . detectChanges ( ) ;
313+ const image : HTMLElement = de . query ( By . css ( 'img' ) ) . nativeElement ;
314+ expect ( image . getAttribute ( 'alt' ) ) . toBe ( 'TRANSLATED ' + comp . alt ) ;
315+ } ) ;
316316 } ) ;
317317
318- describe ( 'but it can\'t be loaded' , ( ) => {
318+ describe ( 'if content can\'t be loaded' , ( ) => {
319319 errorHandler ( ) ;
320320 } ) ;
321321 } ) ;
322322
323- describe ( 'when there is no thumbnail ' , ( ) => {
323+ describe ( 'if RemoteData failed ' , ( ) => {
324324 beforeEach ( ( ) => {
325325 comp . thumbnail = createFailedRemoteDataObject ( ) ;
326326 } ) ;
327327
328- errorHandler ( ) ;
328+ it ( 'should show the default image' , ( ) => {
329+ comp . defaultImage = 'default/image.jpg' ;
330+ comp . ngOnChanges ( ) ;
331+ expect ( comp . src$ . getValue ( ) ) . toBe ( 'default/image.jpg' ) ;
332+ } ) ;
329333 } ) ;
330334 } ) ;
331335} ) ;
0 commit comments