Skip to content

Commit 8f1e0a9

Browse files
authored
fix/COMPASS-9961 make sure id is not silently swallowed on the Field component (#155)
* fix/COMPASS-9961 make sure id is not silently swallowed on the Field component * update mock data in tests
1 parent 6bb6553 commit 8f1e0a9

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { userEvent } from '@testing-library/user-event';
2+
3+
import { render, screen } from '@/mocks/testing-utils';
4+
import { FieldList as FieldListComponent } from '@/components/field/field-list';
5+
import { EditableDiagramInteractionsProvider } from '@/hooks/use-editable-diagram-interactions';
6+
7+
const FieldWithEditableInteractions = ({
8+
onAddFieldToObjectFieldClick,
9+
onFieldNameChange,
10+
onFieldClick,
11+
...props
12+
}: Partial<React.ComponentProps<typeof FieldListComponent>> & {
13+
onAddFieldToObjectFieldClick?: () => void;
14+
onFieldNameChange?: (newName: string) => void;
15+
onFieldClick?: () => void;
16+
}) => {
17+
return (
18+
<EditableDiagramInteractionsProvider
19+
onFieldClick={onFieldClick}
20+
onAddFieldToObjectFieldClick={onAddFieldToObjectFieldClick}
21+
onFieldNameChange={onFieldNameChange}
22+
>
23+
<FieldListComponent nodeType="collection" nodeId="coll" fields={[]} {...props} />
24+
</EditableDiagramInteractionsProvider>
25+
);
26+
};
27+
28+
describe('field-list', () => {
29+
it('Should call field click callback with field id (and fallback to name when id missing)', async () => {
30+
const onFieldClick = vi.fn();
31+
render(
32+
<FieldWithEditableInteractions
33+
onFieldClick={onFieldClick}
34+
fields={[
35+
{ name: 'field-with-just-name', selectable: true },
36+
{ id: ['field', 'with', 'id'], name: 'and-custom-name', selectable: true },
37+
]}
38+
/>,
39+
);
40+
expect(screen.getByText('field-with-just-name')).toBeInTheDocument();
41+
expect(screen.getByText('and-custom-name')).toBeInTheDocument();
42+
43+
await userEvent.click(screen.getByText('field-with-just-name'));
44+
expect(onFieldClick).toHaveBeenCalledTimes(1);
45+
expect(onFieldClick.mock.lastCall?.[1]).toEqual({
46+
id: 'field-with-just-name',
47+
nodeId: 'coll',
48+
});
49+
50+
await userEvent.click(screen.getByText('and-custom-name'));
51+
expect(onFieldClick).toHaveBeenCalledTimes(2);
52+
expect(onFieldClick.mock.lastCall?.[1]).toEqual({
53+
id: ['field', 'with', 'id'],
54+
nodeId: 'coll',
55+
});
56+
});
57+
});

src/components/field/field-list.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const FieldList = ({ fields, nodeId, nodeType, isHovering }: Props) => {
3737
return (
3838
<Field
3939
key={key}
40+
id={id ?? name}
4041
name={name}
4142
nodeId={nodeId}
4243
nodeType={nodeType}

src/components/field/field.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const FieldWithEditableInteractions = ({
3333

3434
describe('field', () => {
3535
const DEFAULT_PROPS: ComponentProps<typeof Field> = {
36+
id: 'ordersId',
3637
nodeType: 'collection',
3738
name: 'ordersId',
3839
nodeId: 'pineapple',

src/components/field/field.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { animatedBlueBorder, ellipsisTruncation } from '@/styles/styles';
1010
import { DEFAULT_DEPTH_SPACING, DEFAULT_FIELD_HEIGHT } from '@/utilities/constants';
1111
import { FieldDepth } from '@/components/field/field-depth';
1212
import { FieldTypeContent } from '@/components/field/field-type-content';
13-
import { NodeField, NodeGlyph, NodeType } from '@/types';
13+
import { FieldId, NodeField, NodeGlyph, NodeType } from '@/types';
1414
import { PreviewGroupArea } from '@/utilities/get-preview-group-area';
1515
import { useEditableDiagramInteractions } from '@/hooks/use-editable-diagram-interactions';
1616

@@ -123,6 +123,7 @@ const IconWrapper = styled(Icon)`
123123
`;
124124

125125
interface Props extends NodeField {
126+
id: FieldId;
126127
nodeId: string;
127128
nodeType: NodeType;
128129
spacing: number;
@@ -136,7 +137,7 @@ export const Field = ({
136137
isHovering = false,
137138
name,
138139
nodeId,
139-
id = name,
140+
id,
140141
depth = 0,
141142
type,
142143
nodeType,

0 commit comments

Comments
 (0)