0

I am using AOR v1.4.0 and trying to write a unit test to test the rendering of a simple list with one row. But nothing gets logged on console as 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


    });
});

The original component

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

Can some one please modify & suggest on how to mock the restClient using JEST ? I guess that is the place I am going wrong.

Also, is there a better way to test the list in isolation ?

Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89

1 Answers1

1

As your restClient is async, you have to wait for the next tick to get something in return, see https://stackoverflow.com/a/43855794/1333479

François Zaninotto
  • 7,068
  • 2
  • 35
  • 56