UNPKG

@gravity-ui/data-source

Version:
97 lines (96 loc) 3.35 kB
"use strict"; var _reactQuery = require("@tanstack/react-query"); var _react = require("@testing-library/react"); var _hooks = require("../../impl/infinite/hooks"); var _hooks2 = require("../../impl/plain/hooks"); var _notReachable = require("../../utils/notReachable"); var _useQueryContext = require("../useQueryContext"); var _useQueryData = require("../useQueryData"); jest.mock('../useQueryContext'); jest.mock('../../impl/plain/hooks'); jest.mock('../../impl/infinite/hooks'); jest.mock('../../utils/notReachable'); describe('useQueryData', function () { var mockQueryClient = new _reactQuery.QueryClient(); var mockContext = { queryClient: mockQueryClient }; var mockPlainState = { status: 'success', data: 'plain data' }; var mockInfiniteState = { status: 'success', data: ['infinite data'] }; beforeEach(function () { jest.clearAllMocks(); _useQueryContext.useQueryContext.mockReturnValue(mockContext); _hooks2.usePlainQueryData.mockReturnValue(mockPlainState); _hooks.useInfiniteQueryData.mockReturnValue(mockInfiniteState); _notReachable.notReachable.mockReturnValue('not reachable'); }); it('should call usePlainQueryData for plain data source', function () { var dataSource = { type: 'plain', name: 'test', fetch: jest.fn() }; var params = { id: 1 }; var options = { refetchInterval: 1000 }; var _renderHook = (0, _react.renderHook)(function () { return (0, _useQueryData.useQueryData)(dataSource, params, options); }), result = _renderHook.result; expect(_useQueryContext.useQueryContext).toHaveBeenCalled(); expect(_hooks2.usePlainQueryData).toHaveBeenCalledWith(mockContext, dataSource, params, options); expect(_hooks.useInfiniteQueryData).not.toHaveBeenCalled(); expect(result.current).toBe(mockPlainState); }); it('should call useInfiniteQueryData for infinite data source', function () { var dataSource = { type: 'infinite', name: 'test', fetch: jest.fn(), next: jest.fn() }; var params = { id: 1 }; var options = { refetchInterval: 1000 }; var _renderHook2 = (0, _react.renderHook)(function () { return (0, _useQueryData.useQueryData)(dataSource, params, options); }), result = _renderHook2.result; expect(_useQueryContext.useQueryContext).toHaveBeenCalled(); expect(_hooks.useInfiniteQueryData).toHaveBeenCalledWith(mockContext, dataSource, params, options); expect(_hooks2.usePlainQueryData).not.toHaveBeenCalled(); expect(result.current).toBe(mockInfiniteState); }); it('should call notReachable for unknown data source type', function () { var dataSource = { type: 'unknown', name: 'test', fetch: jest.fn() }; var params = { id: 1 }; _notReachable.notReachable.mockImplementation(function () { return 'not reachable'; }); (0, _react.renderHook)(function () { return (0, _useQueryData.useQueryData)(dataSource, params); }); expect(_hooks2.usePlainQueryData).not.toHaveBeenCalled(); expect(_hooks.useInfiniteQueryData).not.toHaveBeenCalled(); expect(_notReachable.notReachable).toHaveBeenCalledWith('unknown', expect.any(String)); }); }); // #sourceMappingURL=useQueryData.test.js.map