@gravity-ui/data-source
Version:
A wrapper around data fetching
95 lines • 3.11 kB
JavaScript
import { QueryClient } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react';
import { useInfiniteQueryData } from '../../impl/infinite/hooks';
import { usePlainQueryData } from '../../impl/plain/hooks';
import { notReachable } from '../../utils/notReachable';
import { useQueryContext } from '../useQueryContext';
import { useQueryData } from '../useQueryData';
jest.mock('../useQueryContext');
jest.mock('../../impl/plain/hooks');
jest.mock('../../impl/infinite/hooks');
jest.mock('../../utils/notReachable');
describe('useQueryData', function () {
var mockQueryClient = new QueryClient();
var mockContext = {
queryClient: mockQueryClient
};
var mockPlainState = {
status: 'success',
data: 'plain data'
};
var mockInfiniteState = {
status: 'success',
data: ['infinite data']
};
beforeEach(function () {
jest.clearAllMocks();
useQueryContext.mockReturnValue(mockContext);
usePlainQueryData.mockReturnValue(mockPlainState);
useInfiniteQueryData.mockReturnValue(mockInfiniteState);
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 = renderHook(function () {
return useQueryData(dataSource, params, options);
}),
result = _renderHook.result;
expect(useQueryContext).toHaveBeenCalled();
expect(usePlainQueryData).toHaveBeenCalledWith(mockContext, dataSource, params, options);
expect(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 = renderHook(function () {
return useQueryData(dataSource, params, options);
}),
result = _renderHook2.result;
expect(useQueryContext).toHaveBeenCalled();
expect(useInfiniteQueryData).toHaveBeenCalledWith(mockContext, dataSource, params, options);
expect(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.mockImplementation(function () {
return 'not reachable';
});
renderHook(function () {
return useQueryData(dataSource, params);
});
expect(usePlainQueryData).not.toHaveBeenCalled();
expect(useInfiniteQueryData).not.toHaveBeenCalled();
expect(notReachable).toHaveBeenCalledWith('unknown', expect.any(String));
});
});
// #sourceMappingURL=useQueryData.test.js.map