1- import { CommonModule } from '@angular/common' ;
1+ import {
2+ CommonModule ,
3+ Location ,
4+ } from '@angular/common' ;
25import { PLATFORM_ID } from '@angular/core' ;
36import {
47 ComponentFixture ,
@@ -14,13 +17,15 @@ import { of as observableOf } from 'rxjs';
1417
1518import { getForbiddenRoute } from '../../app-routing-paths' ;
1619import { AuthService } from '../../core/auth/auth.service' ;
20+ import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
1721import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service' ;
1822import { SignpostingDataService } from '../../core/data/signposting-data.service' ;
1923import { HardRedirectService } from '../../core/services/hard-redirect.service' ;
2024import { ServerResponseService } from '../../core/services/server-response.service' ;
2125import { Bitstream } from '../../core/shared/bitstream.model' ;
2226import { FileService } from '../../core/shared/file.service' ;
2327import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils' ;
28+ import { MatomoService } from '../../statistics/matomo.service' ;
2429import { BitstreamDownloadPageComponent } from './bitstream-download-page.component' ;
2530
2631describe ( 'BitstreamDownloadPageComponent' , ( ) => {
@@ -33,10 +38,13 @@ describe('BitstreamDownloadPageComponent', () => {
3338 let hardRedirectService : HardRedirectService ;
3439 let activatedRoute ;
3540 let router ;
41+ let location : Location ;
42+ let dsoNameService : DSONameService ;
3643
3744 let bitstream : Bitstream ;
3845 let serverResponseService : jasmine . SpyObj < ServerResponseService > ;
3946 let signpostingDataService : jasmine . SpyObj < SignpostingDataService > ;
47+ let matomoService : jasmine . SpyObj < MatomoService > ;
4048
4149 const mocklink = {
4250 href : 'http://test.org' ,
@@ -54,6 +62,7 @@ describe('BitstreamDownloadPageComponent', () => {
5462 authService = jasmine . createSpyObj ( 'authService' , {
5563 isAuthenticated : observableOf ( true ) ,
5664 setRedirectUrl : { } ,
65+ getShortlivedToken : observableOf ( 'token' ) ,
5766 } ) ;
5867 authorizationService = jasmine . createSpyObj ( 'authorizationSerivice' , {
5968 isAuthorized : observableOf ( true ) ,
@@ -63,9 +72,18 @@ describe('BitstreamDownloadPageComponent', () => {
6372 retrieveFileDownloadLink : observableOf ( 'content-url-with-headers' ) ,
6473 } ) ;
6574
66- hardRedirectService = jasmine . createSpyObj ( 'fileService ' , {
75+ hardRedirectService = jasmine . createSpyObj ( 'hardRedirectService ' , {
6776 redirect : { } ,
6877 } ) ;
78+
79+ location = jasmine . createSpyObj ( 'location' , {
80+ back : { } ,
81+ } ) ;
82+
83+ dsoNameService = jasmine . createSpyObj ( 'dsoNameService' , {
84+ getName : 'Test Bitstream' ,
85+ } ) ;
86+
6987 bitstream = Object . assign ( new Bitstream ( ) , {
7088 uuid : 'bitstreamUuid' ,
7189 _links : {
@@ -94,6 +112,8 @@ describe('BitstreamDownloadPageComponent', () => {
94112 signpostingDataService = jasmine . createSpyObj ( 'SignpostingDataService' , {
95113 getLinks : observableOf ( [ mocklink , mocklink2 ] ) ,
96114 } ) ;
115+ matomoService = jasmine . createSpyObj ( 'MatomoService' , [ 'appendVisitorId' ] ) ;
116+ matomoService . appendVisitorId . and . callFake ( ( link ) => observableOf ( link ) ) ;
97117 }
98118
99119 function initTestbed ( ) {
@@ -108,7 +128,10 @@ describe('BitstreamDownloadPageComponent', () => {
108128 { provide : HardRedirectService , useValue : hardRedirectService } ,
109129 { provide : ServerResponseService , useValue : serverResponseService } ,
110130 { provide : SignpostingDataService , useValue : signpostingDataService } ,
131+ { provide : MatomoService , useValue : matomoService } ,
111132 { provide : PLATFORM_ID , useValue : 'server' } ,
133+ { provide : Location , useValue : location } ,
134+ { provide : DSONameService , useValue : dsoNameService } ,
112135 ] ,
113136 } )
114137 . compileComponents ( ) ;
@@ -142,9 +165,11 @@ describe('BitstreamDownloadPageComponent', () => {
142165 component = fixture . componentInstance ;
143166 fixture . detectChanges ( ) ;
144167 } ) ;
145- it ( 'should redirect to the content link' , ( ) => {
146- expect ( hardRedirectService . redirect ) . toHaveBeenCalledWith ( 'bitstream-content-link' ) ;
147- } ) ;
168+ it ( 'should redirect to the content link' , waitForAsync ( ( ) => {
169+ fixture . whenStable ( ) . then ( ( ) => {
170+ expect ( hardRedirectService . redirect ) . toHaveBeenCalledWith ( 'bitstream-content-link' ) ;
171+ } ) ;
172+ } ) ) ;
148173 it ( 'should add the signposting links' , ( ) => {
149174 expect ( serverResponseService . setHeader ) . toHaveBeenCalled ( ) ;
150175 } ) ;
@@ -159,9 +184,11 @@ describe('BitstreamDownloadPageComponent', () => {
159184 component = fixture . componentInstance ;
160185 fixture . detectChanges ( ) ;
161186 } ) ;
162- it ( 'should redirect to an updated content link' , ( ) => {
163- expect ( hardRedirectService . redirect ) . toHaveBeenCalledWith ( 'content-url-with-headers' ) ;
164- } ) ;
187+ it ( 'should redirect to an updated content link' , waitForAsync ( ( ) => {
188+ fixture . whenStable ( ) . then ( ( ) => {
189+ expect ( hardRedirectService . redirect ) . toHaveBeenCalledWith ( 'content-url-with-headers' ) ;
190+ } ) ;
191+ } ) ) ;
165192 } ) ;
166193 describe ( 'when the user is not authorized and logged in' , ( ) => {
167194 beforeEach ( waitForAsync ( ( ) => {
@@ -174,9 +201,11 @@ describe('BitstreamDownloadPageComponent', () => {
174201 component = fixture . componentInstance ;
175202 fixture . detectChanges ( ) ;
176203 } ) ;
177- it ( 'should navigate to the forbidden route' , ( ) => {
178- expect ( router . navigateByUrl ) . toHaveBeenCalledWith ( getForbiddenRoute ( ) , { skipLocationChange : true } ) ;
179- } ) ;
204+ it ( 'should navigate to the forbidden route' , waitForAsync ( ( ) => {
205+ fixture . whenStable ( ) . then ( ( ) => {
206+ expect ( router . navigateByUrl ) . toHaveBeenCalledWith ( getForbiddenRoute ( ) , { skipLocationChange : true } ) ;
207+ } ) ;
208+ } ) ) ;
180209 } ) ;
181210 describe ( 'when the user is not authorized and not logged in' , ( ) => {
182211 beforeEach ( waitForAsync ( ( ) => {
@@ -190,10 +219,12 @@ describe('BitstreamDownloadPageComponent', () => {
190219 component = fixture . componentInstance ;
191220 fixture . detectChanges ( ) ;
192221 } ) ;
193- it ( 'should navigate to the login page' , ( ) => {
194- expect ( authService . setRedirectUrl ) . toHaveBeenCalled ( ) ;
195- expect ( router . navigateByUrl ) . toHaveBeenCalledWith ( 'login' ) ;
196- } ) ;
222+ it ( 'should navigate to the login page' , waitForAsync ( ( ) => {
223+ fixture . whenStable ( ) . then ( ( ) => {
224+ expect ( authService . setRedirectUrl ) . toHaveBeenCalled ( ) ;
225+ expect ( router . navigateByUrl ) . toHaveBeenCalledWith ( 'login' ) ;
226+ } ) ;
227+ } ) ) ;
197228 } ) ;
198229 } ) ;
199230} ) ;
0 commit comments