测试函数作为道具传递给组件

问题描述 投票:1回答:1

我有一个使用Antd表的表组件。为了突出显示一行,我传递了一个函数:

rowClassName={(record, index) => record.id === props.highlightRow ? classes.Highlight : ''}

现在,我的测试覆盖率表明该行未包含在测试中。

enter image description here

我该怎么做?

reactjs jestjs enzyme
1个回答
0
投票
it('should highlight row when id matches highlightRow', () => {
  const className = shallow(<YourComponent highLightRow="testId" />).find(Table).prop('rowClassName')({
    id: 'testId'
  });

  expect(className).toBe(classes.Highlight);
});

it('should not highlight row when id does not match highlightRow', () => {
  const className = shallow(<YourComponent highLightRow="newId" />).find(Table).prop('rowClassName')({
    id: 'testId'
  });

  expect(className).not.toBe(classes.Highlight);
});

© www.soinside.com 2019 - 2024. All rights reserved.