javascript - 如何测试: onClick event - Jest/Enzyeme

标签 javascript reactjs unit-testing

我是单元测试的新手,我刚刚运行了一个测试覆盖率。这是目前唯一缺少的行。

我如何测试这条线并确保 100% 被覆盖

   export default class Normalize extends Component {

 constructor(props) {
super(props)
this.state = {}
 //    this.handleChange = this.handleChange.bind(this)
}

 componentDidMount() {
 this.props.normalizeData(null)    
}

 render () {
return (
  <div id='normalize-container'>
    <div id='normalize-header'>
    </div>
    <br /><br />
    <h3 className='normalized-header'>{this.props.newResponse ? 'Raw Data' : 'Normalized Data'}</h3><br/>
    <div>
      {this.props.newResponse ? null :
        this.props.THead ?
          <div>
            {!this.props.datasourceCatalog ? 
              <div id='next-button-modal'>
                {/*<button title='Proceed To Shape' className='request-button' id='next-btn-ready-modal' onClick={this.props.nextTab}><img src={nextImg}/></button>*/}
                <button title='Proceed To Shape' className='request-button' id='next-btn-ready-modal' onClick={(e) => {this.props.toggleModal(); this.props.nextTab();}}>
                  <FontAwesome
                  className='fa-angle-right'
                  name='view-externallink-img'
                  size='2x'/>                  
                </button>

                <h4>Proceed To Shape</h4>
              </div> : 
            null}

            <div className='normalize-table-container'>
              <div className="data-table-wrapper">
                <table>
                  <thead dangerouslySetInnerHTML={{__html: this.props.THead}} />
                  <tbody dangerouslySetInnerHTML={{__html: this.props.TBody}} />
                </table>
              </div>
            </div>
          </div>
        : null
      }
    </div>
  </div>

使用 React JS - 笑话和 enzyme

测试文件:

// jest mock functions (mocks this.props.func)
const normalizeData =  jest.fn();
const toggleModal =  jest.fn();
const nextTab =  jest.fn();
const onClick =  jest.fn();

// defining this.props
const baseProps = {
normalizeData,
newResponse:{},
THead:{},
TBody:{},
datasourceCatalog:{},
toggleModal,
nextTab,
onClick,

describe(' Normalize Test', () => {
let wrapper;
let tree;

beforeEach(() => wrapper = shallow(<Normalize  {...baseProps} />));

it(' Should render with all of the props', () => { 

使用所有 Prop 进行渲染 - 但只需要确保如何测试上面的行,点击 2 个 Prop 。

谢谢

最佳答案

像这样的东西应该可以工作:

it('should call toggleModal and nextTab functions on button click', () => {
  // Reset info from possible previous calls of these mock functions:
  baseProps.toggleModal.mockClear();
  baseProps.nextTab.mockClear();

  // Remove props that would end up hiding the button
  wrapper.setProps({
    newResponse: null,
    datasourceCatalog: null
  });

  // Find the button and call the onClick handler
  wrapper.find('#next-btn-ready-modal').simulate('click');

  // Test to make sure prop functions were called via simulating the button click
  expect(baseProps.toggleModal).toHaveBeenCalled();
  expect(baseProps.nextTab).toHaveBeenCalled();
})

注意:您也可以将它们拆分为单独的测试,一个单独测试每个调用。

关于javascript - 如何测试: onClick event - Jest/Enzyeme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54607307/

相关文章:

C#:从测试方法调用时方法失败,但在其他情况下工作正常

python - django 测试文件下载 - "ValueError: I/O operation on closed file"

javascript - Backbonejs 下划线节流功能未触发

javascript 使用 getElementById 创建具有属性的对象

javascript - 创建后立即自动提交 ReactJS 表单

javascript - 具有 redux 形式的 React-day 选择器

javascript - Yii2 如何从特定 View 或 Controller 操作中包含多个 css 文件

javascript - 我如何从 Div Top 获取并添加到此 div top + 25?

reactjs - 错误: Do not use Array index in keys

c# - 运行测试与调试测试 = 不同的结果