如何使用单元测试测试资源控制器 [英] How to test a Resource controller with unit test

查看:47
本文介绍了如何使用单元测试测试资源控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标准的 react-admin 结构设置,一切都按预期工作,资源从端点获取数据,用于 GET_LISTGET_ONE 问题我我想测试 CandidateView 组件,但我不知道如何向它传递一些模拟数据以检查渲染时它是否包含所有预期的数据.

I have the following standard react-admin structure set–up and everything works as expected, the resource gets the data from the endpoints for GET_LIST and GET_ONE the issue I have is I want to test the CandidateView component and I do not know how to pass it some mocked data to check when rendered it will contain all the data expected.

<Admin
    title="Admin"
    dashboard={Home}
    dataProvider={restProvider}
    history={history}
    loginPage={Login}
    menu={Menu}
  >
    <Resource
      name="candidates"
      list={Candidates}
      icon={UserIcon}
      edit={CandidateEdit}
      show={CandidateView}
    />
  </Admin>

我是否必须挂载资源才能进行测试,还是有更好的方法来挂载和测试我感兴趣的子组件?

Do I have to mount the Resource to be able to test this or is there a better way in which I can just mount and test the subcomponents that I'm interested in?

推荐答案

老实说,React Admin 控制器旨在易于使用和破解,而不是易于测试.

To be honest, React Admin controllers are designed to be easy to use and hack with, not easy to test.

假设您使用的是这样一个简单的显示视图:

Assuming that you are using a simple show view like this one:

export const CandidateView = props => (
    <Show {...props}>
        <SimpleShowLayout>
            <TextField source="title" />
            <TextField source="position" />
            <RichTextField source="body" />
            <DateField label="Registration date" source="created_at" />
        </SimpleShowLayout>
    </Show>
);

确实,您可以阅读源代码,找到所有 HOC,注入所有必需的 props,安装视图并测试输出.这很痛苦,但可行.

Indeed, you can read the source code, find all the HOCs, inject all the required props, mounting the view and test the output. It's painful, but doable.

但如果您的唯一目标只是测试此视图是否具有正确的字段,即 titlepositionbodycreated_at,这不是一种有效的方法.

But if your only goal is just to test that this view has the correct fields that are title, position, body and created_at, it isn't an efficient way to do it.

我们将 React Admin 资源声明视为一个简单的配置.(您不会测试 XML 文件)

We see the React Admin resource declaration as a simple configuration. (you won't test XML files)

在我们的应用中,我们使用端到端测试来检查所有视图是否配置良好,以及是否具有正确的组件和行为.

您可以在 React Admin 本身的页面上找到使用 cypress 或 selenium 进行 E2E 测试的示例.查看cypress/integration 文件夹以查看我们运行的所有测试.

You can find an example of E2E tests with cypress or selenium for that page on React Admin itself. Take a look in cypress/integration folder to see all the tests we run.

这篇关于如何使用单元测试测试资源控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆