酶/玩笑:组件(...):渲染未返回任何内容。这通常意味着缺少return语句。或者,不渲染任何内容,则返回null

时间:2020-02-04 09:05:01

标签: reactjs unit-testing testing jestjs enzyme

我知道这个问题已经在其他帖子中解决了,但是尽管提供了解决方案,我还是无法解决问题...

这是我要实现的测试:

import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.{SparkConf, SparkContext}

object Test extends App {
  val sparkConf = new SparkConf().setAppName(s"BigDataETL - Check if file exists")
  val sc = new SparkContext(sparkConf)
  val fs = FileSystem.get(sc.hadoopConfiguration)
  val fileExists = fs.exists(new Path("<parh_to_file>"))
  if (fileExists) println("File exists!")
  else println("File doesn't exist!")
}

要测试的组件是无状态组件:

import React from 'react'
import renderer from 'react-test-renderer'
import { Provider } from 'react-redux'
import store from '../core/redux-utils/store'
import { shallow, mount } from 'enzyme'
import Auth from '../screens/Auth'
import AuthForm from '../screens/Auth/AuthForm'
import { MDBBtn } from 'mdbreact'


jest.mock('react', () => {
  const React = jest.requireActual('react')
  React.Suspense = ({ children }) => children
  return React
})

describe('Test <Auth /> component', () => {
  it('renders as expected', () => {
    const wrapper = shallow(
      <Provider store={store}>
        <Auth />
      </Provider>
    )
    expect(wrapper.dive()).toMatchSnapshot()
  })
})

describe('Test <AuthForm /> component', () => {
  let wrapper
  let props

  beforeEach(() => {
    props = {
      handleSubmit: jest.fn(),
      t: jest.fn()
    }
    wrapper = mount(<AuthForm {...props} />, {
      disableLifecycleMethods: true
    })
  })

  it('should render correctly', () => {
    const tree = renderer.create(<AuthForm {...props} />)
    expect(tree.toJSON()).toMatchSnapshot()
  })

  it('should find submit button', () => {
    console.log(wrapper.debug())
    expect(wrapper.find('button').simulate('click'))
    expect(props.handleSubmit).toHaveBeenCalled()
  })
})

You can find the error here

我的目标是简单地成功测试组件的呈现,然后单击“提交”按钮。我应该怎么做 ?

谢谢您的帮助!

0 个答案:

没有答案