I have a hook that use one local variable and `apiCall` function. how can I mock the local variable `lastRecord` and `apiCall` function. so I can test the `if/else` logic?
// useApiCall.test.ts // I have tried // jest.mock(‘@src/api/apiCall’); // jest.spyOn(useApiCall.prototype as any, ‘lastRecord’).mockReturnValueOnce({ // name: ‘abc’, // address: ‘i am address, // }); // expect(apiCall).not.toHaveBeenCalled() // useApiCall.ts let lastRecord: QueryVariables | null = null; import {apiCall} from ‘@src/api/apiCall’; export function useApiCall( record: QueryVariables ): UseMutationResult<void, unknown, void, unknown> { return useMutation( async (): Promise<void> => { const isRecordChange = !isEqual(lastRecord, record); if (isRecordChange) { await apiCall(record); } }); }
submitted by /u/jacobcan118
[link] [comments]