UNPKG

@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.

74 lines (73 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const react_1 = require("@testing-library/react"); const React = tslib_1.__importStar(require("react")); const infinite_1 = tslib_1.__importDefault(require("swr/infinite")); const useClientFetchModule = tslib_1.__importStar(require("./useClientFetch")); const useInfiniteQuery_1 = require("./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) => (0, react_1.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 } = (0, react_1.renderHook)(() => (0, useInfiniteQuery_1.useInfiniteQuery)(endpointId, fetcher, hookConfig)); expect(result.current).toBeTruthy(); expect(infinite_1.default).toHaveBeenCalledWith(expect.any(Function), expect.any(Function), { revalidateOnFocus: false, }); }); it('should combine hook level and global SWR configs when used', () => { const { result } = (0, react_1.renderHook)(() => (0, useInfiniteQuery_1.useInfiniteQuery)(endpointId, fetcher, hookConfig, undefined, undefined, { errorRetryCount: 5 })); expect(result.current).toBeTruthy(); expect(infinite_1.default).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 } = (0, react_1.renderHook)(() => (0, useInfiniteQuery_1.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 } = (0, react_1.renderHook)(() => (0, useInfiniteQuery_1.useInfiniteQuery)(endpointId, fetcher, hookConfig)); expect(result.current).toBeTruthy(); expect(mockUseClientFetch).toHaveBeenCalledWith(endpointId, 'query', hookConfig.fetchConfig, fetcher, undefined, undefined, undefined, undefined); }); });