-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inrease coverage for file downloading
- Loading branch information
1 parent
3da892b
commit be9f586
Showing
5 changed files
with
85 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AxiosResponse } from 'axios' | ||
|
||
export async function downloadFile( | ||
response: Promise<AxiosResponse>, | ||
fileName: string | ||
) { | ||
const blobResponse = await response | ||
const url = window.URL.createObjectURL(new Blob([blobResponse.data])) | ||
const link = document.createElement('a') | ||
link.href = url | ||
link.setAttribute('download', fileName) | ||
document.body.appendChild(link) | ||
link.click() | ||
document.body.removeChild(link) | ||
|
||
setTimeout(() => window.URL.revokeObjectURL(url), 100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { describe, it, vi, expect } from 'vitest' | ||
import { downloadFile } from '~/utils/download-file' | ||
|
||
describe('downloadFile', () => { | ||
beforeEach(() => { | ||
global.URL.createObjectURL = vi.fn(() => 'blob:http://localhost/test-url') | ||
global.URL.revokeObjectURL = vi.fn(() => 'blob:http://localhost/test-url') | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('should download a file with the correct filename', async () => { | ||
const mockData = 'test file content' | ||
const mockResponse = Promise.resolve({ | ||
data: mockData | ||
}) | ||
|
||
const mockUrl = 'blob:http://localhost/test-url' | ||
const createObjectURLMock = vi | ||
.spyOn(window.URL, 'createObjectURL') | ||
.mockReturnValue(mockUrl) | ||
const revokeObjectURLMock = vi.spyOn(window.URL, 'revokeObjectURL') | ||
|
||
const link = { | ||
href: '', | ||
setAttribute: vi.fn(), | ||
click: vi.fn() | ||
} | ||
const appendChildMock = vi | ||
.spyOn(document.body, 'appendChild') | ||
.mockImplementation(() => {}) | ||
const removeChildMock = vi | ||
.spyOn(document.body, 'removeChild') | ||
.mockImplementation(() => {}) | ||
vi.spyOn(document, 'createElement').mockReturnValue(link) | ||
|
||
const fileName = 'test-file.txt' | ||
|
||
await downloadFile(mockResponse, fileName) | ||
|
||
expect(createObjectURLMock).toHaveBeenCalledOnce() | ||
expect(createObjectURLMock).toHaveBeenCalledWith(new Blob([mockData])) | ||
expect(link.setAttribute).toHaveBeenCalledWith('download', fileName) | ||
expect(link.click).toHaveBeenCalledOnce() | ||
expect(appendChildMock).toHaveBeenCalledWith(link) | ||
expect(removeChildMock).toHaveBeenCalledWith(link) | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 150)) | ||
expect(revokeObjectURLMock).toHaveBeenCalledWith(mockUrl) | ||
}) | ||
}) |