AOR - UnitTest一个简单的列表

时间:2018-03-19 17:18:00

标签: admin-on-rest

我正在使用AOR v1.4.0并尝试编写单元测试来测试一行的简单列表的呈现。但是没有任何内容在HTML

上记录在控制台上
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as Renderer from 'react-test-renderer';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import {render, shallow, mount} from 'enzyme';
import {Datagrid, List, Admin, Resource} from 'admin-on-rest';
import {CategoryList} from '../Categories';


describe('Categories', ()=>{
    it('renders category list correctly', ()=>{

        const wrapper = mount(
                                <Admin title="Mock Admin Client"  restClient={ jest.fn().mockImplementation(()=>{
                                    return new Promise((res, rej)=>{
                                        res({
                                            total : 1,
                                            data: ()=>{
                                                return {
                                                    id: "0300b4cf-4888-4e73-b4e1-25cf4686e05c",
                                                    name: "cat2",
                                                    displaySequence: 121
                                                }
                                            }
                                        });
                                    });
                                })}>
                                    <Resource options={{ label: 'Categories' }} name="category" list={CategoryList}/>                                    
                                </Admin>
                             );

                            console.log(wrapper.html());//<-- does not log anything


    });
});

原始组件

export const CategoryList = (props: any) => (
  <List {...props}  perPage={50}>
    <Datagrid>
      <TextField source="id" />
      <TextField source="name" />
      <TextField source="displaySequence" />
      <EditButton/>
      <ShowButton/>
    </Datagrid>
  </List>
);

有人可以修改&amp;建议如何使用JEST模拟restClient?我想那是我出错的地方。

此外,是否有更好的方法可以单独测试列表?

1 个答案:

答案 0 :(得分:1)

由于您的restClient是异步的,您必须等待下一个刻度以获得回报,请参阅https://stackoverflow.com/a/43855794/1333479