diff --git a/packages/ra-core/src/controller/input/useReferenceArrayInputController.spec.tsx b/packages/ra-core/src/controller/input/useReferenceArrayInputController.spec.tsx index 23401c4852c..830f75e6826 100644 --- a/packages/ra-core/src/controller/input/useReferenceArrayInputController.spec.tsx +++ b/packages/ra-core/src/controller/input/useReferenceArrayInputController.spec.tsx @@ -3,13 +3,20 @@ import { ReactElement } from 'react'; import expect from 'expect'; import { render, screen, waitFor, fireEvent } from '@testing-library/react'; -import { useReferenceArrayInputController } from './useReferenceArrayInputController'; +import { + useReferenceArrayInputController, + UseReferenceArrayInputParams, +} from './useReferenceArrayInputController'; import { CoreAdminContext } from '../../core'; import { testDataProvider } from '../../dataProvider'; -import { Form } from '../../form'; +import { ChoicesContextValue, Form } from '../../form'; import { SORT_ASC } from '../list/queryReducer'; -const ReferenceArrayInputController = props => { +const ReferenceArrayInputController = ( + props: UseReferenceArrayInputParams & { + children: (params: ChoicesContextValue) => ReactElement; + } +) => { const { children, ...rest } = props; return children(useReferenceArrayInputController(rest)) as ReactElement; }; @@ -139,8 +146,8 @@ describe('useReferenceArrayInputController', () => { Promise.reject(new Error('boom')), + // @ts-ignore getMany: () => - // @ts-ignore Promise.resolve({ data: [{ id: 1, title: 'foo' }], }), @@ -194,6 +201,40 @@ describe('useReferenceArrayInputController', () => { }); }); + it('should call getList with meta when provided', async () => { + const children = jest.fn(() =>
); + const dataProvider = testDataProvider({ + // @ts-ignore + getList: jest + .fn() + .mockResolvedValue(Promise.resolve({ data: [], total: 0 })), + }); + render( + +
+ + {children} + +
+
+ ); + expect(dataProvider.getList).toHaveBeenCalledWith('tags', { + pagination: { + page: 1, + perPage: 25, + }, + sort: { + field: 'id', + order: 'DESC', + }, + filter: {}, + meta: { value: 'a' }, + }); + }); + it('should allow to customize getList arguments with perPage, sort, and filter props', () => { const children = jest.fn(() =>
); const dataProvider = testDataProvider({ @@ -294,6 +335,36 @@ describe('useReferenceArrayInputController', () => { }); }); + it('should call getMany with meta when provided', async () => { + const children = jest.fn(() =>
); + const dataProvider = testDataProvider({ + // @ts-ignore + getMany: jest + .fn() + .mockResolvedValue( + Promise.resolve({ data: [{ id: 5 }, { id: 6 }] }) + ), + }); + render( + +
+ + {children} + +
+
+ ); + await waitFor(() => { + expect(dataProvider.getMany).toHaveBeenCalledWith('tags', { + ids: [5, 6], + meta: { value: 'a' }, + }); + }); + }); + it('should not call getMany when calling setFilters', async () => { const children = jest.fn(({ setFilters }) => (