react-testing-library

How to fix the “Warning: useLayoutEffect does nothing on the server”?

大兔子大兔子 提交于 2020-08-24 05:27:12
问题 Heres the full error: Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client in ForwardRef(ButtonBase) in WithStyles(ForwardRef(ButtonBase)) in ForwardRef(Button) in WithStyles(ForwardRef(Button)) in form in div I get it every

react-test-renderer's create() vs. @testing-library/react's render()

我们两清 提交于 2020-08-09 12:31:48
问题 I'm new to React and confused about all the testing libraries. I got my test code to work but it seems redundant to have to call create() from react-test-renderer in order to use its toMatchSnapshot() and have to call render() from @testing-library/react in order to use its assertions such as getByLabelText() . import {render} from '@testing-library/react'; import {act, create} from 'react-test-renderer'; it('renders a login screen', () => { let mockInitialState: AppState = { auth:

react-test-renderer's create() vs. @testing-library/react's render()

∥☆過路亽.° 提交于 2020-08-09 12:31:19
问题 I'm new to React and confused about all the testing libraries. I got my test code to work but it seems redundant to have to call create() from react-test-renderer in order to use its toMatchSnapshot() and have to call render() from @testing-library/react in order to use its assertions such as getByLabelText() . import {render} from '@testing-library/react'; import {act, create} from 'react-test-renderer'; it('renders a login screen', () => { let mockInitialState: AppState = { auth:

How to test a component with the <Router> tag inside of it?

拈花ヽ惹草 提交于 2020-08-07 21:06:28
问题 About: Hey there, at the moment I am trying to write tests with jest and the react testing library for my react components. I also use react-router. The Problem: I want to check if the app routes to the right component when the path changes without interacting with the single components. So for example, if the current pathname is "/impressum" I want to have a Snapshot from just the Impressum page. I cannot figure out how to pass the path to <App> so that only one Route is displayed. The

How to test a component with the <Router> tag inside of it?

∥☆過路亽.° 提交于 2020-08-07 21:05:15
问题 About: Hey there, at the moment I am trying to write tests with jest and the react testing library for my react components. I also use react-router. The Problem: I want to check if the app routes to the right component when the path changes without interacting with the single components. So for example, if the current pathname is "/impressum" I want to have a Snapshot from just the Impressum page. I cannot figure out how to pass the path to <App> so that only one Route is displayed. The

Testing api call inside useEffect using react-testing-library

限于喜欢 提交于 2020-07-21 03:52:11
问题 I want to test api call and data returned which should be displayed inside my functional component. I created List component which performs api call. I would like the returned data to be displayed in the component and I use the useState hook for this. Component looks like this: const List: FC<{}> = () => { const [data, setData] = useState<number>(); const getData = (): Promise<any> => { return fetch('https://jsonplaceholder.typicode.com/todos/1'); }; React.useEffect(() => { const func = async

How to get babelify 10 to target a browser

大憨熊 提交于 2020-07-09 11:55:06
问题 What I want is To be using the latest versions of the libraries. For it to run in IE 11. I'm building a react app and in order to run the tests I need @testing-library/react. In order to run the tests in a browser I need browserify. In order to run the tests in every browser I support I need babelify. However no matter what I try the babelify does nothing (and IE 11 doesn't support ... or => syntax it generates). Here's the relevant part of package.json (some dependencies are likely not

How to mock API calls made within a React component being tested with Jest

断了今生、忘了曾经 提交于 2020-07-06 09:49:32
问题 I'm trying to mock a fetch() that retrieves data into a component. I'm using this as a model for mocking my fetches, but I'm having trouble getting it to work. I'm getting this error when I run my tests: babel-plugin-jest-hoist: The module factory of 'jest.mock()' is not allowed to reference any out-of-scope variables. Is there a way I can have these functions return mock data instead of actually trying to make real API calls? Code utils/getUsers.js Returns users with roles mapped into each

MaterialUI + React Testing Library: Unit test Select MenuItem breaks after upgrading to version 4

南笙酒味 提交于 2020-06-27 12:51:29
问题 I've got a unit test using Jest and React Testing Library that fills and submits a form. The problem is that after upgrading Material UI to version 4 my unit test fails to select an option. The error is: "Unable to find an element with the text: Brazil" Brazil is the text option I'm trying to select. Using Material UI version 3 was working just fine. Test Code - Gives error: "Unable to find an element with the text: Brazil." fireEvent.click(getByTestId("id-country")); const countryOption =

Testing a material ui slider with @testing-library/react

偶尔善良 提交于 2020-06-27 10:00:07
问题 Hi there I'm trying to test a Slider component created with Material-UI, but I cannot get my tests to penter link description hereass. I would like test the the value changes using the fireEvent with @testing-library/react . I've been following this post to properly query the DOM, I cannot get the correct DOM nodes. Thanks in advance. <Slider /> component // @format // @flow import * as React from "react"; import styled from "styled-components"; import { Slider as MaterialUISlider } from "