【问题标题】:Can I test a function mapping data into a child component using Jest and Enzyme我可以使用 Jest 和 Enzyme 测试将数据映射到子组件的函数吗
【发布时间】:2019-10-25 13:22:23
【问题描述】:

我正在进行一个返回对象数组的 API 调用。然后我将这些对象映射到一个子组件上。我对如何测试这一点的想法是使用 Jest 模拟 API 调用并测试以查看渲染的子组件的数量是否等于对象的数量。我实际上不确定我是否需要模拟 API 调用,并认为只声明一个数组并使用它可能更简单。

这是我将用于 API 调用返回的数组示例。

let organizationRoles = [
    {name : "2nd Project Manager", assigned : false}, 
    {name : "Executive Sponser", assigned: false}, 
    {name : "CFO or Finance Contact", assigned: false},
    {name : "OI&T Contact", assigned: false},
    {name : "Payroll Contact", assigned: false},
    {name : "HR Contact", assigned: false}
]

这是我要运行的测试。

it('renders the appropriate number of cards', () => {
    const wrapper = mount(<AssignRoles />);
    const cardHolder = wrapper.find('#assign-roles-roles-page');
    expect(cardHolder.children()).toHaveLength(organizationRoles.length)
})

我的主要问题是我还没有弄清楚在哪里注入数组。

这是我想从 AssignRoles 组件中测试的代码。

<div className={styles.rolesPage} id="assign-roles-roles-page">
   {organizationRoles.map((roles, key)=> {
       return(<AssignRolesCard name={roles.name} assignee={roles.assignee} id={key} handleFormOpen={handleFormOpen}/>)
   })}
</div>

这里要求的是我的 API 调用:

axios
      .get(URL, config)
      .then(response => {
        const organizationRoles = response.data;
      })
      .catch(error => {
        if (error.response.status === 401 && token) {
          this.renewToken();
        }
      });

我对 Jest 测试还很陌生,想了解如何在我的测试中更好地使用模拟。

【问题讨论】:

  • 你能展示组件获取数据的实现吗?
  • @Teneff 您的意思是获取组织角色的 API 调用吗?我已更新我的问题以包含此内容。

标签: javascript reactjs testing jestjs enzyme


【解决方案1】:

这主要取决于您要测试的实现。因此,如果您的组件负责从 API 获取数据,您应该模拟组件中使用的函数

例如

import axios from 'axios';

jest.mock('axios');

describe('given a component that is fetching data using fetch', () => {

  describe('if the api retuns someData', () => {
    let wrapper;
    beforeAll(() => {
      axios.get.mockResolvedValue(someData);
      wrapper = shallow(<MyComponent />);
    });
    it('should render X', () => {
      expect(wrapper). // assert something 
    });
  });

  describe('if the api returns some other data, () => {
    let wrapper;
    beforeAll(() => {
      axios.get.mockResolvedValue(someOtherData);
      wrapper = shallow(<MyComponent />);
    });
    it('should render Y', () => {
      expect(wrapper). // assert something 
    });
  });
});

如果您的组件将数据作为属性接收,您应该在测试中将数据作为属性提供:

describe('given a component that receives data as prop', () => {
  describe('with some other data, () => {
    let wrapper;
    beforeAll(() => {
      wrapper = shallow(<MyOtherComponent data={someOtherData} />);
    });
    it('should render X', () => {
      expect(wrapper). // assert something 
    });
  });
});

【讨论】:

  • 我的部分问题是将 呈现为映射函数。在上面的代码中,我将 API 数据映射到组件中。所以真的应该有多个 的实例。我不确定这个映射是如何正确发生的
  • 你可以测试如果axios.get返回一个包含6个对象的数组,你的组件应该渲染6个AssignRolesCard,或者你可以使用expect(wrapper).toMatchSnapshot(),它将保存你的组件正在渲染的快照如果实现发生变化,它也会反映在快照中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2020-05-10
  • 1970-01-01
  • 2017-02-12
  • 2017-04-13
  • 2021-06-24
  • 1970-01-01
相关资源
最近更新 更多