react-testing-library

get renderHook's result more than once

[亡魂溺海] 提交于 2020-01-16 09:07:10
问题 I got a test which calls renderHook like this: const { result } = renderHook(() => useMyHook(useDispatch()), wrapper); It's all good and working, and I can get the return value in result.current .. It seems like I can't call another hook with renderHook .. If I do the exact same thing, then it says that result is already defined. If I change the name to something else, like "res", it says that res is undefined, like it's only working for the specific name "result". So how can I call it more

Testing Semantic-UI Dropdown: How to wait for it to be populated?

二次信任 提交于 2020-01-16 08:36:23
问题 I've successfully implemented the Semantic-UI Dropdown to display a list of companies. Now I'm trying to build Jest / React Testing Library tests for it. To accomplish this, I built this Mock Request: beforeEach(() => { request.get = jest.fn(() => Promise.resolve({ companies: [ {"company_id": 1, "company_name": "ABC"}, {"company_id": 2, "company_name": "DEF"} ] })); }); Based on a console.log I added to my component code, this appears to work as expected. Here's an abridged example of my

SyntaxError: Missing initializer in const declaration

允我心安 提交于 2020-01-10 04:14:06
问题 I am writing a simple test using react-native-testing-library (my first steps with that library) in my react native expo app. But I am getting a confused error coming from somewhere inside react-native code base itself. Either there is something wrong with my code or there is a bug with react-native-testing-library npm library. Here is simple jest test: describe("AppTitle", () => { it("should display applicaton title", () => { const { getByText } = render(<AppTitle />); expect(getByText('App

Testing custom hook: Invariant Violation: could not find react-redux context value; please ensure the component is wrapped in a <Provider>

回眸只為那壹抹淺笑 提交于 2020-01-06 05:36:12
问题 I got a custom hook which I want to test. It receives a redux store dispatch function and returns a function. In order to get the result I'm trying to do: const { result } = renderHook(() => { useSaveAuthenticationDataToStorages(useDispatch())}); However, I get an error: Invariant Violation: could not find react-redux context value; please ensure the component is wrapped in a It happens because of the useDispatch and that there is no store connected. However, I don't have any component here

TypeError: scrollIntoView is not a function

眉间皱痕 提交于 2019-12-30 17:26:41
问题 I am new to react-testing-library / jest and attempting to write a test to see if the navigation of routes (using react-router-dom) is performed correctly. So far I have been following the README and this tutorial on how to use. One of my components uses scrollIntoView in a local function and this causes the test to fail. TypeError: this.messagesEnd.scrollIntoView is not a function 45 | 46 | scrollToBottom = () => { > 47 | this.messagesEnd.scrollIntoView({ behavior: "smooth" }); | ^ 48 | } 49

React + Jest + testing-library: “Please upgrade to at least react-dom@16.9.0 to remove this warning.” …but react-dom is 16.9.0 already

旧城冷巷雨未停 提交于 2019-12-24 23:25:03
问题 I'm writing tests for my React app using Jest and testing-library/react My tests pass but I can't get rid of this error: It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning. I am also seeing multiple instances of this warning: Warning: An update to ConnectFunction inside a test was not wrapped in act(...). When testing, code that causes

Error when trying to render component in react-testing-library - got an object

空扰寡人 提交于 2019-12-24 07:38:04
问题 I'm trying to setup some integration tests of some react components that are connected to our redux store. Our general pattern is a const (instead of a component) which is "connect"ed with redux to the store (I think this is just background to the issue, as I have the same problem with a normal component). I am following https://testing-library.com/docs/example-react-redux I get this error console.error node_modules/react/cjs/react.development.js:172 Warning: React.createElement: type is

React testing library: Test attribute / prop

旧城冷巷雨未停 提交于 2019-12-22 05:34:14
问题 I'm writing a React app using TypeScript. I use material-ui for my components and react-testing-library for my unit tests. I'm writing a wrapper for material-ui's Grid component so that I always have an item. import Grid from "@material-ui/core/Grid"; import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles"; import React, { PureComponent } from "react"; import styles from "./styles"; export interface OwnProps { className?: string; } export interface Props extends

Jest mock react context

对着背影说爱祢 提交于 2019-12-22 04:01:19
问题 I need some help understanding how one can test an application using React Context . Here's my sample setup. context.js import React from 'react' export const AppContext = React.createContext() App.js import React from 'react' import MyComponent from './MyComponent' import {AppContext} from './context' const App extends React.Component { state = { items: [] } handleItemAdd = newItem => { const {items} = this.state items.push(newItem) this.setState(items) } render() { return ( <AppContext

React-Testing-Library - Wrapping Component with Redux and Router

拜拜、爱过 提交于 2019-12-21 18:05:29
问题 I am trying to setup an test file to render a route/page on my application. I'm trying to wrap everything with Redux and Router, and this is what I have: import React from 'react'; import { render } from 'react-testing-library'; import { createStore } from 'redux'; import { Provider } from 'react-redux'; import reducer from '../../store/reducer'; import {Link, Route, Router, Switch} from 'react-router-dom' import {createMemoryHistory} from 'history' import ViewNode from '../Pages/ViewNode';