I have a custom hook as below
export const useUserSearch = () => {
const [options, setOptions] = useState([])
const [searchString, setSearchString] = useState('')
const [userSearch] = useUserSearchMutation()
useEffect(() => {
if (searchString.trim().length > 3) {
const searchParams = {
orgId: '1',
userId: '1',
searchQuery: searchString.trim(),
}
userSearch(searchParams)
.then((data) => {
setOptions(data)
})
.catch((err) => {
setOptions([])
console.log('error', err)
})
}
}, [searchString, userSearch])
return {
options,
setSearchString,
}
}
and i want to test this hook but not able to mock userSearch function which is being called inside useEffect. can anybody help? this is my test
it('should set state and test function', async () => {
const wrapper = ({ children }) => (
<Provider store={store}>{children}</Provider>
)
const { result } = renderHook(
() => useUserSearch(),
{ wrapper }
)
await act(async () => {
result.current.setSearchString('abc5')
})
expect(result.current.options).toEqual(expected)
})
from Not able to mock a function inside useEffect
No comments:
Post a Comment