uploader.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import type { AxiosRequestConfig, AxiosResponse } from 'axios';
  2. import { beforeEach, describe, expect, it, vi } from 'vitest';
  3. import { FileUploader } from './uploader';
  4. describe('fileUploader', () => {
  5. let fileUploader: FileUploader;
  6. // Mock the AxiosInstance
  7. const mockAxiosInstance = {
  8. post: vi.fn(),
  9. } as any;
  10. beforeEach(() => {
  11. fileUploader = new FileUploader(mockAxiosInstance);
  12. });
  13. it('should create an instance of FileUploader', () => {
  14. expect(fileUploader).toBeInstanceOf(FileUploader);
  15. });
  16. it('should upload a file and return the response', async () => {
  17. const url = 'https://example.com/upload';
  18. const file = new File(['file content'], 'test.txt', { type: 'text/plain' });
  19. const mockResponse: AxiosResponse = {
  20. config: {} as any,
  21. data: { success: true },
  22. headers: {},
  23. status: 200,
  24. statusText: 'SUCCESS',
  25. };
  26. (
  27. mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
  28. ).mockResolvedValueOnce(mockResponse);
  29. const result = await fileUploader.upload(url, { file });
  30. expect(result).toEqual(mockResponse);
  31. expect(mockAxiosInstance.post).toHaveBeenCalledWith(
  32. url,
  33. expect.any(FormData),
  34. {
  35. headers: {
  36. 'Content-Type': 'multipart/form-data',
  37. },
  38. },
  39. );
  40. });
  41. it('should merge provided config with default config', async () => {
  42. const url = 'https://example.com/upload';
  43. const file = new File(['file content'], 'test.txt', { type: 'text/plain' });
  44. const mockResponse: AxiosResponse = {
  45. config: {} as any,
  46. data: { success: true },
  47. headers: {},
  48. status: 200,
  49. statusText: 'SUCCESS',
  50. };
  51. (
  52. mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
  53. ).mockResolvedValueOnce(mockResponse);
  54. const customConfig: AxiosRequestConfig = {
  55. headers: { 'Custom-Header': 'value' },
  56. };
  57. const result = await fileUploader.upload(url, { file }, customConfig);
  58. expect(result).toEqual(mockResponse);
  59. expect(mockAxiosInstance.post).toHaveBeenCalledWith(
  60. url,
  61. expect.any(FormData),
  62. {
  63. headers: {
  64. 'Content-Type': 'multipart/form-data',
  65. 'Custom-Header': 'value',
  66. },
  67. },
  68. );
  69. });
  70. it('should handle errors gracefully', async () => {
  71. const url = 'https://example.com/upload';
  72. const file = new File(['file content'], 'test.txt', { type: 'text/plain' });
  73. (
  74. mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
  75. ).mockRejectedValueOnce(new Error('Network Error'));
  76. await expect(fileUploader.upload(url, { file })).rejects.toThrow(
  77. 'Network Error',
  78. );
  79. });
  80. it('should handle empty URL gracefully', async () => {
  81. const url = '';
  82. const file = new File(['file content'], 'test.txt', { type: 'text/plain' });
  83. (
  84. mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
  85. ).mockRejectedValueOnce(new Error('Request failed with status code 404'));
  86. await expect(fileUploader.upload(url, { file })).rejects.toThrow(
  87. 'Request failed with status code 404',
  88. );
  89. });
  90. it('should handle null URL gracefully', async () => {
  91. const url = null as unknown as string;
  92. const file = new File(['file content'], 'test.txt', { type: 'text/plain' });
  93. (
  94. mockAxiosInstance.post as unknown as ReturnType<typeof vi.fn>
  95. ).mockRejectedValueOnce(new Error('Request failed with status code 404'));
  96. await expect(fileUploader.upload(url, { file })).rejects.toThrow(
  97. 'Request failed with status code 404',
  98. );
  99. });
  100. });