UNPKG

@gravity-ui/data-source

Version:
205 lines (204 loc) 8.09 kB
import _typeof from "@babel/runtime/helpers/typeof"; import _regeneratorRuntime from "@babel/runtime/helpers/regeneratorRuntime"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import { withCatch } from '../withCatch'; describe('withCatch', function () { it('should return the result of the fetch function when it succeeds', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() { var mockFetch, mockErrorHandler, safeFetch, result; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: mockFetch = jest.fn().mockResolvedValue({ data: 'success' }); mockErrorHandler = jest.fn().mockReturnValue({ error: 'handled' }); safeFetch = withCatch(mockFetch, mockErrorHandler); _context.next = 5; return safeFetch('arg1', 42); case 5: result = _context.sent; expect(mockFetch).toHaveBeenCalledWith('arg1', 42); expect(mockErrorHandler).not.toHaveBeenCalled(); expect(result).toEqual({ data: 'success' }); case 9: case "end": return _context.stop(); } }, _callee); }))); it('should call the error handler when the fetch function fails', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { var error, mockFetch, mockErrorHandler, safeFetch, result; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: error = new Error('fetch failed'); mockFetch = jest.fn().mockRejectedValue(error); mockErrorHandler = jest.fn().mockReturnValue({ error: 'handled' }); safeFetch = withCatch(mockFetch, mockErrorHandler); _context2.next = 6; return safeFetch('arg1', 42); case 6: result = _context2.sent; expect(mockFetch).toHaveBeenCalledWith('arg1', 42); expect(mockErrorHandler).toHaveBeenCalledWith(error); expect(result).toEqual({ error: 'handled' }); case 10: case "end": return _context2.stop(); } }, _callee2); }))); it('should work with functions that take no parameters', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { var mockFetch, mockErrorHandler, safeFetch, result; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: mockFetch = jest.fn().mockResolvedValue({ data: 'success' }); mockErrorHandler = jest.fn().mockReturnValue({ error: 'handled' }); safeFetch = withCatch(mockFetch, mockErrorHandler); _context3.next = 5; return safeFetch(); case 5: result = _context3.sent; expect(mockFetch).toHaveBeenCalledWith(); expect(mockErrorHandler).not.toHaveBeenCalled(); expect(result).toEqual({ data: 'success' }); case 9: case "end": return _context3.stop(); } }, _callee3); }))); it('should work with functions that take multiple parameters', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() { var mockFetch, mockErrorHandler, safeFetch, result; return _regeneratorRuntime().wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: mockFetch = jest.fn().mockResolvedValue({ data: 'success' }); mockErrorHandler = jest.fn().mockReturnValue({ error: 'handled' }); safeFetch = withCatch(mockFetch, mockErrorHandler); _context4.next = 5; return safeFetch('arg1', 42, true, { complex: 'object' }); case 5: result = _context4.sent; expect(mockFetch).toHaveBeenCalledWith('arg1', 42, true, { complex: 'object' }); expect(mockErrorHandler).not.toHaveBeenCalled(); expect(result).toEqual({ data: 'success' }); case 9: case "end": return _context4.stop(); } }, _callee4); }))); it('should handle error handlers that return promises', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() { var error, mockFetch, mockErrorHandler, safeFetch, result; return _regeneratorRuntime().wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: error = new Error('fetch failed'); mockFetch = jest.fn().mockRejectedValue(error); mockErrorHandler = jest.fn().mockResolvedValue({ error: 'async handled' }); safeFetch = withCatch(mockFetch, mockErrorHandler); _context5.next = 6; return safeFetch('arg1'); case 6: result = _context5.sent; expect(mockFetch).toHaveBeenCalledWith('arg1'); expect(mockErrorHandler).toHaveBeenCalledWith(error); expect(result).toEqual({ error: 'async handled' }); case 10: case "end": return _context5.stop(); } }, _callee5); }))); it('should preserve the type of the fetch function return value', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() { var mockFetch, mockErrorHandler, safeFetch, result, userName; return _regeneratorRuntime().wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: mockFetch = jest.fn().mockResolvedValue({ id: 1, name: 'John Doe' }); mockErrorHandler = jest.fn().mockReturnValue(null); safeFetch = withCatch(mockFetch, mockErrorHandler); _context6.next = 5; return safeFetch('user1'); case 5: result = _context6.sent; expect(result).toEqual({ id: 1, name: 'John Doe' }); // TypeScript should recognize result as User | null if (result !== null) { // This should compile without errors userName = result.name; expect(userName).toBe('John Doe'); } case 8: case "end": return _context6.stop(); } }, _callee6); }))); it('should preserve the type of the error handler return value', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() { var error, mockFetch, mockErrorHandler, safeFetch, result, errorCode; return _regeneratorRuntime().wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: error = new Error('fetch failed'); mockFetch = jest.fn().mockRejectedValue(error); mockErrorHandler = jest.fn().mockReturnValue({ code: 500, message: 'Internal Server Error' }); safeFetch = withCatch(mockFetch, mockErrorHandler); _context7.next = 6; return safeFetch('user1'); case 6: result = _context7.sent; expect(mockErrorHandler).toHaveBeenCalledWith(error); // TypeScript should recognize result as unknown | ErrorResponse if (_typeof(result) === 'object' && result !== null && 'code' in result) { // This should compile without errors errorCode = result.code; expect(errorCode).toBe(500); } case 9: case "end": return _context7.stop(); } }, _callee7); }))); }); // #sourceMappingURL=withCatch.test.js.map