Skip to content

Commit c4c2e94

Browse files
committed
refactor: updated DialogActions.tsx and tests
1 parent 8f4cb22 commit c4c2e94

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/components/Dialog/DialogActions.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ const DialogActions = ({ children, style, theme, ...rest }: Props) => {
6161
return (
6262
<View {...rest} style={[styles.v3Container, style]}>
6363
{actions.map((child, index) => (
64-
<View
65-
key={child.key ?? index}
66-
style={[
67-
index === actions.length - 1 ? styles.itemLast : styles.item,
68-
child.props.style,
69-
]}
64+
<React.Fragment
65+
key={
66+
React.isValidElement(child) && child.key != null ? child.key : index
67+
}
7068
>
69+
{index > 0 && <View style={styles.spacer} />}
7170
{child}
72-
</View>
71+
</React.Fragment>
7372
))}
7473
</View>
7574
);
@@ -86,11 +85,8 @@ const styles = StyleSheet.create({
8685
paddingBottom: 24,
8786
paddingHorizontal: 24,
8887
},
89-
item: {
90-
marginRight: 8,
91-
},
92-
itemLast: {
93-
marginRight: 0,
88+
spacer: {
89+
width: 8,
9490
},
9591
});
9692

src/components/__tests__/Dialog.test.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,36 @@ describe('DialogActions', () => {
219219
);
220220

221221
const dialogActionsContainer = screen.getByTestId('dialog-actions');
222-
const dialogActionButtons = dialogActionsContainer.children;
222+
const dialogActionChildren = dialogActionsContainer.children;
223223

224224
expect(dialogActionsContainer).toHaveStyle({
225225
paddingBottom: 24,
226226
paddingHorizontal: 24,
227227
});
228-
expect(dialogActionButtons[0]).toHaveStyle({ marginRight: 8 });
229-
expect(dialogActionButtons[1]).toHaveStyle({ marginRight: 0 });
228+
229+
// We expect 3 children because Dialog.Actions puts <View style={{ width: 8 }} /> in between actions to add a proper styling
230+
expect(dialogActionChildren).toHaveLength(3);
231+
expect(dialogActionChildren[1]).toHaveStyle({ width: 8 });
230232
});
231233

232234
it('should apply custom styles', async () => {
233235
await render(
234236
<Dialog.Actions testID="dialog-actions">
235-
<Button style={styles.spacing}>Cancel</Button>
236-
<Button style={styles.noSpacing}>Ok</Button>
237+
<Button testID="button-cancel" style={styles.spacing}>
238+
Cancel
239+
</Button>
240+
<Button testID="button-ok" style={styles.noSpacing}>
241+
Ok
242+
</Button>
237243
</Dialog.Actions>
238244
);
239245

240-
const dialogActionsContainer = screen.getByTestId('dialog-actions');
241-
const dialogActionButtons = dialogActionsContainer.children;
242-
243-
expect(dialogActionButtons[0]).toHaveStyle({ margin: 10 });
244-
expect(dialogActionButtons[1]).toHaveStyle({ margin: 0 });
246+
expect(screen.getByTestId('button-cancel-container')).toHaveStyle({
247+
margin: 10,
248+
});
249+
expect(screen.getByTestId('button-ok-container')).toHaveStyle({
250+
margin: 0,
251+
});
245252
});
246253
});
247254

0 commit comments

Comments
 (0)