@@ -14,6 +14,7 @@ import { AutocompleteInput } from './AutocompleteInput';
14
14
import { useCreateSuggestionContext } from './useSupportCreateSuggestion' ;
15
15
import {
16
16
InsideReferenceInput ,
17
+ InsideReferenceInputDefaultValue ,
17
18
VeryLargeOptionsNumber ,
18
19
} from './AutocompleteInput.stories' ;
19
20
import { act } from '@testing-library/react-hooks' ;
@@ -1150,48 +1151,67 @@ describe('<AutocompleteInput />', () => {
1150
1151
expect ( screen . queryByText ( 'New Kid On The Block' ) ) . not . toBeNull ( ) ;
1151
1152
} ) ;
1152
1153
1153
- it ( 'should work inside a ReferenceInput field' , async ( ) => {
1154
- render ( < InsideReferenceInput /> ) ;
1155
- await waitFor ( ( ) => {
1156
- expect (
1157
- ( screen . getByRole ( 'textbox' ) as HTMLInputElement ) . value
1158
- ) . toBe ( 'Leo Tolstoy' ) ;
1159
- } ) ;
1160
- screen . getByRole ( 'textbox' ) . focus ( ) ;
1161
- fireEvent . click ( screen . getByLabelText ( 'Clear value' ) ) ;
1162
- await waitFor ( ( ) => {
1163
- expect ( screen . getByRole ( 'listbox' ) . children ) . toHaveLength ( 5 ) ;
1164
- } ) ;
1165
- fireEvent . change ( screen . getByRole ( 'textbox' ) , {
1166
- target : { value : 'Vic' } ,
1154
+ describe ( 'Inside <ReferenceInput>' , ( ) => {
1155
+ it ( 'should work inside a ReferenceInput field' , async ( ) => {
1156
+ render ( < InsideReferenceInput /> ) ;
1157
+ await waitFor ( ( ) => {
1158
+ expect (
1159
+ ( screen . getByRole ( 'textbox' ) as HTMLInputElement ) . value
1160
+ ) . toBe ( 'Leo Tolstoy' ) ;
1161
+ } ) ;
1162
+ screen . getByRole ( 'textbox' ) . focus ( ) ;
1163
+ fireEvent . click ( screen . getByLabelText ( 'Clear value' ) ) ;
1164
+ await waitFor ( ( ) => {
1165
+ expect ( screen . getByRole ( 'listbox' ) . children ) . toHaveLength ( 5 ) ;
1166
+ } ) ;
1167
+ fireEvent . change ( screen . getByRole ( 'textbox' ) , {
1168
+ target : { value : 'Vic' } ,
1169
+ } ) ;
1170
+ await waitFor (
1171
+ ( ) => {
1172
+ expect ( screen . getByRole ( 'listbox' ) . children ) . toHaveLength (
1173
+ 1
1174
+ ) ;
1175
+ } ,
1176
+ { timeout : 2000 }
1177
+ ) ;
1178
+ expect ( screen . queryByText ( 'Leo Tolstoy' ) ) . toBeNull ( ) ;
1167
1179
} ) ;
1168
- await waitFor (
1169
- ( ) => {
1170
- expect ( screen . getByRole ( 'listbox' ) . children ) . toHaveLength ( 1 ) ;
1171
- } ,
1172
- { timeout : 2000 }
1173
- ) ;
1174
- expect ( screen . queryByText ( 'Leo Tolstoy' ) ) . toBeNull ( ) ;
1175
- } ) ;
1176
1180
1177
- it ( 'should allow to clear the value inside a ReferenceInput field' , async ( ) => {
1178
- render ( < InsideReferenceInput /> ) ;
1179
- await waitFor ( ( ) => {
1180
- expect (
1181
- ( screen . getByRole ( 'textbox' ) as HTMLInputElement ) . value
1182
- ) . toBe ( 'Leo Tolstoy' ) ;
1181
+ it ( 'should allow to clear the value inside a ReferenceInput field' , async ( ) => {
1182
+ render ( < InsideReferenceInput /> ) ;
1183
+ await waitFor ( ( ) => {
1184
+ expect (
1185
+ ( screen . getByRole ( 'textbox' ) as HTMLInputElement ) . value
1186
+ ) . toBe ( 'Leo Tolstoy' ) ;
1187
+ } ) ;
1188
+ fireEvent . click ( screen . getByLabelText ( 'Clear value' ) ) ;
1189
+ userEvent . tab ( ) ;
1190
+ // Couldn't reproduce the infinite loop issue without this timeout
1191
+ // See https://github.com/marmelab/react-admin/issues/7482
1192
+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
1193
+ await waitFor ( ( ) => {
1194
+ expect (
1195
+ ( screen . getByRole ( 'textbox' ) as HTMLInputElement ) . value
1196
+ ) . toEqual ( '' ) ;
1197
+ } ) ;
1198
+ expect ( screen . queryByText ( 'Leo Tolstoy' ) ) . toBeNull ( ) ;
1183
1199
} ) ;
1184
- fireEvent . click ( screen . getByLabelText ( 'Clear value' ) ) ;
1185
- userEvent . tab ( ) ;
1186
- // Couldn't reproduce the infinite loop issue without this timeout
1187
- // See https://github.com/marmelab/react-admin/issues/7482
1188
- await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
1189
- await waitFor ( ( ) => {
1190
- expect (
1191
- ( screen . getByRole ( 'textbox' ) as HTMLInputElement ) . value
1192
- ) . toEqual ( '' ) ;
1200
+
1201
+ it ( 'should not change an undefined value to empty string' , async ( ) => {
1202
+ const onSuccess = jest . fn ( ) ;
1203
+ render ( < InsideReferenceInputDefaultValue onSuccess = { onSuccess } /> ) ;
1204
+ const input = await screen . findByDisplayValue ( 'War and Peace' ) ;
1205
+ fireEvent . change ( input , { target : { value : 'War' } } ) ;
1206
+ screen . getByText ( 'Save' ) . click ( ) ;
1207
+ await waitFor ( ( ) => {
1208
+ expect ( onSuccess ) . toHaveBeenCalledWith (
1209
+ expect . objectContaining ( { author : null } ) ,
1210
+ expect . anything ( ) ,
1211
+ expect . anything ( )
1212
+ ) ;
1213
+ } ) ;
1193
1214
} ) ;
1194
- expect ( screen . queryByText ( 'Leo Tolstoy' ) ) . toBeNull ( ) ;
1195
1215
} ) ;
1196
1216
1197
1217
it ( "should allow to edit the input if it's inside a FormDataConsumer" , ( ) => {
0 commit comments