@@ -15,12 +15,17 @@ const Field = (props: React.ComponentProps<typeof FieldComponent>) => (
15
15
16
16
const FieldWithEditableInteractions = ( {
17
17
onAddFieldToObjectFieldClick,
18
+ onFieldNameChange,
18
19
...fieldProps
19
20
} : React . ComponentProps < typeof FieldComponent > & {
20
21
onAddFieldToObjectFieldClick ?: ( ) => void ;
22
+ onFieldNameChange ?: ( newName : string ) => void ;
21
23
} ) => {
22
24
return (
23
- < EditableDiagramInteractionsProvider onAddFieldToObjectFieldClick = { onAddFieldToObjectFieldClick } >
25
+ < EditableDiagramInteractionsProvider
26
+ onAddFieldToObjectFieldClick = { onAddFieldToObjectFieldClick }
27
+ onFieldNameChange = { onFieldNameChange }
28
+ >
24
29
< FieldComponent { ...fieldProps } />
25
30
</ EditableDiagramInteractionsProvider >
26
31
) ;
@@ -81,7 +86,63 @@ describe('field', () => {
81
86
const button = screen . queryByRole ( 'button' ) ;
82
87
expect ( button ) . not . toBeInTheDocument ( ) ;
83
88
} ) ;
89
+
90
+ it ( 'Should allow field name editing an editable field' , async ( ) => {
91
+ const onFieldNameChangeMock = vi . fn ( ) ;
92
+
93
+ const fieldId = [ 'ordersId' ] ;
94
+ const newFieldName = 'newFieldName' ;
95
+ render (
96
+ < FieldWithEditableInteractions
97
+ { ...DEFAULT_PROPS }
98
+ id = { fieldId }
99
+ editable = { true }
100
+ onFieldNameChange = { onFieldNameChangeMock }
101
+ /> ,
102
+ ) ;
103
+ const fieldName = screen . getByText ( 'ordersId' ) ;
104
+ expect ( fieldName ) . toBeInTheDocument ( ) ;
105
+ await userEvent . dblClick ( fieldName ) ;
106
+ const input = screen . getByDisplayValue ( 'ordersId' ) ;
107
+ expect ( input ) . toBeInTheDocument ( ) ;
108
+ await userEvent . clear ( input ) ;
109
+ await userEvent . type ( input , newFieldName ) ;
110
+ expect ( input ) . toHaveValue ( newFieldName ) ;
111
+ expect ( onFieldNameChangeMock ) . not . toHaveBeenCalled ( ) ;
112
+ await userEvent . type ( input , '{enter}' ) ;
113
+ expect ( onFieldNameChangeMock ) . toHaveBeenCalledWith ( DEFAULT_PROPS . nodeId , fieldId , newFieldName ) ;
114
+ } ) ;
115
+
116
+ it ( 'Should not allow field name editing if a field is not editable' , async ( ) => {
117
+ const onFieldNameChangeMock = vi . fn ( ) ;
118
+
119
+ const fieldId = [ 'ordersId' ] ;
120
+ render (
121
+ < FieldWithEditableInteractions
122
+ { ...DEFAULT_PROPS }
123
+ id = { fieldId }
124
+ editable = { false }
125
+ onFieldNameChange = { onFieldNameChangeMock }
126
+ /> ,
127
+ ) ;
128
+ const fieldName = screen . getByText ( 'ordersId' ) ;
129
+ expect ( fieldName ) . toBeInTheDocument ( ) ;
130
+ await userEvent . dblClick ( fieldName ) ;
131
+ expect ( screen . queryByDisplayValue ( 'ordersId' ) ) . not . toBeUndefined ( ) ;
132
+ } ) ;
133
+
134
+ it ( 'Should not allow editing if there is no callback' , async ( ) => {
135
+ const fieldId = [ 'ordersId' ] ;
136
+ render (
137
+ < FieldWithEditableInteractions { ...DEFAULT_PROPS } id = { fieldId } editable = { true } onFieldNameChange = { undefined } /> ,
138
+ ) ;
139
+ const fieldName = screen . getByText ( 'ordersId' ) ;
140
+ expect ( fieldName ) . toBeInTheDocument ( ) ;
141
+ await userEvent . dblClick ( fieldName ) ;
142
+ expect ( screen . queryByDisplayValue ( 'ordersId' ) ) . not . toBeUndefined ( ) ;
143
+ } ) ;
84
144
} ) ;
145
+
85
146
describe ( 'With specific types' , ( ) => {
86
147
it ( 'shows [] with "array"' , ( ) => {
87
148
render ( < Field { ...DEFAULT_PROPS } type = "array" /> ) ;
0 commit comments