简体   繁体   English

如何为使用 jest 调用方法的组件编写测试用例

[英]How to write test case for a component which is calling a method using jest

I am new to Jest and Enzyme & facing an issue.我是 Jest 和 Enzyme 的新手,面临一个问题。 Can someone help?有人可以帮忙吗? Here is my jsx file (myTemplate.jsx):这是我的 jsx 文件(myTemplate.jsx):

export default (props) => {
  const oneSection = () => {
        return <div>Hello</div>
    };

  return (
    props.hasData ? { <div>Hey</div> } : { <div><oneSection/></div> }
   )
}

And this is my test file:这是我的测试文件:

import React from "react";
import { shallow } from "enzyme";
import myTemplate from '../myTemplate';

 describe("template component", () => {
 const props= {
        hasData: false,
 }
 let myComponent = null;
    beforeAll(() => {
        myComponent = shallow(<myTemplate {...props}/>);
    })
    test("should render with initial state properly", () => {
        expect(myComponent).toMatchSnapshot();
    })
  })

Now this test case is running successfully.现在这个测试用例运行成功了。 Snapshot is getting created with a div which has oneSection but oneSection is not getting replaced with actual html within it.使用具有 oneSection 的 div 创建快照,但 oneSection 没有被其中的实际 html 替换。 Basically these lines are not getting covered:基本上这些行没有被覆盖:

 const oneSection = () => {
            return <div>Hello</div>
        };

How can i cover this piece of code using Jest and enzyme?我如何使用 Jest 和酶覆盖这段代码?

import React from 'react'
import { cleanup, render } from '@testing-library/react'

describe('DMReports', () => {
  afterEach(cleanup)
  test('DMReports: hasData true', () => {
    const { container } = render(
      <DMReports hasData={true}/>,
    )
    expect(container).toMatchSnapshot()
  })
  test('DMReports: hasData false', () => {
    const { container, getByText } = render(
      <DMReports hasData={false}/>,
    )
    expect(container).toMatchSnapshot()
    expect(getByText('Hello')).toBeTruthy()
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Jest和Enzyme为简单的React组件编写测试用例 - How to write a test case for a simple React component using Jest and Enzyme 如何使用 jest 为使用 uuid 的函数编写测试用例? - How do I write test case for a function which uses uuid using jest? 玩笑和酶反应成分方法的测试案例 - test case for a react component method with jest and enzyme 我应该如何使用 Jest 为我的 Vue 组件编写测试? - How should I write a test for my Vue component using Jest? 在 JEST-Enzyme 中为 promise 在构造函数中调用编写测试用例 - Write test-case in JEST-Enzyme for promise calling in constructor 如何使用 React 测试库为返回 Jest 中的组件的 Switch 语句编写单元测试用例 - How to write Unit Test case for Switch Statement that returns Component in Jest with React Testing Library 如何为 React useEffect 中的代码编写 Jest-enzyme 测试用例,其依赖项在其子组件中更新 - How to write a Jest-enzyme test case for code in React useEffect whose dependencies are updated in its child component 如何使用 Jest - Enzyme 测试 React 中 mapStateToProps 中的方法 - How to test a method which is in mapStateToProps in React using Jest - Enzyme 使用Jest测试React组件的方法 - Using Jest to test a React component's method 如何为使用Vuex存储的Vue表单组件编写Jest单元测试? - How to write Jest unit test for a Vue form component which uses a Vuex store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM