@gravity-ui/data-source
Version:
A wrapper around data fetching
208 lines (206 loc) • 8.69 kB
JavaScript
;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _withCatch = require("../withCatch");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
describe('withCatch', function () {
it('should return the result of the fetch function when it succeeds', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee() {
var mockFetch, mockErrorHandler, safeFetch, result;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee2() {
var error, mockFetch, mockErrorHandler, safeFetch, result;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee3() {
var mockFetch, mockErrorHandler, safeFetch, result;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee4() {
var mockFetch, mockErrorHandler, safeFetch, result;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee5() {
var error, mockFetch, mockErrorHandler, safeFetch, result;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee6() {
var mockFetch, mockErrorHandler, safeFetch, result, userName;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee7() {
var error, mockFetch, mockErrorHandler, safeFetch, result, errorCode;
return (0, _regeneratorRuntime2.default)().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 = (0, _withCatch.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 ((0, _typeof2.default)(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