react-test-renderer

Jest - Testing modals in React gives error

旧街凉风 提交于 2020-12-12 05:27:16
问题 I am using react-test-renderer with Jest to test react components. But if I test a react-mui modal dialog like this: describe('Dashboard', function () { let dashboard; beforeEach(async () => { testRenderer = TestRenderer.create(<MemoryRouter><Route component={Dashboard} /></MemoryRouter>); dashboard = testRenderer.root.findByType(Dashboard); await waitForExpect(() => expect(dashboard.instance.state.hasLoaded).toBeTruthy()); }); it('opens dialog on clicking the new class', async () => { const

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:

React Jest test fails to run with ts-jest - Encountered an unexpected token on imported file

余生颓废 提交于 2020-06-27 08:40:49
问题 I'm new to testing React/Typescript apps. I want to unit test my React components, because I'm not satisfied to develop apps without tests at all. The app itself works fine, it's just failing to run in test mode. command (alias for react-scripts test ): yarn test output: FAIL src/containers/pages/Feature/Feature.test.tsx ● Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain

React-bootstrap Typeahead does not work with Jest snapshots

强颜欢笑 提交于 2020-02-06 08:02:35
问题 I have a very simple AsyncTypeahead example from the API documentation : import {AsyncTypeahead} from "react-bootstrap-typeahead"; import React, {Component} from "react"; class AsyncTest extends Component<Props, State> { constructor() { super() this.state = { isLoading:false, options:{} } } render() { return <AsyncTypeahead disabled={true} isLoading={this.state.isLoading} onSearch={query => { this.setState({isLoading: true}); fetch(`https://api.github.com/search/users?q=123`) .then(resp =>

How to write jest test cases for navigation in react

只愿长相守 提交于 2019-12-22 08:12:02
问题 I am new to Jest test cases writing, Trying to write the test case for checking navigation in an react application. Currently I have written test case which works like below: I have Login page which is having register link on it, which redirects user to the Register page. So what I am checking is 1. Loading the login page 2. Triggered Register link click event 3. So user moved to the Register page but I am not able to check if the Register page is loaded or not? Is there any way to check the

Testing React components that fetches data using Hooks

百般思念 提交于 2019-12-18 08:23:44
问题 My React-application has a component that fetches data to display from a remote server. In the pre-hooks era, componentDidMount() was the place to go. But now I wanted to use hooks for this. const App = () => { const [ state, setState ] = useState(0); useEffect(() => { fetchData().then(setState); }); return ( <div>... data display ...</div> ); }; And my test using Jest and Enzyme looks like this: import React from 'react'; import { mount } from 'enzyme'; import App from './App'; import { act

Jestjs how to test function being called inside another function

ぃ、小莉子 提交于 2019-12-12 10:44:12
问题 For testing I use jest and react-test-renderer. It should be simple to test, however I have a hard time finding the proper example. I have tried to do something like that (in general I keep the functions in separate files): utils.js export const childFunction = () => 'something'; const parentFunction = () => childFunction(); export default parentFunction; utils.test.js import parentFunction from './utils.js'; it('childFunction should be called', () => { const childFunction = jest.fn();

Cannot read property '_ownerDocument' of undefined while mocking ref

风格不统一 提交于 2019-12-11 15:37:09
问题 I am using createMockNode from react-test-renderer and have mocked out the style to prevent undefined errors. let createNodeMock = (element) => { if (element.type === 'input') { return { style: {"color":"red"}, }; } return null; } This has prevented the "undefined" error on style, but now I get this error : TypeError: Cannot read property '_ownerDocument' of undefined at unstable_runWithPriority (A:\frontend\node_modules\scheduler\cjs\scheduler.development.js:643:12) TypeError: Cannot read

How to write jest test cases for navigation in react

此生再无相见时 提交于 2019-12-05 13:49:54
I am new to Jest test cases writing, Trying to write the test case for checking navigation in an react application. Currently I have written test case which works like below: I have Login page which is having register link on it, which redirects user to the Register page. So what I am checking is 1. Loading the login page 2. Triggered Register link click event 3. So user moved to the Register page but I am not able to check if the Register page is loaded or not? Is there any way to check the snapshot of the "Register" page and link in URL. I have used jest, enzyme and react-test-render as