Skip to content

Commit 2a20aa0

Browse files
committed
add tests
1 parent 1d5885c commit 2a20aa0

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/components/field/field.test.tsx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ const Field = (props: React.ComponentProps<typeof FieldComponent>) => (
1515

1616
const FieldWithEditableInteractions = ({
1717
onAddFieldToObjectFieldClick,
18+
onFieldNameChange,
1819
...fieldProps
1920
}: React.ComponentProps<typeof FieldComponent> & {
2021
onAddFieldToObjectFieldClick?: () => void;
22+
onFieldNameChange?: (newName: string) => void;
2123
}) => {
2224
return (
23-
<EditableDiagramInteractionsProvider onAddFieldToObjectFieldClick={onAddFieldToObjectFieldClick}>
25+
<EditableDiagramInteractionsProvider
26+
onAddFieldToObjectFieldClick={onAddFieldToObjectFieldClick}
27+
onFieldNameChange={onFieldNameChange}
28+
>
2429
<FieldComponent {...fieldProps} />
2530
</EditableDiagramInteractionsProvider>
2631
);
@@ -81,7 +86,63 @@ describe('field', () => {
8186
const button = screen.queryByRole('button');
8287
expect(button).not.toBeInTheDocument();
8388
});
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+
});
84144
});
145+
85146
describe('With specific types', () => {
86147
it('shows [] with "array"', () => {
87148
render(<Field {...DEFAULT_PROPS} type="array" />);

0 commit comments

Comments
 (0)