Skip to content

Commit 9646936

Browse files
committed
test: add filesRepository cases
1 parent 2d585b0 commit 9646936

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

test/unit/files/FilesRepository.test.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
createFileCountsPayload
3131
} from '../../testHelpers/files/fileCountsHelper'
3232
import { createFilesTotalDownloadSizePayload } from '../../testHelpers/files/filesTotalDownloadSizeHelper'
33-
import { FileDownloadSizeMode } from '../../../src'
33+
import { FileDownloadSizeMode, WriteError } from '../../../src'
3434
import {
3535
createMultipartFileUploadDestinationModel,
3636
createMultipartFileUploadDestinationPayload,
@@ -1159,4 +1159,72 @@ describe('FilesRepository', () => {
11591159
).rejects.toThrow(ReadError)
11601160
})
11611161
})
1162+
1163+
describe('deleteFile', () => {
1164+
describe('by numeric id', () => {
1165+
test('should return undefined on success', async () => {
1166+
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/${testFile.id}`
1167+
1168+
jest.spyOn(axios, 'delete').mockResolvedValue(undefined)
1169+
1170+
// API Key auth
1171+
const actual = await sut.deleteFile(testFile.id)
1172+
expect(actual).toBeUndefined()
1173+
expect(axios.delete).toHaveBeenCalledWith(
1174+
expectedApiEndpoint,
1175+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
1176+
)
1177+
1178+
// Session cookie auth
1179+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE)
1180+
const actual2 = await sut.deleteFile(testFile.id)
1181+
expect(actual2).toBeUndefined()
1182+
expect(axios.delete).toHaveBeenCalledWith(
1183+
expectedApiEndpoint,
1184+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
1185+
)
1186+
})
1187+
1188+
test('should return error result on error response', async () => {
1189+
jest.spyOn(axios, 'delete').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
1190+
1191+
await expect(sut.deleteFile(testFile.id)).rejects.toThrow(WriteError)
1192+
})
1193+
})
1194+
1195+
describe('by persistent id', () => {
1196+
test('should return undefined on success', async () => {
1197+
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/:persistentId?persistentId=${TestConstants.TEST_DUMMY_PERSISTENT_ID}`
1198+
1199+
jest.spyOn(axios, 'delete').mockResolvedValue(undefined)
1200+
1201+
// API Key auth
1202+
const actual = await sut.deleteFile(TestConstants.TEST_DUMMY_PERSISTENT_ID)
1203+
expect(axios.delete).toHaveBeenCalledWith(
1204+
expectedApiEndpoint,
1205+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
1206+
)
1207+
expect(actual).toBeUndefined()
1208+
1209+
// Session cookie auth
1210+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE)
1211+
1212+
const actual2 = await sut.deleteFile(TestConstants.TEST_DUMMY_PERSISTENT_ID)
1213+
expect(actual2).toBeUndefined()
1214+
1215+
expect(axios.delete).toHaveBeenCalledWith(
1216+
expectedApiEndpoint,
1217+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
1218+
)
1219+
})
1220+
1221+
test('should return error result on error response', async () => {
1222+
jest.spyOn(axios, 'delete').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
1223+
1224+
await expect(sut.deleteFile(TestConstants.TEST_DUMMY_PERSISTENT_ID)).rejects.toThrow(
1225+
WriteError
1226+
)
1227+
})
1228+
})
1229+
})
11621230
})

0 commit comments

Comments
 (0)