@@ -359,7 +359,7 @@ describe('DataverseClient', () => {
359359 await client . searchOnlyPublished ( { query : mockQuery , type, dataverseAlias : mockDataverseAlias } )
360360
361361 assert . calledOnce ( searchStub )
362- assert . calledWithExactly ( searchStub , expectedOptions , { 'X-Dataverse-key' : '' } )
362+ assert . calledWithExactly ( searchStub , expectedOptions , { 'X-Dataverse-key' : '' } )
363363 } )
364364 } )
365365
@@ -1147,10 +1147,15 @@ describe('DataverseClient', () => {
11471147 const testFile = fs . readFileSync ( path . resolve ( __dirname , '../test/assets/test-file.csv' ) , 'base64' )
11481148 const fileBuffer = Buffer . from ( testFile , 'base64' )
11491149
1150- it ( 'should call request with expected url' , async ( ) => {
1151- const mockFileId : string = random . number ( ) . toString ( )
1152- const mockFilename : string = system . fileName ( )
1150+ let mockFileId : string
1151+ let mockFilename : string
1152+
1153+ beforeEach ( ( ) => {
1154+ mockFileId = random . number ( ) . toString ( )
1155+ mockFilename = system . fileName ( )
1156+ } )
11531157
1158+ it ( 'should call request with expected input' , async ( ) => {
11541159 const expectedRequest = {
11551160 url : `${ host } /api/files/${ mockFileId } /replace` ,
11561161 headers : { 'X-Dataverse-key' : apiToken } ,
@@ -1160,7 +1165,8 @@ describe('DataverseClient', () => {
11601165 options : {
11611166 filename : mockFilename
11621167 }
1163- }
1168+ } ,
1169+ jsonData : JSON . stringify ( { } )
11641170 } ,
11651171 resolveWithFullResponse : true
11661172 }
@@ -1171,9 +1177,39 @@ describe('DataverseClient', () => {
11711177 assert . calledWithExactly ( requestPostStub , expectedRequest )
11721178 } )
11731179
1180+ describe ( 'with json data' , ( ) => {
1181+ it ( 'should call request with expected input' , async ( ) => {
1182+ const mockDescription : string = random . words ( )
1183+
1184+ const expectedRequest = {
1185+ url : `${ host } /api/files/${ mockFileId } /replace` ,
1186+ headers : { 'X-Dataverse-key' : apiToken } ,
1187+ formData : {
1188+ file : {
1189+ value : fileBuffer ,
1190+ options : {
1191+ filename : mockFilename
1192+ }
1193+ } ,
1194+ jsonData : JSON . stringify ( {
1195+ forceReplace : true ,
1196+ description : mockDescription
1197+ } )
1198+ } ,
1199+ resolveWithFullResponse : true
1200+ }
1201+
1202+ await client . replaceFile ( mockFileId , mockFilename , fileBuffer , {
1203+ forceReplace : true ,
1204+ description : mockDescription
1205+ } )
1206+
1207+ assert . calledOnce ( requestPostStub )
1208+ assert . calledWithExactly ( requestPostStub , expectedRequest )
1209+ } )
1210+ } )
1211+
11741212 it ( 'should return expected response' , async ( ) => {
1175- const mockFileId : string = random . number ( ) . toString ( )
1176- const mockFilename : string = system . fileName ( )
11771213 const randomValue = random . word ( )
11781214 const expectedResponse = {
11791215 ...mockResponse ,
@@ -1189,7 +1225,8 @@ describe('DataverseClient', () => {
11891225 options : {
11901226 filename : mockFilename
11911227 }
1192- }
1228+ } ,
1229+ jsonData : JSON . stringify ( { } )
11931230 } ,
11941231 resolveWithFullResponse : true
11951232 }
@@ -1202,11 +1239,9 @@ describe('DataverseClient', () => {
12021239 } )
12031240
12041241 it ( 'should throw expected error' , async ( ) => {
1205- const mockFileId : string = random . number ( ) . toString ( )
1206- const mockFilename : string = system . fileName ( )
12071242 const errorMessage = random . words ( )
12081243 const errorCode = random . number ( )
1209- requestPostStub . rejects ( { response : { statusCode : errorCode , body : JSON . stringify ( { message : errorMessage } ) } } )
1244+ requestPostStub . rejects ( { response : { statusCode : errorCode , body : { message : errorMessage } } } )
12101245
12111246 let error : DataverseException = undefined
12121247
0 commit comments