UNPKG

@gravity-ui/data-source

Version:
74 lines 2.51 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import { renderHook } from '@testing-library/react'; import { getError, getStatus } from '../../../core'; import { useQueryResponses } from '../useQueryResponses'; import { useRefetchAll } from '../useRefetchAll'; import { useRefetchErrored } from '../useRefetchErrored'; jest.mock('../useRefetchAll'); jest.mock('../useRefetchErrored'); jest.mock('../../../core', function () { var originalModule = jest.requireActual('../../../core'); return _objectSpread(_objectSpread({}, originalModule), {}, { getStatus: jest.fn(), getError: jest.fn() }); }); describe('useQueryResponses', function () { var mockRefetchAll = jest.fn(); var mockRefetchErrored = jest.fn(); beforeEach(function () { jest.clearAllMocks(); useRefetchAll.mockReturnValue(mockRefetchAll); useRefetchErrored.mockReturnValue(mockRefetchErrored); getStatus.mockReturnValue('success'); getError.mockReturnValue(null); }); it('should return combined status, error, and refetch functions', function () { var responses = [{ status: 'loading', error: null, refetch: jest.fn() }, { status: 'success', error: null, refetch: jest.fn() }, { status: 'error', error: { message: 'Error' }, refetch: jest.fn() }]; var _renderHook = renderHook(function () { return useQueryResponses(responses); }), result = _renderHook.result; expect(getStatus).toHaveBeenCalledWith(responses); expect(getError).toHaveBeenCalledWith(responses); expect(useRefetchAll).toHaveBeenCalledWith(responses); expect(useRefetchErrored).toHaveBeenCalledWith(responses); expect(result.current).toEqual({ status: 'success', error: null, refetch: mockRefetchAll, refetchErrored: mockRefetchErrored }); }); it('should handle empty responses array', function () { var _renderHook2 = renderHook(function () { return useQueryResponses([]); }), result = _renderHook2.result; expect(getStatus).toHaveBeenCalledWith([]); expect(getError).toHaveBeenCalledWith([]); expect(useRefetchAll).toHaveBeenCalledWith([]); expect(useRefetchErrored).toHaveBeenCalledWith([]); expect(result.current).toEqual({ status: 'success', error: null, refetch: mockRefetchAll, refetchErrored: mockRefetchErrored }); }); }); // #sourceMappingURL=useQueryResponses.test.js.map