From a77e902df88cef077347b127f44cce8772fd87af Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Wed, 21 Aug 2019 14:33:33 +0200 Subject: [PATCH] Fix unit tests --- .../ReferenceArrayFieldController.spec.tsx | 50 +++++++++++++------ .../src/field/ReferenceArrayField.spec.js | 3 ++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/packages/ra-core/src/controller/field/ReferenceArrayFieldController.spec.tsx b/packages/ra-core/src/controller/field/ReferenceArrayFieldController.spec.tsx index acfd5360cd..65ba59127a 100644 --- a/packages/ra-core/src/controller/field/ReferenceArrayFieldController.spec.tsx +++ b/packages/ra-core/src/controller/field/ReferenceArrayFieldController.spec.tsx @@ -1,9 +1,10 @@ import React from 'react'; import { cleanup } from '@testing-library/react'; +import expect from 'expect'; import ReferenceArrayFieldController from './ReferenceArrayFieldController'; +import { DataProviderContext } from '../../dataProvider'; import renderWithRedux from '../../util/renderWithRedux'; -import { crudGetManyAccumulate } from '../../actions'; describe('', () => { afterEach(cleanup); @@ -33,13 +34,15 @@ describe('', () => { expect(children.mock.calls[0][0]).toEqual({ currentSort: { field: 'id', order: 'ASC' }, loaded: false, + loading: true, referenceBasePath: '', - data: null, + data: {}, ids: [1, 2], + error: null, }); }); - it('should set the loaded prop to true when at least one related record is found', () => { + it('should set the loaded prop to false when at least one related record is not found', () => { const children = jest.fn().mockReturnValue('child'); renderWithRedux( @@ -70,7 +73,8 @@ describe('', () => { expect(children.mock.calls[0][0]).toEqual({ currentSort: { field: 'id', order: 'ASC' }, - loaded: true, + loaded: false, + loading: true, referenceBasePath: '', data: { 2: { @@ -79,6 +83,7 @@ describe('', () => { }, }, ids: [1, 2], + error: null, }); }); @@ -110,12 +115,14 @@ describe('', () => { expect(children.mock.calls[0][0]).toEqual({ currentSort: { field: 'id', order: 'ASC' }, loaded: true, + loading: true, referenceBasePath: '', data: { 1: { id: 1, title: 'hello' }, 2: { id: 2, title: 'world' }, }, ids: [1, 2], + error: null, }); }); @@ -147,27 +154,37 @@ describe('', () => { expect(children.mock.calls[0][0]).toEqual({ currentSort: { field: 'id', order: 'ASC' }, loaded: true, + loading: true, referenceBasePath: '', data: { 'abc-1': { id: 'abc-1', title: 'hello' }, 'abc-2': { id: 'abc-2', title: 'world' }, }, ids: ['abc-1', 'abc-2'], + error: null, }); }); - it('should dispatch crudGetManyAccumulate', () => { + it('should call the dataProvider with GET_MANY on mount', async () => { const children = jest.fn().mockReturnValue('child'); + const dataProvider = jest.fn(); + dataProvider.mockReturnValueOnce( + Promise.resolve({ + data: [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }], + }) + ); const { dispatch } = renderWithRedux( - - {children} - , + + + {children} + + , { admin: { resources: { @@ -178,6 +195,9 @@ describe('', () => { }, } ); - expect(dispatch).toBeCalledWith(crudGetManyAccumulate('bar', [1, 2])); + await new Promise(resolve => setTimeout(resolve, 10)); + expect(dispatch).toBeCalledTimes(5); + expect(dispatch.mock.calls[0][0].type).toBe('RA/CRUD_GET_MANY'); + expect(dataProvider).toBeCalledTimes(1); }); }); diff --git a/packages/ra-ui-materialui/src/field/ReferenceArrayField.spec.js b/packages/ra-ui-materialui/src/field/ReferenceArrayField.spec.js index 544913d92f..2960feafee 100644 --- a/packages/ra-ui-materialui/src/field/ReferenceArrayField.spec.js +++ b/packages/ra-ui-materialui/src/field/ReferenceArrayField.spec.js @@ -93,6 +93,7 @@ describe('', () => { source="barIds" basePath="" data={data} + loaded={true} ids={['abc-1', 'abc-2']} > @@ -121,6 +122,7 @@ describe('', () => { source="barIds" basePath="" data={data} + loaded={true} ids={[1, 2]} > @@ -151,6 +153,7 @@ describe('', () => { basePath="" data={data} ids={[1, 2]} + loaded={true} >