@rocketmakers/api-swr
Version:
Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.
71 lines (70 loc) • 3.17 kB
JavaScript
import { act, renderHook } from '@testing-library/react';
import * as React from 'react';
import useSwrInfinite from 'swr/infinite';
import * as useClientFetchModule from './useClientFetch';
import { useInfiniteQuery } from './useInfiniteQuery';
// Mock index and prev data
const mockIndex = 0;
const mockPrevData = { test: 'OK' };
// Mock SWR infinite hook
jest.mock('swr/infinite', () => ({
__esModule: true,
default: jest.fn((key, innerFetcher) => {
const [data, setData] = React.useState();
React.useEffect(() => {
void innerFetcher(key(mockIndex, mockPrevData)).then((fetchedData) => act(() => setData(fetchedData)));
}, [innerFetcher, key]);
return { data };
}),
}));
// Mock param getter
const params = { id: 'test-cache-key-value' };
// Define test data
const fetcher = jest.fn().mockReturnValue(Promise.resolve({ test: 'OK' }));
const paramGetter = jest.fn().mockReturnValue(params);
const endpointId = 'controllerKey.endpointKey';
const hookConfig = {
cacheKey: 'id',
params: paramGetter,
fetchConfig: { headers: { Authorization: 'Bearer token' } },
swrConfig: { revalidateOnFocus: false },
};
// Mock client fetch
const mockUseClientFetch = jest.fn((incomingEndpointId, mode, conf, innerFetcher, incomingParams) => {
return {
validationErrors: [],
clientFetch: () => innerFetcher(incomingParams, conf),
data: undefined,
error: undefined,
isLoading: false,
};
});
jest.mock('./useClientFetch.ts');
useClientFetchModule.useClientFetch.mockImplementation(mockUseClientFetch);
describe('useInfiniteQuery', () => {
it('should call useSwrInfinite with the correct parameters when used', () => {
const { result } = renderHook(() => useInfiniteQuery(endpointId, fetcher, hookConfig));
expect(result.current).toBeTruthy();
expect(useSwrInfinite).toHaveBeenCalledWith(expect.any(Function), expect.any(Function), {
revalidateOnFocus: false,
});
});
it('should combine hook level and global SWR configs when used', () => {
const { result } = renderHook(() => useInfiniteQuery(endpointId, fetcher, hookConfig, undefined, undefined, { errorRetryCount: 5 }));
expect(result.current).toBeTruthy();
expect(useSwrInfinite).toHaveBeenCalledWith(expect.any(Function), expect.any(Function), {
errorRetryCount: 5,
revalidateOnFocus: false,
});
});
it('should call the param getter with the correct parameters when used', () => {
const { result } = renderHook(() => useInfiniteQuery(endpointId, fetcher, hookConfig));
expect(result.current).toBeTruthy();
expect(paramGetter).toHaveBeenCalledWith(mockIndex, mockPrevData);
});
it('should call useClientFetch with the correct parameters when used', () => {
const { result } = renderHook(() => useInfiniteQuery(endpointId, fetcher, hookConfig));
expect(result.current).toBeTruthy();
expect(mockUseClientFetch).toHaveBeenCalledWith(endpointId, 'query', hookConfig.fetchConfig, fetcher, undefined, undefined, undefined, undefined);
});
});