@@ -48,12 +48,9 @@ import {
4848 WorkspaceitemSectionUnpaywallObject
4949} from '../../../core/submission/models/workspaceitem-section-unpaywall-object' ;
5050import { UnpaywallSectionStatus } from './models/unpaywall-section-status' ;
51- import { UnpaywallApi } from './models/unpaywall-api' ;
52- import { ResourceService } from '../../../core/services/resource.service' ;
5351import { of } from 'rxjs' ;
5452import { AuthTokenInfo } from '../../../core/auth/models/auth-token-info.model' ;
5553import { provideMockStore } from '@ngrx/store/testing' ;
56- import { FileItem } from 'ng2-file-upload' ;
5754import { SubmissionObject } from '../../../core/submission/models/submission-object.model' ;
5855import { SectionsType } from '../sections-type' ;
5956import { WorkspaceitemSectionUploadObject } from '../../../core/submission/models/workspaceitem-section-upload.model' ;
@@ -62,12 +59,13 @@ import {
6259} from '../../../core/submission/models/workspaceitem-section-upload-file.model' ;
6360import { RawRestResponse } from '../../../core/dspace-rest/raw-rest-response.model' ;
6461import { SubmissionState } from '../../submission.reducers' ;
62+ import { SectionUploadService } from '../upload/section-upload.service' ;
63+ import { getMockSectionUploadService } from '../../../shared/mocks/section-upload.service.mock' ;
6564
6665describe ( 'SubmissionSectionUnpaywallComponentComponent' , ( ) => {
6766 let component : SubmissionSectionUnpaywallComponent ;
6867 let fixture : ComponentFixture < SubmissionSectionUnpaywallComponent > ;
6968 let httpMock : HttpTestingController ;
70- let resourceService : ResourceService ;
7169 let submissionService : SubmissionService ;
7270 let halService : HALEndpointService ;
7371 let tokenExtractor : HttpXsrfTokenExtractor ;
@@ -76,6 +74,7 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
7674 let translate : TranslateService ;
7775 let restApi : DspaceRestService ;
7876 let store : Store < SubmissionState > ;
77+ let sectionUploadService : SectionUploadService ;
7978 const xsrfToken = 'mock-token' ;
8079
8180 const initialState = {
@@ -140,6 +139,7 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
140139 { provide : 'collectionIdProvider' , useValue : mockSubmissionCollectionId } ,
141140 { provide : 'sectionDataProvider' , useValue : { } } ,
142141 { provide : 'submissionIdProvider' , useValue : mockSubmissionId } ,
142+ { provide : SectionUploadService , useValue : getMockSectionUploadService ( ) } ,
143143 ] ,
144144 declarations : [ SubmissionSectionUnpaywallComponent ] ,
145145 schemas : [ NO_ERRORS_SCHEMA ]
@@ -149,7 +149,6 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
149149
150150 beforeEach ( ( ) => {
151151 fixture = TestBed . createComponent ( SubmissionSectionUnpaywallComponent ) ;
152- resourceService = TestBed . inject ( ResourceService ) ;
153152 submissionService = TestBed . inject ( SubmissionService ) ;
154153 halService = TestBed . inject ( HALEndpointService ) ;
155154 httpMock = TestBed . inject ( HttpTestingController ) ;
@@ -159,6 +158,7 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
159158 translate = TestBed . inject ( TranslateService ) ;
160159 restApi = TestBed . inject ( DspaceRestService ) ;
161160 store = TestBed . inject ( Store ) ;
161+ sectionUploadService = TestBed . inject ( SectionUploadService ) ;
162162 component = fixture . componentInstance ;
163163 fixture . detectChanges ( ) ;
164164 } ) ;
@@ -171,19 +171,24 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
171171 expect ( component ) . toBeTruthy ( ) ;
172172 } ) ;
173173
174- describe ( 'uploadFileIfNeeded' , ( ) => {
175- it ( 'should upload file if needed' , ( ) => {
176- const resourceUrl = 'http://resourse-url/test.txt' ;
177- const resource = new Blob ( [ 'Test data' ] , { type : 'text/plain' } ) ;
174+ describe ( 'import file' , ( ) => {
175+ it ( 'should import the linked resource if needed' , ( ) => {
178176 const submissionObjectName = 'workspaceitems' ;
179177 const testEndpoint = 'http://test-endpoint' ;
180178 const successLabel = of ( 'success-label' ) ;
181- const unpaywallApiResp = { best_oa_location : { url : resourceUrl } } as UnpaywallApi ;
182- const submissionObject = {
179+ const uploadSection : WorkspaceitemSectionUploadObject = { files : [ ] } ;
180+ const successfulSection : WorkspaceitemSectionUnpaywallObject = {
181+ id : 1 ,
182+ status : UnpaywallSectionStatus . SUCCESSFUL ,
183+ doi : 'test-doi' ,
184+ itemId : 'test-item-id' ,
185+ timestampCreated : new Date ( ) ,
186+ timestampLastModified : new Date ( )
187+ } ;
188+ const importedResource = {
183189 sections : {
184190 [ SectionsType . Unpaywall ] : {
185- status : UnpaywallSectionStatus . SUCCESSFUL ,
186- jsonRecord : JSON . stringify ( unpaywallApiResp ) ,
191+ status : UnpaywallSectionStatus . IMPORTED ,
187192 doi : 'test-doi' ,
188193 itemId : 'test-item-id' ,
189194 timestampCreated : new Date ( ) ,
@@ -196,27 +201,32 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
196201 } as WorkspaceitemSectionUploadObject
197202 }
198203 } as unknown as SubmissionObject ;
199- component . section$ . next ( < WorkspaceitemSectionUnpaywallObject > submissionObject . sections . unpaywall ) ;
204+ component . unpaywallSection$ . next ( successfulSection ) ;
205+ component . uploadSection$ . next ( { [ SectionsType . Upload ] : uploadSection } ) ;
206+ const requestResponse = { payload : importedResource } as unknown as RawRestResponse ;
207+ jasmine . clock ( ) . install ( ) ;
200208
201- spyOn ( resourceService , 'download' ) . withArgs ( resourceUrl ) . and . returnValue ( of ( resource ) ) ;
202209 spyOn ( submissionService , 'getSubmissionObjectLinkName' ) . and . returnValue ( submissionObjectName ) ;
203210 spyOn ( halService , 'getEndpoint' ) . withArgs ( submissionObjectName ) . and . returnValue ( of ( testEndpoint ) ) ;
204211 spyOn ( tokenExtractor , 'getToken' ) . and . returnValue ( xsrfToken ) ;
205- spyOn ( component . uploader , 'uploadAll' ) . and . callFake ( ( ) =>
206- ( component . uploader as any ) . _onSuccessItem ( new FileItem ( component . uploader , new File ( [ resource ] , 'test.txt' ) , { } ) , JSON . stringify ( submissionObject ) , null , null ) ) ;
212+ spyOn ( restApi , 'request' ) . and . returnValue ( of ( requestResponse ) ) ;
207213 spyOn ( sectionService , 'isSectionType' )
208214 . withArgs ( mockSubmissionId , SectionsType . Unpaywall , SectionsType . Upload ) . and . returnValue ( of ( false ) )
209215 . withArgs ( mockSubmissionId , SectionsType . Upload , SectionsType . Upload ) . and . returnValue ( of ( true ) ) ;
210- spyOn ( translate , 'get' ) . withArgs ( 'submission.sections.upload.upload-successful' ) . and . returnValue ( successLabel ) ;
216+ spyOn ( translate , 'get' ) . withArgs ( 'submission.sections.unpaywall.status.imported' ) . and . returnValue ( successLabel ) ;
217+ spyOn ( component . loading$ , 'next' ) ;
218+ spyOn ( component . status$ , 'next' ) ;
219+ spyOn ( component . unpaywallSection$ , 'next' ) ;
211220 spyOn ( notificationsService , 'success' ) . withArgs ( null , successLabel ) ;
212221 spyOn ( notificationsService , 'error' ) ;
213- spyOn ( sectionService , 'updateSectionData' ) . withArgs ( mockSubmissionId , SectionsType . Upload , submissionObject . sections [ SectionsType . Upload ] , undefined , undefined ) ;
214222
215- component . uploadFileIfNeeded ( ) ;
223+ component . confirmImport ( ) ;
216224
217- expect ( component . uploader . uploadAll ) . toHaveBeenCalledTimes ( 1 ) ;
218- expect ( notificationsService . success ) . toHaveBeenCalledTimes ( 1 ) ;
219- expect ( notificationsService . error ) . not . toHaveBeenCalled ( ) ;
225+ expect ( component . loading$ . next ) . toHaveBeenCalledWith ( false ) ;
226+ expect ( component . status$ . next ) . toHaveBeenCalledOnceWith ( ( importedResource . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) . status ) ;
227+ expect ( component . unpaywallSection$ . next ) . toHaveBeenCalledWith ( importedResource . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) ;
228+ expect ( sectionUploadService . addUploadedFile ) . toHaveBeenCalledTimes ( 1 ) ;
229+ jasmine . clock ( ) . uninstall ( ) ;
220230 } ) ;
221231 } ) ;
222232
@@ -228,7 +238,6 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
228238 sections : {
229239 [ SectionsType . Unpaywall ] : {
230240 status : UnpaywallSectionStatus . SUCCESSFUL ,
231- jsonRecord : '' ,
232241 doi : 'test-doi' ,
233242 itemId : 'test-item-id' ,
234243 timestampCreated : new Date ( ) ,
@@ -245,15 +254,15 @@ describe('SubmissionSectionUnpaywallComponentComponent', () => {
245254 spyOn ( component . loading$ , 'next' ) ;
246255 spyOn ( component . status$ , 'next' )
247256 . withArgs ( ( submissionObject . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) . status ) ;
248- spyOn ( component . section $, 'next' )
257+ spyOn ( component . unpaywallSection $, 'next' )
249258 . withArgs ( submissionObject . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) ;
250259
251260 component . refreshApiCheck ( ) ;
252261 jasmine . clock ( ) . tick ( 3010 ) ;
253262
254263 expect ( component . loading$ . next ) . toHaveBeenCalledWith ( false ) ;
255264 expect ( component . status$ . next ) . toHaveBeenCalledOnceWith ( ( submissionObject . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) . status ) ;
256- expect ( component . section $. next ) . toHaveBeenCalledOnceWith ( submissionObject . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) ;
265+ expect ( component . unpaywallSection $. next ) . toHaveBeenCalledOnceWith ( submissionObject . sections [ SectionsType . Unpaywall ] as WorkspaceitemSectionUnpaywallObject ) ;
257266 jasmine . clock ( ) . uninstall ( ) ;
258267 } ) ;
259268 } ) ;
0 commit comments