Arrays 如何在react中使用jest编写单元测试用例?
我正在为我的一个react应用程序编写单元测试用例,在类组件中有一些箭头函数和普通函数,但我不知道如何编写?那个么,如何在测试用例中触发下一页函数检查其状态值呢Arrays 如何在react中使用jest编写单元测试用例?,arrays,node.js,json,reactjs,jestjs,Arrays,Node.js,Json,Reactjs,Jestjs,我正在为我的一个react应用程序编写单元测试用例,在类组件中有一些箭头函数和普通函数,但我不知道如何编写?那个么,如何在测试用例中触发下一页函数检查其状态值呢 const setup = ( const store = storeFactory(initialState); return mountWithAppProvider(<Invoice store = {store}/>); } const wrapper = setup();
const setup = ( const store = storeFactory(initialState);
return mountWithAppProvider(<Invoice store = {store}/>);
}
const wrapper = setup();
test('should pagination next page works properly', () => {
wrapper.nextPage();
})
renderInvoiceTotals() {
let a = 0 ;
let b = 0;
let c = 0;
c = a+b ;
console.log("result :"c);
}
//arrow function
nextPage = () => {
const newPageNumber = this.state.currentPage + 1;
this.setState({ currentPage: newPageNumber });
};
const setup=(const store=storeFactory(initialState);
返回mountWithAppProvider();
}
const wrapper=setup();
测试('下一页分页是否正常',()=>{
wrapper.nextPage();
})
renderInvoiceTotals(){
设a=0;
设b=0;
设c=0;
c=a+b;
console.log(“结果:”c);
}
//箭头函数
下一页=()=>{
const newPageNumber=this.state.currentPage+1;
this.setState({currentPage:newPageNumber});
};
根据代码,我想从测试文件中调用nextPage函数
但无法为renderInvoice总计调用相同的
所以在我的单元测试用例中,我只想通过触发它来检查nextPage中的两个函数,我想检查expect中的状态
要调用的方法驻留在包装器的实例中。这意味着您需要像这样调用它:
wrapper.instance().nextPage();
import React from "react";
import { mount } from 'enzyme';
describe('Testing component',() => {
let wrapper = mount(<Invoice store = {store} />)
test('testing nextPage method', ()=>{
wrapper.instance().nextPage();
expect(wrapper.state('currentPage')).toEqual('the value you want here');
});
});
按如下方式设置测试文件:
wrapper.instance().nextPage();
import React from "react";
import { mount } from 'enzyme';
describe('Testing component',() => {
let wrapper = mount(<Invoice store = {store} />)
test('testing nextPage method', ()=>{
wrapper.instance().nextPage();
expect(wrapper.state('currentPage')).toEqual('the value you want here');
});
});
从“React”导入React;
从“酶”导入{mount};
描述('测试组件',()=>{
让wrapper=mount()
测试('测试下一页方法',()=>{
wrapper.instance().nextPage();
expect(wrapper.state('currentPage')).toEqual('此处需要的值');
});
});
希望这有帮助!所以你有一个组件,这个组件基本上是一个分页,你有一个下一页链接,当你点击下一页链接或按钮时,它会调用这个函数。这就是你想要实现的吗?我想为下一页编写测试用例,点击下一页方法得到的下一页分页calledconst setup=(initialState={})=>{const store=storeFactory(initialState);返回mountWithAppProvider();}const wrapper=setup();从这个包装器中,我试图获得该方法意味着我的主要座右铭是我从测试文件中调用该方法触发器它只是检查组件的当前页面状态,是1还是不在expect中,所以首先我无法触发该方法,无法访问组件状态,所以我可以在expecterror中检查它无法访问该方法类组件,当分页下一页单击时调用该下一页。设置测试文件的方式似乎不正确。test('下一页分页是否正常工作',()=>{let wrapper=mountWithAppProvider(