downloader.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import type { AxiosRequestConfig } from 'axios';
  2. import { beforeEach, describe, expect, it, vi } from 'vitest';
  3. import { FileDownloader } from './downloader';
  4. describe('fileDownloader', () => {
  5. let fileDownloader: FileDownloader;
  6. const mockAxiosInstance = {
  7. get: vi.fn(),
  8. } as any;
  9. beforeEach(() => {
  10. fileDownloader = new FileDownloader(mockAxiosInstance);
  11. });
  12. it('should create an instance of FileDownloader', () => {
  13. expect(fileDownloader).toBeInstanceOf(FileDownloader);
  14. });
  15. it('should download a file and return a Blob', async () => {
  16. const url = 'https://example.com/file';
  17. const mockBlob = new Blob(['file content'], { type: 'text/plain' });
  18. const mockResponse: Blob = mockBlob;
  19. mockAxiosInstance.get.mockResolvedValueOnce(mockResponse);
  20. const result = await fileDownloader.download(url);
  21. expect(result).toBeInstanceOf(Blob);
  22. expect(result).toEqual(mockBlob);
  23. expect(mockAxiosInstance.get).toHaveBeenCalledWith(url, {
  24. responseType: 'blob',
  25. responseReturn: 'body',
  26. });
  27. });
  28. it('should merge provided config with default config', async () => {
  29. const url = 'https://example.com/file';
  30. const mockBlob = new Blob(['file content'], { type: 'text/plain' });
  31. const mockResponse: Blob = mockBlob;
  32. mockAxiosInstance.get.mockResolvedValueOnce(mockResponse);
  33. const customConfig: AxiosRequestConfig = {
  34. headers: { 'Custom-Header': 'value' },
  35. };
  36. const result = await fileDownloader.download(url, customConfig);
  37. expect(result).toBeInstanceOf(Blob);
  38. expect(result).toEqual(mockBlob);
  39. expect(mockAxiosInstance.get).toHaveBeenCalledWith(url, {
  40. ...customConfig,
  41. responseType: 'blob',
  42. responseReturn: 'body',
  43. });
  44. });
  45. it('should handle errors gracefully', async () => {
  46. const url = 'https://example.com/file';
  47. mockAxiosInstance.get.mockRejectedValueOnce(new Error('Network Error'));
  48. await expect(fileDownloader.download(url)).rejects.toThrow('Network Error');
  49. });
  50. it('should handle empty URL gracefully', async () => {
  51. const url = '';
  52. mockAxiosInstance.get.mockRejectedValueOnce(
  53. new Error('Request failed with status code 404'),
  54. );
  55. await expect(fileDownloader.download(url)).rejects.toThrow(
  56. 'Request failed with status code 404',
  57. );
  58. });
  59. it('should handle null URL gracefully', async () => {
  60. const url = null as unknown as string;
  61. mockAxiosInstance.get.mockRejectedValueOnce(
  62. new Error('Request failed with status code 404'),
  63. );
  64. await expect(fileDownloader.download(url)).rejects.toThrow(
  65. 'Request failed with status code 404',
  66. );
  67. });
  68. });