Questions tagged [react-hooks-testing-library]

Questions about testing React hooks with the react-hooks-testing-library utility.

react-hooks-testing-library

90 questions
1
vote
1 answer

Testing hooks which throw errors

The [deprecated?] react-hooks-testing-library would return any errors thrown by the hook under test. Probably my misunderstanding, but it looks like implementation now in the main @testing-library/react lost this feature? Here's what I'm…
1
vote
1 answer

How to mock useRouter parameters for react-hooks-testing-library?

I have a custom hook, which has structure of: const urlHook = () => { const router = useRouter(); const read = () => { return validate(router.query.param); } const write = (params) => { router.push( { query: { param: params, …
Alyona
  • 1,682
  • 2
  • 21
  • 44
1
vote
0 answers

Formik useFormikContext testing with testing-library

I am trying to write some unit tests for my input components, most of them are connected to Formik and there are cases like my Autocomplete component where the form gets validated when the user presses special keys i.e ( "Enter", ",", " ", etc ),…
1
vote
1 answer

Testing React Native Image.getSize: Cannot spyOn on a primitive value; string given

I have a hook that detects the orientation of a React Native Image: import { useState, useEffect } from 'react' import { Image } from 'react-native' const useFindImageSize = (image) => { const [width, setWidth] = useState(0) const [height,…
1
vote
0 answers

TypeError: Cannot read property 'hasOwnProperty' of undefined in renderHook import

I am trying to use renderHook from the testing-library. When I run my test I get this error, TypeError: Cannot read property 'hasOwnProperty' of undefined package.json "devDependencies": { "@babel/plugin-proposal-nullish-coalescing-operator":…
1
vote
2 answers

How to test that dispatch only gets called once

I have a custom hook that dispatches an action when a URL parameter changes: export const useUser = (): void => { const dispatch = useDispatch(); const { user } = useParams<{ user: string }>(); useEffect(() => {dispatch(getUser(user)); },…
1
vote
1 answer

The current result of a custom hook is updated when testing using react-hooks-testing-library

I am testing the custom React Hook shown below. Its functionality is to get the size of an image and then use the props provided to calculate the size of the image that the user wants. import { useEffect, useState } from 'react'; import { Image,…
1
vote
1 answer

Get Firebase Error: No Firebase App '[DEFAULT]' has been created when using React-hooks-testing-library with jest

I using Firebase auth in React, and I try to test it with react-hooks-testing-library. The code I write is working. But when I try to test with react-hooks-testing-library I get this error: FirebaseError: Firebase: No Firebase App '[DEFAULT]' has…
1
vote
1 answer

How can we test a customHook with useEffect with no return values inside by triggering it multiple times?

Hi all I am new to jest and now working on some tests for our customHooks. My customHook has useEffect inside and does not return value: const useCustomHook = (func: EffectCallback, deps?: DependencyList) => { const didMount = useRef(false); …
1
vote
1 answer

Testing async custom hooks that useEffect with react-hooks-testing-library

I've written a simple React hook and want to test it with react-hooks-testing-library. This hook calls an async function once both provider and domain variables, then once it's resolved puts data in state and returns it. I couldn't find anywhere how…
v1rtl
  • 185
  • 3
  • 8
1
vote
1 answer

How to use react-testing-library and jest with mocked custom react hook updating?

Look at the following custom hook. The premise is that it updates its state when query changes. export function UseCustomHook() { const { query } = useRouter() const [state, setState] = useState({}) useEffect(() => { const updatedState =…
1
vote
2 answers

waitFor times out after calling renderHook()

I am trying to test a simple custom hook: export const getSearchResults = async (searchText: string) => { const { data } = await axios.get(`${BASE_URL}/search`, { params: { searchText, apiToken: API_KEY }, }); return data…
1
vote
1 answer

Test custom hook with react-hooks-testing-library

I am trying to write tests for this custom hook using react-hooks-testing-library. import { deleteArticleById } from '../../API/articles/deleteArticleById'; const useArticleDelete = () => { const [articleId, setArticleId] = useState(null); const…
1
vote
1 answer

testing callback return value with react-hooks-testing-library

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…
1
vote
2 answers

how to test a hook with async state update in useEffect?

i have a simple hook that fetches the value and sets it to option as follows: import Fuse from 'fuse.js' import React from 'react' // prefetches options and uses fuzzy search to search on that option // instead of fetching on each keystroke export…