In this example we have a simple hook called useLog that returns a method. How do I test that it gets returned with react-hooks-testing-library. I am trying to figure out how to write the expect.
The hooks useLog:
import { useCallback } from 'react'
export const useLog = () => {
const log = useCallback(() => console.log("hello World"), [])
return { log }
}
test:
import { renderHook } from '@testing-library/react-hooks'
import {useLog} from "./useCounter";
const log = jest.fn()
test('should return log metod', () => {
const { result } = renderHook(() => useLog())
expect(result.current.log).toHaveReturnedWith(log);
})
what I get:
Matcher error: received value must be a mock function
Received has type: function
Received has value: [Function anonymous]
result.current.logisn't a mock function,logis. Butlogis unrelated to the code under test, it's not clear why that would be any part of the expectation. It's not clear to me whatuseLogis actually for, so it's hard to say what a good test for it would look like.result.current.logwill contain the callback function that you defined inside theuseLoghook. If you want to test that function, why not call it and assert that it's done what it should?should increment counter- what counter? How does the expectation relate to that? Then the expectation itself is that the memoized callback, which isn't a test double (and shouldn't be, it's part of the thing you're testing), gets called and returnslog(which has no connection to the code you're testing).