Javascript 使用Jest进行React组件测试

Javascript 使用Jest进行React组件测试,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,大家好,我对Jest和Java脚本不熟悉。我想对我的一个组件执行测试 我想检查管理员是否看到以下句子: “请选择一个用户以显示其捐赠:” 我的建议是: const sentence = "Please select a user to show his/her donations:" it('Shows: Please select a user to show his/her donations:', () => { const admin = shallow(<AdminVi

大家好,我对Jest和Java脚本不熟悉。我想对我的一个组件执行测试

我想检查管理员是否看到以下句子:

“请选择一个用户以显示其捐赠:”

我的建议是:

const sentence = "Please select a user to show his/her donations:"
it('Shows: Please select a user to show his/her donations:', () => {
  const admin = shallow(<AdminViewComponent />);
  const wantedSentence = admin.find(sentence);
  expect(wantedSentence).toEqual(true);
});

如果向DOM节点添加一个可选属性,则会更容易

  it('Shows: Please select a user to show his/her donations:', () => {
    const admin = mount(<AdminViewComponent />);
    const actualText = admin.find("[data-id='someSelector']").text();
    const expectedText = "Please select a user to show his/her donations";

    expect(actualText).toEqual(expectedText);
  });

  /*
     Alternately you could use a snapshot test as this would remove 
     the need to copy the text into the test
  */


  it('Shows: Please select a user to show his/her donations:', () => {
    const admin = mount(<AdminViewComponent />);
    const actualText = admin.find("[data-id='someSelector']").text();

    expect(actualText).toMatchSnapshot();
  });
it('显示:请选择一个用户显示他/她的捐赠:',()=>{
常量admin=mount();
const actualText=admin.find(“[data id='someSelector']”)。text();
const expectedText=“请选择一个用户以显示其捐赠”;
expect(实际文本);toEqual(预期文本);
});
/*
或者,您可以使用快照测试,因为这将删除
需要将文本复制到测试中
*/
它('显示:请选择一个用户来显示他/她的捐赠:',()=>{
常量admin=mount();
const actualText=admin.find(“[data id='someSelector']”)。text();
expect(actualText).toMatchSnapshot();
});

由于div元素上没有类或id,因此使用
.find()
检索它应该很困难。幸运的是,您还可以使用
.containsMatchingElement(节点)
检查组件是否包含元素,而不是选择器。换句话说,您可以执行以下操作:

  const elementToCheck = "<div> Please select user to show his/her donations </div>"
  it('Shows: Please select a user to show his/her donations:', () => {
    const admin = shallow(<AdminViewComponent />);
   expect(admin.containsMatchingElement(elementToCheck)).toEqual(true);
  });
const elementToCheck=“请选择用户以显示其捐赠”
它('显示:请选择一个用户来显示他/她的捐赠:',()=>{

const admin=shallow(.

.find()
接受选择器作为其参数,而不是文本。在测试中,您需要模拟axiosCan。请在我的测试中使用它,以便我可以看到如何操作?请查看我如何编辑我的测试建议
  it('Shows: Please select a user to show his/her donations:', () => {
    const admin = mount(<AdminViewComponent />);
    const actualText = admin.find("[data-id='someSelector']").text();
    const expectedText = "Please select a user to show his/her donations";

    expect(actualText).toEqual(expectedText);
  });

  /*
     Alternately you could use a snapshot test as this would remove 
     the need to copy the text into the test
  */


  it('Shows: Please select a user to show his/her donations:', () => {
    const admin = mount(<AdminViewComponent />);
    const actualText = admin.find("[data-id='someSelector']").text();

    expect(actualText).toMatchSnapshot();
  });
  const elementToCheck = "<div> Please select user to show his/her donations </div>"
  it('Shows: Please select a user to show his/her donations:', () => {
    const admin = shallow(<AdminViewComponent />);
   expect(admin.containsMatchingElement(elementToCheck)).toEqual(true);
  });