improved-purpleI
Refine3y ago
7 replies
improved-purple

Mocking useTable with Jest

Hey there - I'm trying to mock the output of useTable to check the behavior of a custom hook. Here is my code -

import { useTable } from '@pankod/refine-core';
import { renderHook } from '@testing-library/react';

const isError = {
tableQueryResult: {
data: undefined,
dataUpdatedAt: 0,
error: {
isAxiosError: true
},
errorUpdateCount: 1,
errorUpdatedAt: 1674504481247,
failureCount: 4,
failureReason: {
isAxiosError: true
},
fetchStatus: "idle",
isError: true,
isFetched: true,
isFetchedAfterMount: true,
isFetching: false,
isInitialLoading: false,
isLoading: false,
isLoadingError: true,
isPaused: false,
isPlaceholderData: false,
isPreviousData: false,
isRefetching: false,
isStale: true,
isSuccess: false,
refetch: () => { },
remove: () => { },
status: "error"
}
}

let mockData: any;

jest.mock('@pankod/refine-core', () => {
return {
__esModule: true,
useTable: jest.fn(() => (mockData))
};
});

describe('test useTableCustom hook', () => {

afterEach(() => {
jest.clearAllMocks();
});

it('successfully uses useTable', () => {
mockData = isError;
const { result } = renderHook(() => useTable({ resource: '' }));
expect(result.current).toBeTruthy();
});
});

result turns out to be undefined, test fails. Did I set up the mock incorrectly - is there any example in the source code of mocking the output of useTable?
Was this page helpful?