import * as React from 'react'; import { render, waitFor, screen, fireEvent } from '@testing-library/react'; import FormDataConsumer, { FormDataConsumerView } from './FormDataConsumer'; import { testDataProvider } from '../dataProvider'; import { AdminContext, BooleanInput, SimpleForm, TextInput, SimpleFormIterator, ArrayInput, } from 'ra-ui-materialui'; import expect from 'expect'; describe('FormDataConsumerView', () => { it('does not call its children function with scopedFormData and getSource if it did not receive an index prop', () => { const children = jest.fn(); const formData = { id: 123, title: 'A title' }; render( {children} ); expect(children).toHaveBeenCalledWith({ formData, }); }); it('calls its children function with scopedFormData and getSource if it received an index prop', () => { const children = jest.fn(({ getSource }) => { getSource('id'); return null; }); const formData = { id: 123, title: 'A title', authors: [{ id: 0 }] }; render( {children} ); expect(children.mock.calls[0][0].formData).toEqual(formData); expect(children.mock.calls[0][0].scopedFormData).toEqual({ id: 0 }); expect(children.mock.calls[0][0].getSource('id')).toEqual( 'authors[0].id' ); }); it('calls its children with updated formData on first render', async () => { let globalFormData; render( {({ formData, ...rest }) => { globalFormData = formData; return ; }} ); await waitFor(() => { expect(globalFormData).toEqual({ hi: true, bye: undefined }); }); }); it('should be reactive', async () => { render( {({ formData, ...rest }) => !formData.hi ? ( ) : null } ); await waitFor(() => { expect( screen.queryByLabelText('resources.undefined.fields.bye') ).toBeNull(); }); fireEvent.click(screen.getByLabelText('resources.undefined.fields.hi')); await waitFor(() => { expect( screen.getByLabelText('resources.undefined.fields.bye') ).not.toBeNull(); }); }); it('calls its children with updated scopedFormData when inside an ArrayInput', async () => { let globalScopedFormData; render( {({ formData, scopedFormData, getSource, ...rest }) => { globalScopedFormData = scopedFormData; return scopedFormData && scopedFormData.name ? ( ) : null; }} ); expect(globalScopedFormData).toEqual(undefined); fireEvent.click(screen.getByLabelText('ra.action.add')); expect(globalScopedFormData).toEqual({ name: '' }); fireEvent.change( screen.getByLabelText('resources.undefined.fields.authors.name'), { target: { value: 'a' }, } ); await waitFor(() => { expect(globalScopedFormData).toEqual({ name: 'a', role: undefined, }); }); }); });