Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何在react组件';孩子_Unit Testing_Reactjs_Mocha.js_Sinon_Enzyme - Fatal编程技术网

Unit testing 如何在react组件';孩子

Unit testing 如何在react组件';孩子,unit-testing,reactjs,mocha.js,sinon,enzyme,Unit Testing,Reactjs,Mocha.js,Sinon,Enzyme,Hy,我不知道如何在React组件的子组件中模拟内联函数 我的堆栈:sinon,chai,enzyme 组件使用: <ListItem onClick={() => someFn()} /> 测试呢 it('simulates click events', () => { const { link, actions } = setup(); link.simulate('click'); //Click on <a href> e

Hy,我不知道如何在React组件的子组件中模拟内联函数

我的堆栈:
sinon
chai
enzyme

组件使用:

<ListItem onClick={() => someFn()} />
测试呢

  it('simulates click events', () => {
    const { link, actions } = setup();
    link.simulate('click'); //Click on <a href>
    expect(actions).to.have.property('callCount', 1); //will be fine if we remove e.preventDefault()
  });
试试这个

link.simulate('click', {
  preventDefault: () => {
  }
 });
test('模拟点击事件',()=>{
const e={stopPropagation:jest.fn()};
常量分量=浅();
const li=component.find('li').at(0).childAt(0)
li.props().onClick(e)
expect();
});

请注意,只有在使用
浅层
渲染器时才会出现此问题。对于完整DOM呈现程序
mount
,事件对象包含
preventDefault
方法,因此您不必模拟它

我建议使用

然后使用它:

element.simulate('click', event);

对于那些使用Jest和
@测试库
react测试库
s
firevent
的用户,需要提供一个初始化的事件对象,否则无法通过元素调度事件

然后,可以通过为初始化事件指定属性来断言调用
e.preventDefault

test('防止单击时出现默认值',()=>{
const{getByText}=render();
const button=getByText(/click me/);
//初始化事件,并指定自己的默认值
const clickEvent=new MouseEvent('click');
赋值(clickEvent,{preventDefault:jest.fn()});
fireEvent(按钮,点击事件);
期望(clickEvent.preventDefault).已调用时间(1);
});
类似地,对于停止播放

因为Jest很有用。

您可以通过一些测试工具定义一个对象,其中包含您将要模拟的功能,例如查看Jest

describe('Form component', () => {
  test('deos not reload page after submition', () => {
    const wrapper = shallow(<TodosForm />)
    // an object with some function
    const event = { preventDefault: () => {} }
    // mocks for this function
    jest.spyOn(event, 'preventDefault')
    wrapper.find('form').simulate('submit', event)
    // how would you know that function is called
    expect(event.preventDefault).toBeCalled()
  })
})
description('表单组件',()=>{
测试('deos提交后不重新加载页面',()=>{
常量包装器=浅()
//具有某种功能的对象
const event={preventDefault:()=>{}
//此函数的模拟
jest.spyOn(事件'preventDefault')
wrapper.find('form').simulate('submit',event)
//您如何知道调用了该函数
expect(event.preventDefault).toBeCalled()
})
})

我正在使用Web组件,这对我很有用-

  const callback = jest.fn();
  MouseEvent.prototype.stopPropagation = callback;
  const element = createElement({});
  element.shadowRoot.querySelector('ul').click();
  expect(callback).toHaveBeenCalledTimes(1);

最后一行对吗
event.preventDefault
expect如何知道
event
的上下文?@sidonaldson感谢您的帮助-修复了引用
clickEvent
的示例我有invoke方法,这对我不适用
 test('simulates click events', () => {
    const e = { stopPropagation: jest.fn() };
    const component = shallow(<ListItem{...props} />);
    const li = component.find('li').at(0).childAt(0)
    li.props().onClick(e)

    expect();
  });
const event = Object.assign(jest.fn(), {preventDefault: () => {}})
element.simulate('click', event);
describe('Form component', () => {
  test('deos not reload page after submition', () => {
    const wrapper = shallow(<TodosForm />)
    // an object with some function
    const event = { preventDefault: () => {} }
    // mocks for this function
    jest.spyOn(event, 'preventDefault')
    wrapper.find('form').simulate('submit', event)
    // how would you know that function is called
    expect(event.preventDefault).toBeCalled()
  })
})
  const callback = jest.fn();
  MouseEvent.prototype.stopPropagation = callback;
  const element = createElement({});
  element.shadowRoot.querySelector('ul').click();
  expect(callback).toHaveBeenCalledTimes(1);