|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { resolveResponse } from '../responseUtils.js'; |
| 3 | + |
| 4 | +describe('resolveResponse', () => { |
| 5 | + it('should handle Error without promise', async () => { |
| 6 | + const testError = new Error('Test error'); |
| 7 | + |
| 8 | + const result = await resolveResponse<unknown, Error>(testError); |
| 9 | + |
| 10 | + expect(result).toEqual({ |
| 11 | + error: testError, |
| 12 | + response: undefined, |
| 13 | + data: undefined, |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should handle Response without promise', async () => { |
| 18 | + const mockResponse = new Response(null); |
| 19 | + |
| 20 | + // We use casting to bypass type checking, since we know that the function accepts Response |
| 21 | + // without a promise in the implementation, although types do not allow it directly. |
| 22 | + const result = await (resolveResponse as any)(mockResponse); |
| 23 | + |
| 24 | + expect(result).toEqual({ |
| 25 | + error: expect.any(Error), |
| 26 | + response: mockResponse, |
| 27 | + data: undefined, |
| 28 | + }); |
| 29 | + expect((result.error as Error).message).toBe( |
| 30 | + 'Unhandled response without promise to resolve' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should resolve promise with data and response', async () => { |
| 35 | + const mockResponse = new Response(null); |
| 36 | + const testData = { test: 'data' }; |
| 37 | + const dataPromise = Promise.resolve(testData); |
| 38 | + |
| 39 | + const result = await resolveResponse<typeof testData, Error>( |
| 40 | + mockResponse, |
| 41 | + dataPromise |
| 42 | + ); |
| 43 | + |
| 44 | + expect(result).toEqual({ |
| 45 | + data: testData, |
| 46 | + response: mockResponse, |
| 47 | + error: undefined, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle rejected promise with error and response', async () => { |
| 52 | + const mockResponse = new Response(null); |
| 53 | + const testError = new Error('Test rejection'); |
| 54 | + const dataPromise = Promise.reject(testError); |
| 55 | + |
| 56 | + const result = await resolveResponse<unknown, Error>( |
| 57 | + mockResponse, |
| 58 | + dataPromise |
| 59 | + ); |
| 60 | + |
| 61 | + expect(result).toEqual({ |
| 62 | + error: testError, |
| 63 | + response: mockResponse, |
| 64 | + data: undefined, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should handle custom error objects in rejected promise', async () => { |
| 69 | + const mockResponse = new Response(null); |
| 70 | + type CustomError = { code: string; message: string }; |
| 71 | + const customError: CustomError = { |
| 72 | + code: 'CUSTOM_ERROR', |
| 73 | + message: 'Custom error object', |
| 74 | + }; |
| 75 | + const dataPromise = Promise.reject(customError); |
| 76 | + |
| 77 | + const result = await resolveResponse<unknown, CustomError>( |
| 78 | + mockResponse, |
| 79 | + dataPromise |
| 80 | + ); |
| 81 | + |
| 82 | + expect(result).toEqual({ |
| 83 | + error: customError, |
| 84 | + response: mockResponse, |
| 85 | + data: undefined, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should preserve response object when promise resolves', async () => { |
| 90 | + const headers = new Headers(); |
| 91 | + headers.append('Content-Type', 'application/json'); |
| 92 | + const mockResponse = new Response('{"key":"value"}', { headers }); |
| 93 | + const testData = { key: 'value' }; |
| 94 | + const dataPromise = Promise.resolve(testData); |
| 95 | + |
| 96 | + const result = await resolveResponse<typeof testData, Error>( |
| 97 | + mockResponse, |
| 98 | + dataPromise |
| 99 | + ); |
| 100 | + |
| 101 | + expect(result.response).toBe(mockResponse); |
| 102 | + expect(result.data).toEqual(testData); |
| 103 | + expect(result.error).toBeUndefined(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments