首页 > 解决方案 > 在 react js 中进行单元测试时,有没有办法改变 props 值?

问题描述

我正在为我的反应代码编写开玩笑的单元测试。firstName 是我从 PersonInfo.mock.js 获得的数据

import { firstName } from './PersonInfo.mock.js'

const PrintName = () => {
  const FirstName = () => {
    if ( firstName.length === 0 ) { 
      return <h1>No Name Found</h1>
    }else{
      return <h2>{firstName}</h2>
    }
  };

  return <>{FirstName()}</>
};

我尝试使用 setProps,但似乎无法更改为 firstName 的值。

describe('<PrintName/>', () => {
  const testContainer = shallow(<PrintName />);
  testContainer.setProps( {firstName : []} );

  it('should print No Name Found', () => {
    expect(testContainer.find('h1').exists()).toBeTruthy();
  });

知道如何更改/模拟我的名字的值吗?

标签: javascriptreactjsunit-testingjestjs

解决方案


推荐阅读