Skip to content

Commit

Permalink
♻️ refactor(core): Reorder downloadFile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Oct 3, 2023
1 parent 92019e9 commit 0c7f450
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/downloadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function downloadFile(options: DownloaderOptions) {
reject(new Error(`[Writer] ${error.message}`))
})
})
.catch((error) => {
.catch((error: Error) => {
// TODO: Handle errors in a more sophisticated way
reject(new Error(`[Axios] ${error.message}`))
})
Expand Down
44 changes: 22 additions & 22 deletions packages/core/test/downloadFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ describe('downloadFile', () => {
})
})

it('throws an error if the file cannot be downloaded', async () => {
server.use(
rest.get(MOCK_URL, (_, res, ctx) => {
return res(
ctx.status(500),
ctx.json({ message: 'Internal server error' })
)
})
)

const downloadFileFuncCall = downloadFile({
url: MOCK_URL,
outputDir: MOCK_OUTPUT_DIR,
fileName: MOCK_FILE_NAME
})

await expect(downloadFileFuncCall).rejects.toThrowError(
'[DownloadFile] Error downloading the file - [Axios] Request failed with status code 500'
)
})

describe('when the file does not exist', () => {
it('downloads a file', async () => {
const result = await downloadFile({
Expand All @@ -82,27 +103,6 @@ describe('downloadFile', () => {
expect(fs.existsSync(result.filePath)).toBe(true)
expect(fs.readFileSync(result.filePath, 'utf8')).toBe(MOCK_FILE_CONTENT)
})

it('throws an error if the file cannot be downloaded', async () => {
server.use(
rest.get(MOCK_URL, (_, res, ctx) => {
return res(
ctx.status(500),
ctx.json({ message: 'Internal server error' })
)
})
)

const downloadFileFuncCall = downloadFile({
url: MOCK_URL,
outputDir: MOCK_OUTPUT_DIR,
fileName: MOCK_FILE_NAME
})

await expect(() => downloadFileFuncCall).rejects.toThrowError(
'[DownloadFile] Error downloading the file - [Axios] Request failed with status code 500'
)
})
})

describe('when the file already exists', () => {
Expand All @@ -113,7 +113,7 @@ describe('downloadFile', () => {
expect(fs.existsSync(MOCK_FILE_PATH)).toBe(true)
})

it('skips download if the file exists', async () => {
it('skips download', async () => {
const result = await downloadFile({
url: MOCK_URL,
outputDir: MOCK_OUTPUT_DIR,
Expand Down

0 comments on commit 0c7f450

Please sign in to comment.