request-client.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import axios from 'axios';
  2. import MockAdapter from 'axios-mock-adapter';
  3. import { afterEach, beforeEach, describe, expect, it } from 'vitest';
  4. import { RequestClient } from './request-client';
  5. describe('requestClient', () => {
  6. let mock: MockAdapter;
  7. let requestClient: RequestClient;
  8. beforeEach(() => {
  9. mock = new MockAdapter(axios);
  10. requestClient = new RequestClient();
  11. });
  12. afterEach(() => {
  13. mock.reset();
  14. });
  15. it('should successfully make a GET request', async () => {
  16. mock.onGet('test/url').reply(200, { data: 'response' });
  17. const response = await requestClient.get('test/url');
  18. expect(response.data).toEqual({ data: 'response' });
  19. });
  20. it('should successfully make a POST request', async () => {
  21. const postData = { key: 'value' };
  22. const mockData = { data: 'response' };
  23. mock.onPost('/test/post', postData).reply(200, mockData);
  24. const response = await requestClient.post('/test/post', postData);
  25. expect(response.data).toEqual(mockData);
  26. });
  27. it('should successfully make a PUT request', async () => {
  28. const putData = { key: 'updatedValue' };
  29. const mockData = { data: 'updated response' };
  30. mock.onPut('/test/put', putData).reply(200, mockData);
  31. const response = await requestClient.put('/test/put', putData);
  32. expect(response.data).toEqual(mockData);
  33. });
  34. it('should successfully make a DELETE request', async () => {
  35. const mockData = { data: 'delete response' };
  36. mock.onDelete('/test/delete').reply(200, mockData);
  37. const response = await requestClient.delete('/test/delete');
  38. expect(response.data).toEqual(mockData);
  39. });
  40. it('should handle network errors', async () => {
  41. mock.onGet('/test/error').networkError();
  42. try {
  43. await requestClient.get('/test/error');
  44. expect(true).toBe(false);
  45. } catch (error: any) {
  46. expect(error.isAxiosError).toBe(true);
  47. expect(error.message).toBe('Network Error');
  48. }
  49. });
  50. it('should handle timeout', async () => {
  51. mock.onGet('/test/timeout').timeout();
  52. try {
  53. await requestClient.get('/test/timeout');
  54. expect(true).toBe(false);
  55. } catch (error: any) {
  56. expect(error.isAxiosError).toBe(true);
  57. expect(error.code).toBe('ECONNABORTED');
  58. }
  59. });
  60. it('should successfully upload a file', async () => {
  61. const fileData = new Blob(['file contents'], { type: 'text/plain' });
  62. mock.onPost('/test/upload').reply((config) => {
  63. return config.data instanceof FormData && config.data.has('file')
  64. ? [200, { data: 'file uploaded' }]
  65. : [400, { error: 'Bad Request' }];
  66. });
  67. const response = await requestClient.upload('/test/upload', {
  68. file: fileData,
  69. });
  70. expect(response.data).toEqual({ data: 'file uploaded' });
  71. });
  72. it('should successfully download a file as a blob', async () => {
  73. const mockFileContent = new Blob(['mock file content'], {
  74. type: 'text/plain',
  75. });
  76. mock.onGet('/test/download').reply(200, mockFileContent);
  77. const res = await requestClient.download('/test/download');
  78. expect(res.data).toBeInstanceOf(Blob);
  79. });
  80. });