如何使用jest / enzyme添加Link的测试用例

时间:2020-11-04 09:07:32

标签: reactjs jestjs enzyme

我正在尝试使用玩笑和酶来编写一些测试用例,我无法为Link添加测试用例,请建议是否需要添加更多测试用例

这些是样式化的组件,什么时候应该监视/ mock函数

displayErr.tsx

export const DisplayErr = React.memo<Props>(({ text, SearchLink }) => (
    <Section>
      <h1>{text}</h1>

      {!SearchLink && <Link to={getPath('SEARCH')}>some text</Link>}
    </Section>
));

displayErr.test.tsx

describe('Error Msg', () => {
  it('styled component', () => {
    const output = mount(<DisplayErr text="hello world" SearchLink={true} />);
    const link = output.find(Link).find({ to: '/Search' });
    expect(output.find(Section).text()).toEqual('hello world');
    //expect(link).toBe('<div class="link">Login</div>');//error
  });
});

1 个答案:

答案 0 :(得分:1)

此处Link将基于SearchLink道具进行渲染。当您将其传递为true时,它将在DOM中不可用。通过SearchLink=false并执行单元测试。

describe('Error Msg', () => {
       it('styled component', () => {
       const output = mount(<DisplayErr text="hello world" SearchLink=false/>)
      expect(output.find(Link).props().to).toEqual('/search');
    });

如果您遇到相同的问题,请告诉我。

相关问题