Skip to content

Commit 07053d8

Browse files
committed
test: add field-list tests
1 parent d050d64 commit 07053d8

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/components/field/field-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const FieldContent = ({
132132
/>
133133
{hasCollapseButton && (
134134
<DiagramIconButton
135-
data-testid={`object-field-expand-toggle-${nodeId}-${typeof id === 'string' ? id : id.join('.')}`}
135+
data-testid={`field-expand-toggle-${nodeId}-${typeof id === 'string' ? id : id.join('.')}`}
136136
onClick={handleFieldExpandToggle}
137137
aria-label={expanded ? 'Collapse Field' : 'Expand Field'}
138138
title={expanded ? 'Collapse Field' : 'Expand Field'}

src/components/field/field-list.test.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ const FieldWithEditableInteractions = ({
88
onAddFieldToObjectFieldClick,
99
onFieldNameChange,
1010
onFieldClick,
11+
onFieldExpandToggle,
1112
...props
1213
}: Partial<React.ComponentProps<typeof FieldListComponent>> & {
1314
onAddFieldToObjectFieldClick?: () => void;
1415
onFieldNameChange?: (newName: string) => void;
1516
onFieldClick?: () => void;
17+
onFieldExpandToggle?: () => void;
1618
}) => {
1719
return (
1820
<EditableDiagramInteractionsProvider
1921
onFieldClick={onFieldClick}
2022
onAddFieldToObjectFieldClick={onAddFieldToObjectFieldClick}
2123
onFieldNameChange={onFieldNameChange}
24+
onFieldExpandToggle={onFieldExpandToggle}
2225
>
2326
<FieldListComponent nodeType="collection" nodeId="coll" fields={[]} {...props} />
2427
</EditableDiagramInteractionsProvider>
@@ -54,4 +57,46 @@ describe('field-list', () => {
5457
nodeId: 'coll',
5558
});
5659
});
60+
61+
it('should filter out children of collapsed fields', () => {
62+
render(
63+
<FieldWithEditableInteractions
64+
fields={[
65+
{ id: ['expandedParent'], name: 'expandedParent', expanded: true },
66+
{ id: ['expandedParent', 'child1'], name: 'visibleChild1', depth: 1 },
67+
{ id: ['expandedParent', 'child2'], name: 'visibleChild2', depth: 1 },
68+
{ id: ['collapsedParent'], name: 'collapsedParent', expanded: false },
69+
{ id: ['collapsedParent', 'child1'], name: 'invisibleChild1', depth: 1 },
70+
{ id: ['collapsedParent', 'child2'], name: 'invisibleChild2', depth: 1 },
71+
{ id: ['other'], name: 'other' },
72+
]}
73+
/>,
74+
);
75+
expect(screen.getByText('expandedParent')).toBeInTheDocument();
76+
expect(screen.getByText('visibleChild1')).toBeInTheDocument();
77+
expect(screen.getByText('visibleChild2')).toBeInTheDocument();
78+
expect(screen.getByText('collapsedParent')).toBeInTheDocument();
79+
expect(screen.queryByText('invisibleChild1')).not.toBeInTheDocument();
80+
expect(screen.queryByText('invisibleChild2')).not.toBeInTheDocument();
81+
expect(screen.getByText('other')).toBeInTheDocument();
82+
});
83+
84+
it('should ensure that items that do have children are collapsible', () => {
85+
const onFieldExpandToggle = vi.fn();
86+
render(
87+
<FieldWithEditableInteractions
88+
onFieldExpandToggle={onFieldExpandToggle}
89+
fields={[
90+
{ id: ['other'], name: 'other' },
91+
{ id: ['parent'], name: 'parent', expanded: true },
92+
{ id: ['parent', 'child1'], name: 'child1', depth: 1 },
93+
{ id: ['parent', 'child2'], name: 'child2', depth: 1 },
94+
]}
95+
/>,
96+
);
97+
expect(screen.queryByTestId('field-expand-toggle-coll-other')).not.toBeInTheDocument();
98+
expect(screen.getByTestId('field-expand-toggle-coll-parent')).toBeInTheDocument();
99+
expect(screen.queryByTestId('field-expand-toggle-coll-parent-child1')).not.toBeInTheDocument();
100+
expect(screen.queryByTestId('field-expand-toggle-coll-parent-child2')).not.toBeInTheDocument();
101+
});
57102
});

src/components/field/field-list.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const FieldList = ({ fields: allFields, nodeId, nodeType, isHovering }: P
6565
const selectedGroupHeight = useMemo(() => {
6666
return isFieldSelectionEnabled ? getSelectedFieldGroupHeight(fields) : undefined;
6767
}, [fields, isFieldSelectionEnabled]);
68+
6869
return (
6970
<NodeFieldWrapper>
7071
{fields.map(({ id, name, type: fieldType, ...rest }, i) => {

0 commit comments

Comments
 (0)