首页 > 解决方案 > React 测试库 - 无法找到带有文本的元素

问题描述

我希望测试确保子组件未呈现

这是我的测试:

test(' Renders not render upload-model step for other impl type', () => {
    render(<StepSelector />, {
      initialState: { implOnboard: { ...initialState, onboardType: 'SOMETHING_ELSE' } },
    });
    expect(screen.getByText('Upload Model')).not.toBeDefined();
});

零件

 const renderSecondStep = () => {
    switch (onboardType) {
      case IMPL_TYPES.PRE_TRAINED:
        return (
          <StepBox
            completedSteps={0}
            totalSteps={1}
            stepNumber={2}
            title="Upload Model"
            active={true}
            data-testid="upload-model"
          />
        );
      default:
        return <React.Fragment></React.Fragment>; // 
    }
  };

错误

TestingLibraryElementError: Unable to find an element with 
the text: Upload Model. This could be because the text is broken up by multiple elements. 
In this case, you can provide a function for your text matcher to make your matcher more flexible.

标签: reactjsreact-reduxreact-testing-library

解决方案


推荐阅读